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
# 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_/helloActual behavior
HTTP 404:
{"message":"Invalid API id specified"}Expected behavior
Request routes through API Gateway to the Lambda function and returns the Lambda response.
Notes
- The API is created successfully (confirmed via get-rest-api and Floci logs)
- The deployment succeeds
- Management plane operations (create/get/update APIs) all work correctly
- Only the data plane invocation path fails
- Tested with Floci 1.5.16
Environment
- Floci 1.5.16 (docker.io/floci/floci:latest)
- Podman 5.8.0 rootless (also likely reproducible with Docker)
- AWS CLI v2
- Region: eu-west-2
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. - Assistant
I'll start by exploring the repository structure to understand the codebase. Let me first see what's in the current directory.
- Assistant
Let me look at the directory structure more broadly.
- Assistant
This is a Java project using Maven. Let me explore the source code structure.
- Assistant
Let me look at the API Gateway related files specifically.
- Assistant
The issue is about path-based invocation via
/restapis/{id}/{stage}/_user_request_/{path}returning 404. Let me look at the relevant controller files, specifically theApiGatewayUserRequestController.javaandApiGatewayExecuteController.java. - Assistant
Now let me look at the
ApiGatewayExecuteController.javato understand the dispatch logic. - Assistant
Let me look at the dispatch method more closely, particularly the part that checks the API:
- Assistant
Now let me look at the dispatch method from the ApiGatewayExecuteController. The error message is "Invalid API id specified", so let me search for that string.
- Assistant
The error comes from
ApiGatewayService.getRestApi(). Let me look at the dispatch method in ApiGatewayExecuteController to see what happens:- It checks if it's a v2 API by calling
apiGatewayV2Service.getApi()- this would throw an exception (not a v2 API) - Then it calls
apiGatewayService.getRestApi()- this is throwing the "Invalid API id specified" error
Let me look at how
getRestApiworks to understand why it's failing. - It checks if it's a v2 API by calling
- Assistant
Now let me look at the routing configuration to understand how paths are matched. Let me check the
ApiGatewayServiceto understand howgetRestApiworks: - Assistant
Now I need to understand the routing. The
/restapispath prefix is used for management plane AND user requests. Let me check if there's a conflict between the management plane controller and the user request controller. - Assistant
Let me look for GET patterns on the management controller that might intercept the
/restapis/{id}/{stage}/_user_request_/...path: - Assistant
Let me look at the
ApiGatewayV2Service.getApi()method: - Assistant
- Assistant
Now let me look at the existing user request test:
- Assistant
Now let me understand the full controller structure and how paths might conflict. Let me look at all
@Pathannotations in the management controller: - Assistant
OK, so I need to understand the routing. The management controller has
@Path("/")at the class level, and theApiGatewayUserRequestControllerhas@Path("/restapis/{apiId}/{stageName}/_user_request_")at the class level.The request path
/restapis/$API_ID/dev/_user_request_/helloshould be matched by the user request controller.Wait, I need to check whether there's a
GET /restapis/{apiId}endpoint that might match this. The request is GET/restapis/$API_ID/dev/_user_request_/hello.With JAX-RS, the
/restapis/{apiId}pattern in the management controller would try to match/{apiId}against$API_ID/dev/_user_request_/hello... No, that's not how it works. The path parameter{apiId}only matches a single path segment (not crossing/boundaries). So/restapis/{apiId}would only match/restapis/abc123but not/restapis/abc123/dev/_user_request_/hello.So the management controller shouldn't be intercepting this request. Let me think about what else could be going on.
Wait, could it be an issue with the
ApiGatewayCustomDomainFilter? Let me check that filter. - Assistant
Let me check the
AwsServiceRouterto see if it might interfere: - Assistant
Let me now try to run the existing test for the user request to see if it passes:
- Assistant
Now let me try to run the existing user request test to understand the current state:
- Assistant
Let me check the JAVA_HOME setting and the maven wrapper.
- Assistant
Let me check the HOME env and maven.
- Assistant
The existing test passes. Now let me think about the actual issue. The test passes because it creates and invokes the API in the same region (default region, no auth header).
In the user's reproduction scenario, the AWS CLI creates the API (with Authorization header containing region eu-west-2), but the curl invocation (no Authorization header) uses the default region. This could be a region mismatch issue.
But let me first try to reproduce the exact error by looking at the region resolution more carefully.
- Assistant
Let me look at the
apiKeymethod: - Assistant
Let me look at the key method:
- Assistant
Now I understand the issue! The keys are region-prefixed (
region::apiId). The API is stored in eu-west-2 but the curl request without an Authorization header defaults to us-east-1 (the default region). Let me verify this hypothesis and look at the dispatch method again. - Assistant
Now I understand the root cause. The
dispatchmethod resolves the region from theAuthorizationheader. But when a user invokes via curl without an Authorization header (as in the/restapis/{apiId}/{stageName}/_user_request_/{path}pattern), the region defaults to the default region (e.g.,us-east-1). If the API was created in a different region (e.g.,eu-west-2), the lookup fails with "Invalid API id specified".The fix should be to try to find the API across all regions when it's not found in the resolved region. Let me check the
ApiGatewayV2Service.getApi()to see if it has the same pattern: - Assistant
Now let me check if there's any guidance about how to fix this in AGENT.md:
- Assistant
Now I understand the issue. Let me trace through the flow. The
dispatchmethod resolves the region from theAuthorizationheader. When making acurlrequest without an Authorization header (as in the_user_request_invocation pattern), the region defaults to the configured default region (e.g.,us-east-1). But if the API was created via AWS CLI with regioneu-west-2, the lookup usesregion::apiIdas the key and fails.Let me verify by looking at how
resolveRegionworks when there's no Authorization header: - Assistant
Let me also check the application.yml for the default region configuration:
- Assistant
Let me find the floci configuration section:
- Assistant
Let me see the full floci config section:
- Assistant
Now I fully understand the problem. The default region is
us-east-1. If the user creates an API via AWS CLI with regioneu-west-2, the API is stored with keyeu-west-2::apiId. When the user invokes via curl (no Authorization header), the region is resolved tous-east-1, and the lookup forus-east-1::apiIdfails.The fix should make the dispatch method try to find the API across all regions when it's not found in the resolved region. Let me look at the storage backend to understand how to scan:
- Assistant
Now I understand the problem. The API is created in one region (e.g.,
eu-west-2via AWS CLI with an Authorization header), but thecurlinvocation via_user_request_path has no Authorization header, so it resolves to the default region (us-east-1). The lookup keyus-east-1::{apiId}doesn't match the stored keyeu-west-2::{apiId}.The fix is to make the data-plane dispatch (execute-api and user_request paths) region-agnostic by falling back to searching across all regions when the API is not found in the resolved region. Let me implement this.
- Assistant
Let me look at the apiStore type and available scanning methods:
- Assistant
Now let me understand the approach. I need to add a
findRestApi(String apiId)method toApiGatewayServicethat scans all regions, and modify thedispatchmethod to use it as a fallback. Let me also look at the V2 service: - Assistant
Now I have a clear picture. Let me implement the fix. I'll add methods to both
ApiGatewayServiceandApiGatewayV2Serviceto find APIs by ID across all regions, and modify thedispatchmethod to use them as fallbacks.Let me start with
ApiGatewayService: - Assistant
Let me look at the
getRestApismethod inApiGatewayServiceto understand how it scans for APIs: - Assistant
Now I have a clear understanding of the issue. The API is stored with a region-prefixed key (
region::apiId). When the API is created via AWS CLI with a region header but invoked viacurlwithout one, the region mismatch causes the lookup to fail.Let me implement the fix:
- Add
findRestApiById(String apiId)toApiGatewayServicethat scans all regions - Add
findApiById(String apiId)toApiGatewayV2Servicethat scans all regions - Modify the
dispatchmethod to try all regions as a fallback
Let me first look at how the storage API scan works:
- Add
- Assistant
Now let me add a method to
ApiGatewayServiceto find a REST API by ID across all regions: - Assistant
Let me look at the RestApi model to see if it stores the region:
- Assistant
Now I have a full picture. Let me implement the fix. The key insight is that the
dispatchmethod needs to try all regions when the API isn't found in the resolved region, because the data-plane path (like_user_request_orexecute-api) may be invoked without an Authorization header.Let me first add a
findRegionForApimethod toApiGatewayService: - Assistant
Now let me add the
findRegionForApimethod toApiGatewayService. I'll add it right after thegetRestApismethod. - Assistant
Now let me add the
findRegionForApimethod toApiGatewayService. I'll add it right after thegetRestApismethod. - Assistant
Let me use a different approach to insert the code:
- Assistant
Let me verify the insertion:
- Assistant
The indentation got messed up - the closing brace of
getRestApiswas absorbed. Let me fix this properly by first reverting and then doing it correctly: - Assistant
Let me use the open/edit approach:
- Assistant
Now let me insert the new method after line 197:
- Assistant
Let me use a Python script to insert the code:
- Assistant
Let me try with python:
- Assistant
Let me try using sed or perl instead.
- Assistant
Let me use a temporary file approach with sed:
- Assistant
Let me create a temporary file with the new method and use awk to insert it:
- Assistant
Now let me use awk to insert this after line 197:
- Assistant
Let me verify the insertion:
- Assistant
The method is inserted correctly. Now let me add a similar method to
ApiGatewayV2Service: - Assistant
Let me find the exact line number in the V2 service:
- Assistant
Let me see lines 120-126:
- Assistant
Good, the closing brace of
getApisis at line 123. Let me create the temp file for V2 and insert after line 123: - Assistant
Now let me insert the V2 method after line 123:
- Assistant
Let me verify the V2 insertion:
- Assistant
Both methods are inserted correctly. Now I need to modify the
dispatchmethod inApiGatewayExecuteControllerto use these fallback methods. Let me open it: - Assistant
Now I need to modify the
dispatchmethod to try all regions when the API is not found in the resolved region. The key change is to the region resolution and API lookup logic: - Assistant
Now let me replace lines 226-252 with the new code:
- Assistant
Let me verify the change:
- Assistant
Good, the change looks correct. But I notice a potential issue: if the API is found in a different region but the stage doesn't exist there, the
getStagecall in the catch block would throw anAwsExceptionthat propagates uncaught. Let me check how that's handled: - Assistant
Let me check the exception mapper:
- Assistant
Good, the
AwsExceptionMapperwill handle any uncaughtAwsExceptionfrom the fallbackgetStagecall. The logic looks correct:- If
getRestApifails (API not in resolved region), tryfindRegionForApi→ if found in a different region, updateregionand trygetStagein that region. - If
getRestApisucceeds butgetStagefails,findRegionForApireturns the same region, so we return the original error (stage not found). - If
findRegionForApireturns null, API doesn't exist in any region, so return the original error.
Now let me run the existing tests to make sure the changes compile and work:
- If
- Assistant
The existing test passes. Now let me run the broader API Gateway tests:
- Assistant
All 48 tests pass. Let me run all API Gateway tests:
- Assistant
The failing tests are all authorizer-related, not related to my changes. Let me verify by checking if these tests were already failing before my changes:
- Assistant
Good - 9 pre-existing failures. Let me restore my changes and check if the authorizer context test was also failing before:
- Assistant
These are all pre-existing failures (9 + 2 = 11, exactly matching the total). Let me restore my changes:
- Assistant
Good, the pre-existing failures (11) match exactly with the failures before my changes. My changes don't break anything.
Now let me also write a test script to verify the cross-region scenario actually works. Let me create a temporary test script:
- Assistant
All V2 tests pass too. Let me run the core API Gateway tests once more to confirm everything is good:
- Assistant
All 133 tests pass. Let me review the final diff to make sure everything looks correct:
- Assistant
The diff looks clean and correct. Let me verify the build compiles cleanly:
- Assistant
Compiles cleanly (only dependency warnings, not from our code). Let me now do a final comprehensive check by running the most relevant test - the user request integration test one more time:
- Assistant
All 44 tests pass. Let me now check if there are other places in the execute controller that might have the same issue (region mismatch for other operations):
- Assistant
Let me check the V2 dispatch path to make sure it also uses the region correctly:
- Assistant
Good, the V2 dispatch receives the
regionparameter, so it uses whatever region was resolved (including the cross-region fallback). The fix is complete.Let me now summarize the changes and submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.