ISSUE DESCRIPTION [BUG] API Gateway REST: path-based invocation…
- User
ISSUE DESCRIPTION
[BUG] API Gateway REST: path-based invocation (/restapis/{id}/{stage}/user_request/{path}) returns 404 After creating a REST API, adding a resource with a Lambda proxy integration, and deploying to a stage, invoking via the documented path format returns "Invalid API id specified" (404).
Reproduction
bash# Create Lambda function aws --endpoint-url=http://localhost:4566 lambda create-function \ --function-name test-fn --runtime nodejs22.x --handler index.handler \ --role arn:aws:iam::000000000000:role/r \ --code S3Bucket=...,S3Key=... # Create REST API + resource + integration + deployment API_ID=$(aws --endpoint-url=http://localhost:4566 apigateway create-rest-api --name test | jq -r '.id') ROOT_ID=$(aws --endpoint-url=http://localhost:4566 apigateway get-resources --rest-api-id $API_ID | jq -r '.items[0].id') RES_ID=$(aws --endpoint-url=http://localhost:4566 apigateway create-resource --rest-api-id $API_ID --parent-id $ROOT_ID --path-part hello | jq -r '.id') aws --endpoint-url=http://localhost:4566 apigateway put-method --rest-api-id $API_ID --resource-id $RES_ID --http-method GET --authorization-type NONE aws --endpoint-url=http://localhost:4566 apigateway put-integration --rest-api-id $API_ID --resource-id $RES_ID --http-method GET --type AWS_PROXY --integration-http-method POST --uri "arn:aws:apigateway:eu-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-2:000000000000:function:test-fn/invocations" aws --endpoint-url=http://localhost:4566 apigateway create-deployment --rest-api-id $API_ID --stage-name dev # Invoke via documented path curl http://localhost:4566/restapis/$API_ID/dev/_user_request_/hello - Assistant
I'll start by exploring the repository to understand its structure and locate relevant code for API Gateway REST. Let's first look at the root directory.
- Assistant
Let me explore the repository structure more broadly to understand the codebase.
- Assistant
This is a Java project (Maven). Let me explore the src directory.
- Assistant
This is a Java project. Let me explore the structure more to understand the API Gateway implementation.
- Assistant
The issue is about path-based invocation for API Gateway REST (
/restapis/{id}/{stage}/_user_request_/{path}). Let me look at theApiGatewayUserRequestController.javawhich seems relevant for handling user requests. - Assistant
Let me examine the execute controller and the service router to understand the routing:
- Assistant
Now let me check the AwsServiceRouter to see how routing works:
- Assistant
Now let me look at the
ApiGatewayController.javato understand the routing: - Assistant
Let me see more of the ApiGatewayController to understand its path patterns:
- Assistant
Let me search for the error message "Invalid API id specified" to find where it originates:
- Assistant
Let me look at the custom domain filter and the AwsExecute controller too:
- Assistant
Now let me check the
ApiGatewayAwsExecuteController.java: - Assistant
Now let me search for other filters that might be interfering:
- Assistant
Let me understand the flow. Let me look at the
ApiGatewayService.getRestApimethod: - Assistant
Let me look at the dispatch method and the getRestApi/getStage methods:
- Assistant
Now let me look at the
dispatchmethod more carefully, and also look at thegetStagemethod to understand the flow. - Assistant
Let me look at the dispatch method more carefully:
- Assistant
Let me look at the dispatch method starting from line 226:
- Assistant
Let me look at the RegionResolver and check if there's a filter that might be interfering:
- Assistant
Let me look at the
RegionResolverto understand how region resolution works: - Assistant
Let me check the
apiKeymethod and understand the store structure: - Assistant
Now I understand the basic structure. Let me try to build and run the project to understand the issue better. First, let me look at how tests are run:
- Assistant
Let me now look at how the
ApiGatewayServicebuilds its keys for the API store, since the error comes fromgetRestApifailing to find the API: - Assistant
Let me find the
apiKeymethod definition: - Assistant
Let me look at the
getStagemethod since the dispatch calls it: - Assistant
Let me look at the getStage method:
- Assistant
Let me look for existing tests for the user request path:
- Assistant
Let me look at the existing integration test for user requests:
- Assistant
Great, there's already an integration test that covers this exact scenario. Let me run it to see if it passes:
- Assistant
Let me run the existing test to understand the current behavior:
- Assistant
HOME is not set. Let me set it.
- Assistant
- Assistant
Now I need to understand the issue. The test passes, but the bug report fails. Let me look at the key difference - the test creates the API and invokes it without auth headers (default region), while the bug report creates via AWS CLI (with auth headers for
eu-west-2) and invokes via plain curl (no auth headers, default region). Let me verify the region resolution behavior: - Assistant
Let me look at how the
apiKeymethod works: - Assistant
Now I understand the issue. When the API is created via AWS CLI with
eu-west-2region, it's stored with keyeu-west-2::{apiId}. When a plaincurlrequest (without auth headers) hits the_user_request_path, the region resolves to the default region (us-east-1), causing the lookup to fail with "Invalid API id specified".The same issue would affect the
/execute-api/path as well.The fix should make the execute controllers look up the API across all regions when the initial region-specific lookup fails. Let me check how the storage works:
- Assistant
Let me look at the StorageBackend interface:
- Assistant
Now I understand the issue. When the user creates an API via AWS CLI with a specific region (
eu-west-2), it's stored with the keyeu-west-2::{apiId}. When invoking via plaincurl(no Authorization header), theresolveRegion()returns the default region (us-east-1), which doesn't match. The API can't be found → "Invalid API id specified".The fix should be: in the execute/dispatch path, if the API isn't found with the resolved region, try to find it across all regions (since the API ID uniquely identifies it). Let me also verify by looking at how the
getRestApismethod scans: - Assistant
Wait, let me reconsider. The test creates the API and invokes it without AWS auth headers. But the issue says the user creates the API via AWS CLI (with auth headers from eu-west-2) and invokes with plain curl. Let me think about what the "right" fix is here.
Actually, let me re-read the issue. The user is using Floci (local emulator), so they might be using the region
eu-west-2when creating via CLI, but then curl with no auth headers would use the default region (us-east-1).The core issue is that the execute path (both
/restapis/.../_user_request_/and/execute-api/...) should be able to find APIs regardless of region, since the API ID is globally unique in a local emulator. Or at least, it should try all regions.Let me implement a fix: add a method to
ApiGatewayServicethat finds an API by ID across all regions, and use it in the dispatch path as a fallback.Now I understand the issue clearly. Let me verify my hypothesis by examining the dispatch method more carefully and then implement the fix:
- Assistant
Now let me understand the full picture. The
getRestApi(region, apiId)uses the keyregion + "::" + apiId. When invoked viacurl(no auth headers), the region resolves to the default region, but the API might have been created in a different region via the AWS CLI.Let me write a fix. The approach is to add a method to
ApiGatewayServicethat can find an API by ID across all regions, and use it in the dispatch method as a fallback: - Assistant
Let me look at the V2 service getApi method:
- Assistant
Now I understand the full picture. The issue is that when a request comes in via the
/restapis/{apiId}/{stageName}/_user_request_/{path}path without proper AWS auth headers (like a plaincurl), the region resolves to the default region, but the API was created via AWS CLI with a specific region (e.g.,eu-west-2). The key mismatch causes "Invalid API id specified".The fix should add region-agnostic API lookup for the execute paths. Let me implement it:
- Assistant
Now let me add a region-agnostic lookup method to
ApiGatewayService: - Assistant
Now I understand the full issue. When a request is made via
curl(no auth headers), theregionResolver.resolveRegion(headers)returns the default region (typicallyus-east-1). But if the API was created via AWS CLI with a different region (likeeu-west-2), the API is stored undereu-west-2::{apiId}and the lookup fails.The fix should be: in the
dispatchmethod, if the region-specific lookup fails, try to find the API across all regions (since API IDs are unique). Let me implement this.First, let me add a region-agnostic lookup method to
ApiGatewayService: - Assistant
Now I need to add a method to find an API by ID regardless of region. Let me also check if there's an equivalent for V2 APIs:
- Assistant
Now I understand the issue clearly. The problem is in the
dispatchmethod: when invoked viacurl(no auth headers), the region resolves to the default (us-east-1), but the API was stored under a different region (eu-west-2). Let me implement the fix. I'll add a method toApiGatewayServiceto find an API by ID across all regions, and use it as a fallback in the dispatch: - Assistant
Now let me add a region-agnostic lookup method to
ApiGatewayService. The method should find a REST API by ID regardless of the region: - Assistant
I see there's a duplicate
getRestApismethod. Let me fix that: - Assistant
Now I need to check if
Optionalis already imported inApiGatewayService.java: - Assistant
I need to add the
Optionalimport: - Assistant
Good. Now I need to modify the
dispatchmethod inApiGatewayExecuteControllerto use the fallback region-agnostic lookup. Let me update the dispatch method: - Assistant
Let me look at the full dispatch method again, and also at how the V2 dispatch resolves region. I need to modify both to fall back to region-agnostic lookup.
- Assistant
Now I'll modify the dispatch method to try region-agnostic lookup when the initial region-specific lookup fails:
- Assistant
Now I need to add a
findApiByIdmethod toApiGatewayV2Serviceand check ifRestApihas agetRegion()method: - Assistant
Let me look at the RestApi model:
- Assistant
The
RestApimodel doesn't have aregionfield. The region is only in the storage key. I need a different approach. Let me modifyfindRestApiByIdto return the region along with the API: - Assistant
I need a different approach since
RestApidoesn't have a region field. I'll changefindRestApiByIdto return the region too. Let me use aMap.Entry: - Assistant
It seems the edit was applied to the wrong file! Let me check the current state:
- Assistant
The file open seems to be wrong. Let me check what file is actually open and what happened.
- Assistant
The edit seems to have been applied to the wrong file. Let me check what happened more carefully:
- Assistant
I see there's a
findRestApiByIdincorrectly added to the ExecuteController. Let me fix this - the method belongs only in ApiGatewayService. Let me check and fix the ExecuteController: - Assistant
The edit got applied to the wrong file. Let me revert the ExecuteController and fix this properly. First, let me restore the
handlePutmethod: - Assistant
Good. Now let me check the current state of
ApiGatewayService.findRestApiById- I need to change it to return the region too: - Assistant
The
findRestApiByIdreturnsOptional<RestApi>butRestApidoesn't have agetRegion(). I need to update it to also return the region. Let me change it to return aMap.Entry<String, RestApi>: - Assistant
It seems the edits keep going to the wrong file. Let me check the actual content of both files carefully:
- Assistant
The ExecuteController file is badly messed up. Let me reset and start fresh:
- Assistant
Let me also restore the Service file since my earlier edit was wrong too:
- Assistant
Good, both files are restored. Now let me carefully implement the fix step by step. First, let me add the
findRestApiByIdmethod toApiGatewayService.java: - Assistant
Now let me carefully make the changes using sed to avoid the edit tool issues. First, let me add the
findRestApiByIdmethod toApiGatewayService.java: - Assistant
Let me try using python instead of python3.
- Assistant
Let me use sed instead:
- Assistant
Let me verify the change was applied correctly:
- Assistant
The method was added correctly. Let me verify the imports:
- Assistant
Good. Now let me add a similar
findApiByIdmethod toApiGatewayV2Service.java. Let me first check its structure: - Assistant
Good. Now I need to add a
findApiByIdmethod toApiGatewayV2Service. Let me check if it needs an Optional import: - Assistant
Let me check what the
Apimodel looks like: - Assistant
Good. Same issue with
Api- nogetRegion(). Let me check theapiKeymethod in V2: - Assistant
Good. Now let me add a
findApiByIdmethod to the V2 service: - Assistant
Let me verify the V2 change:
- Assistant
Good. Now I need to modify the
dispatchmethod inApiGatewayExecuteController.java. Let me also verify the imports in V2: - Assistant
Good. Now let me modify the
dispatchmethod inApiGatewayExecuteController.java. Let me open it: - Assistant
Now I need to replace the dispatch method. Let me use a careful sed approach:
- Assistant
Let me verify the dispatch method was updated correctly:
- Assistant
The dispatch method looks correct. Now I need to check if
dispatchV2also has the same region issue. Let me look at it: - Assistant
Let me look at the
dispatchV2method: - Assistant
Good. The
dispatchV2receives theregionparameter from the caller, so our fix in the dispatch method will properly propagate the resolved region. Now let me try to compile and run the tests: - Assistant
The code compiles. Now let me run the existing test to make sure it still passes:
- Assistant
The existing test passes. Now let me run all API Gateway tests to make sure nothing is broken:
- Assistant
Some tests are failing. Let me check if these are pre-existing failures or caused by my changes. Let me first check if these failures existed before my changes:
- Assistant
Good - these failures are pre-existing (not caused by my changes). Let me restore my changes:
- Assistant
Let me now run the tests that are most relevant - the main API Gateway tests, execute tests, and user request tests, excluding the ones with pre-existing failures:
- Assistant
All 103 tests pass. Now let me also run the other API Gateway tests that had pre-existing failures, but let me first confirm the pre-existing failures match:
- Assistant
Good. Now let me create a test to verify that the cross-region scenario actually works. Let me create a test script:
- Assistant
I notice the instructions say I should NOT add new tests to the repository. Let me create a temporary test script instead and run it to verify the cross-region scenario:
- Assistant
Let me remove the test file I accidentally created and instead write a temporary test script to verify the cross-region scenario:
- Assistant
Now let me create a proper temporary test to verify the cross-region scenario:
- Assistant
Now let me run this test to see if the cross-region scenario works:
- Assistant
The cross-region test passes perfectly. The API is created in
eu-west-2and invoked without auth headers (defaulting tous-east-1), and the cross-region fallback works correctly.Now let me clean up the temporary test file and review all changes:
- Assistant
Let me review all the changes:
- Assistant
The diff looks clean and correct. Let me run the full set of API Gateway tests (excluding the pre-existing failures) one more time to make sure nothing is broken:
- Assistant
All 133 tests pass. Let me now clean up any temporary files and run the existing tests one more time:
- Assistant
Let me verify the final diff one more time:
- Assistant
Good. Let me verify no stray test files are left:
- Assistant
Good, no stray test files. Let me do one final compilation check and then submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.