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 structure to understand the codebase and locate relevant files. Let's first check the root directory.
- Assistant
Let me explore the repository structure more to understand how it's organized.
- Assistant
This is a Java project using Maven. Let me explore the API Gateway related files.
- Assistant
Let me look at the key files related to the issue. The bug is about path-based invocation (
/restapis/{id}/{stage}/_user_request_/{path}) returning 404. Let me look at the relevant controller files. - Assistant
This controller looks like it should handle the path. Let me look at the
ApiGatewayExecuteControllerto understand how it dispatches requests. - Assistant
The issue is about invoking REST APIs via the path format
/restapis/{apiId}/{stageName}/_user_request_/{path}. Let me look at the key method -dispatch()- which is called by both theApiGatewayUserRequestControllerandApiGatewayExecuteController.Looking at the
ApiGatewayUserRequestController, it delegates toexecuteController.dispatch(). TheApiGatewayExecuteControllerhas thedispatchmethod that handles the request.The issue says it returns "Invalid API id specified" which would be thrown in the
dispatchmethod when eithergetRestApiorgetStagefails. Let me look more closely at the dispatch method to understand the flow.Let me see the full dispatch method.
- Assistant
Let me look at the dispatch method around line 226 to understand the logic.
- Assistant
Let me look at the
AwsServiceRouterto understand how requests are routed: - Assistant
Now let me look at the
ApiGatewayController(the management plane): - Assistant
I can see the
ApiGatewayControlleris mapped to@Path("/")and handles REST API management endpoints. The issue is that when a request comes in for/restapis/{apiId}/{stageName}/_user_request_/{proxy}, it might be matching the wrong controller or not being matched at all.Looking at the
ApiGatewayController:@Path("/")- handles root path- It has specific methods like
@Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}")
The
ApiGatewayUserRequestController:@Path("/restapis/{apiId}/{stageName}/_user_request_")- handles the user request path- Methods like
@Path("/{proxy: .*}")- handle the proxy path
The problem might be that the
ApiGatewayControllerat@Path("/")is intercepting the request before it reaches theApiGatewayUserRequestController.Let me check the
ApiGatewayControllerto see if it has a catch-all path that would intercept/restapis/{apiId}/{stageName}/_user_request_/{proxy}.Looking at the
ApiGatewayController, I need to see all its path annotations to understand if it has something that would match the user request path. Let me search for all annotations in the controller. - Assistant
Now let me look at the
ApiGatewayServiceto see howgetRestApiandgetStagework: - Assistant
Now I need to understand the region resolution. Let me check the
RegionResolver: - Assistant
Let me look at the
ApiGatewayCustomDomainFilter.javato understand how requests are routed: - Assistant
Now I understand the region resolution. Let me check the
EmulatorConfig: - Assistant
Now I understand the potential issue. Let me look for existing tests related to this functionality:
- Assistant
Let me look at the test file to understand how the integration is being tested.
- Assistant
Let me first check if this test passes to see if there's actually a routing issue:
- Assistant
Let me set the HOME environment variable and try again.
- Assistant
The test passes because both creation and invocation use the same default region (us-east-1, no auth headers). But in the issue's reproduction, the AWS CLI sends
--region eu-west-2, which means the API gets stored with regioneu-west-2. When the curl invocation happens (no auth header), the region resolves to the defaultus-east-1, causing a lookup miss.Let me verify this theory by looking at the
getRestApimethod and how we might fix it: - Assistant
Let me check git log to see when the
ApiGatewayUserRequestControllerwas added: - Assistant
Let me check git log for the
ApiGatewayUserRequestControllerto see when it was added: - Assistant
Let me look at the actual execute-api endpoint to see how it differs:
- Assistant
Let me understand the issue better. Let me look at how the API is stored and looked up, and see if there's a region mismatch issue:
- Assistant
Now I understand the issue. The problem is a region mismatch:
- When the API is created via AWS CLI with
--region eu-west-2, theRegionResolverextractseu-west-2from the Authorization header and stores the API with keyeu-west-2::<apiId>. - When invoking via
curl http://localhost:4566/restapis/$API_ID/dev/_user_request_/hello, there's no Authorization header, so the region falls back to the defaultus-east-1, andgetRestApi("us-east-1", apiId)fails with "Invalid API id specified".
The fix: In the
dispatchmethod, when the API is not found in the resolved region, try to find it in any region (since API IDs are globally unique in the emulator).Let me implement this:
- When the API is created via AWS CLI with
- Assistant
Now I need to add a region-agnostic lookup method. Let me first check the
StorageBackendinterface: - Assistant
Now I understand the issue clearly. Let me implement the fix. The problem is in the
dispatchmethod ofApiGatewayExecuteController. When the API is not found in the resolved region (because curl doesn't send Authorization headers), we need to fall back to finding the API by ID across all regions.Let me first add a method to
ApiGatewayServicethat can find a REST API by ID regardless of region: - Assistant
Now let me look more carefully at how the
dispatchmethod inApiGatewayExecuteControllerworks and what the fix should be: - Assistant
Now I understand the full picture. The issue is:
- The
ApiGatewayUserRequestControllerdelegates toApiGatewayExecuteController.dispatch() - The
dispatchmethod resolves the region from the Authorization header - When the request comes via
curl(no Authorization header), the region defaults tous-east-1 - If the API was created with a different region (e.g.,
eu-west-2), the lookup fails
The fix should be to add a region-agnostic fallback in the
dispatchmethod. When the API is not found in the resolved region, try to find it across all regions.Let me implement this fix in the
dispatchmethod. - The
- Assistant
Now I understand the issue clearly. The problem is in the
dispatchmethod ofApiGatewayExecuteController(and similarly the v2 dispatch). When the API is created with a non-default region (e.g.,eu-west-2via AWS CLI), it's stored with keyeu-west-2::apiId. But when invoked viacurl(no auth header), the region defaults tous-east-1, causing the lookup to fail.The fix: Add a region-agnostic lookup method to
ApiGatewayServiceand use it as a fallback in the dispatch method. Let me implement this: - Assistant
Now let me add a method to find a REST API by ID regardless of region, and also fix the region resolution for the execute endpoints. Let me first add the helper method to
ApiGatewayService: - Assistant
Wait, I need a more comprehensive fix. The region used for the API lookup is also used for
getStage,getResources, etc. I need to resolve the actual region when the initial lookup fails. Let me revise the approach: - Assistant
Now let me add this method to the
ApiGatewayServiceclass. I need to insert it after thegetRestApimethod: - Assistant
Now let me add the
resolveRestApimethod toApiGatewayService. I'll insert it after thegetRestApimethod: - Assistant
Let me use the
editcommand to add the new method: - Assistant
Now let me add the
resolveRestApimethod after thegetRestApimethod. I'll use the edit command. - Assistant
I see there's a duplicate line. Let me fix that:
- Assistant
Good. Now I need to make sure the
Optionalimport exists. Let me check: - Assistant
I need to add the
Optionalimport: - Assistant
I see there are duplicate import lines. Let me fix the imports by removing the duplicates:
- Assistant
Good, now the imports are clean. Now I need to modify the
dispatchmethod inApiGatewayExecuteControllerto useresolveRestApiinstead ofgetRestApi, and use the resolved region for all subsequent calls. Let me look at the dispatch method: - Assistant
Now I need to modify the
dispatchmethod to useresolveRestApiinstead of the separategetRestApicall, and use the resolved region for all subsequent calls. Let me update the dispatch method: - Assistant
Now I also need to handle the v2 API lookup similarly. Let me check the
dispatchV2method: - Assistant
I also need to handle the v2 API lookup similarly. Let me check how
apiGatewayV2Service.getApiworks: - Assistant
The V2 lookup also has the same issue, but let me focus on the v1 REST API case which is the reported issue. Let me also check if the
dispatchV2path in the v2 check needs similar treatment: - Assistant
I also need to handle the V2 API check in the
dispatchmethod. If a V2 API was created with a different region, the current check would fail. But the reported issue is specifically about V1 REST APIs. Let me first also add region-agnostic lookup to the V2 check in the dispatch method: - Assistant
Now I need to also fix the v2 API detection to be region-agnostic. Let me add a similar
resolveApimethod toApiGatewayV2Service: - Assistant
Let me check the imports for the V2 service:
- Assistant
Now let me add a
resolveApimethod toApiGatewayV2Service: - Assistant
I see there are duplicate imports. Let me fix those:
- Assistant
Good. Now let me add the
resolveApimethod toApiGatewayV2Service: - Assistant
Now let me add the
resolveApimethod toApiGatewayV2Serviceafter thegetApimethod: - Assistant
Now let me add the
resolveApimethod aftergetApiinApiGatewayV2Service. I also need to add aresolvedApimethod that can be used in the dispatch method to get both the resolved region and the API. - Assistant
I see the edit created some duplicates and the
getApimethod body was lost. Let me fix these issues: - Assistant
Now I need to update the v2 API check in the
dispatchmethod to also use the region-agnostic fallback: - Assistant
Now I need to update the v2 API check to use the
findApimethod with region-agnostic fallback. If the API is found but in a different region, I need to use the correct region. - Assistant
Now I also need to update the
dispatchV2call to use the resolved region from the v2 API lookup. Let me also fix thefindApimethod to return the resolved region: - Assistant
Good. Now I need to update the
dispatchmethod inApiGatewayExecuteControllerto useresolveApiinstead offindApiand handle the resolved region properly. Let me update the dispatch method. - Assistant
Now I need to update the v2 detection to use
resolveApiand pass the resolved region todispatchV2: - Assistant
Good. Now let me compile and run the tests to make sure everything works:
- Assistant
Good, it compiles. Now let me run the existing tests to make sure I didn't break anything:
- Assistant
Existing test passes. Now let me run all the API Gateway tests to make sure nothing is broken:
- Assistant
The pattern is wrong. Let me try with the correct test pattern.
- Assistant
Let me check if these test failures are pre-existing by reverting and running the same tests:
- Assistant
Good, the same 11 test failures exist without my changes. These are pre-existing failures (likely Docker-related Lambda tests that don't work in this environment). Let me restore my changes.
- Assistant
Let me now run the non-authorizer API Gateway tests to confirm my changes don't break anything:
- Assistant
All 100 non-authorizer API Gateway tests pass. Now let me also check the
ApiGatewayAwsExecuteControllerwhich might have the same issue: - Assistant
Good, the
ApiGatewayAwsExecuteControlleralso delegates toexecuteController.dispatch(), so it benefits from the same fix. All three execute endpoints share the same dispatch method.Let me now also look at the stage key to make sure there aren't similar issues:
- Assistant
Let me also look at the
getStagemethod: - Assistant
Now let me verify that the fix is complete. Let me review all the changes I made:
- Assistant
The diff looks good. Now let me also check the
application.yml.bakfile mentioned in the test log, since it's flagged as a warning: - Assistant
Let me verify the fix is complete and correct. Let me run all the relevant API Gateway tests one more time to make sure everything passes:
- Assistant
All 100 tests pass. Let me also run the API Gateway v2 tests:
- Assistant
Let me check if these test failures are also pre-existing:
- Assistant
Good, these are also pre-existing failures. Let me restore my changes.
- Assistant
Good. All pre-existing failures. Let me also verify that the old
findApimethod I replaced was not used elsewhere: - Assistant
Good, neither
findApinorfindRestApiare used elsewhere. Let me also verify there are no compile errors with thevarkeyword usage and ensure everything is clean: - Assistant
Everything compiles. Now let me write a temporary test to verify that the fix actually resolves the issue. Let me create a test that simulates the exact scenario from the issue:
- Assistant
The test passes! The API was created in
eu-west-2and invoked without auth headers (defaulting tous-east-1), and it works correctly. Let me clean up the temporary test file: - Assistant
Let me verify the temporary test file is removed and do a final check:
- Assistant
Good. Let me do one final verification - run all non-failing API Gateway tests:
- Assistant
All 140 tests pass. Let me review the final state of all changed files:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.