- Newest
- Most votes
- Most comments
I have managed to figure this out myself (stupid mistake):
I had configured the Endpoint URL field in the Integration request settings incorrectly. I had specified just the domain (http://<EC2 public IP>:8000), incorrectly thinking that the additional URL path (http://<EC2 public IP>:8000/models/list) was appended according to the Methods and Resources setup in API Gateway. API Gateway does append those to the Method Request URL (https://<api_id>.execute-api.ap-southeast-2.amazonaws.com/<stage>/models/list), but not to the Endpoint URL.
A basic beginner mistake, but hopefully this helps someone else out!
The issue you're experiencing, where API Gateway calls return HTML instead of the expected JSON while direct calls to the EC2 instance work correctly, could be caused by several factors:
-
API Gateway Configuration: Ensure that your API Gateway is correctly set up to proxy requests to your EC2 instance. Check that the integration type is set to HTTP Proxy and that the endpoint URL is correct.
-
CORS (Cross-Origin Resource Sharing): If your frontend application is hosted on a different domain than your API, you may need to configure CORS settings in both API Gateway and your FastAPI application. Make sure that the appropriate CORS headers are being set, including 'Access-Control-Allow-Origin'.
-
VPC Link and Network Load Balancer: Since you mentioned setting up a VPC Link and NLB, verify that these are configured correctly. Ensure that the security groups and network ACLs allow traffic between API Gateway, the NLB, and your EC2 instance.
-
FastAPI Configuration: Check your FastAPI application code to ensure it's not inadvertently returning HTML instead of JSON under certain conditions. Look for any error handling or middleware that might be intercepting the request and returning HTML.
-
API Gateway Stage Settings: Verify that your API Gateway stage is deployed correctly and that any stage variables or settings are properly configured.
-
CloudWatch Logs: Enable detailed CloudWatch logging for your API Gateway and examine the logs for any error messages or unexpected behavior.
-
Network Path: Ensure that the network path from API Gateway to your EC2 instance is clear and that all necessary ports are open.
-
HTTP vs HTTPS: Check if there's any mismatch in the protocol (HTTP/HTTPS) being used between API Gateway and your EC2 instance.
-
Response Transformation: If you're using any response transformation in API Gateway, ensure it's not inadvertently converting your JSON to HTML.
-
Timeout Settings: Check if there are any timeout issues. If your backend function isn't completing, it might be hitting a timeout limit in API Gateway or the NLB.
To troubleshoot:
- Test your API directly from the API Gateway console to isolate if the issue is with the Gateway or your client application.
- Use tools like Postman to make requests to both your EC2 instance directly and through API Gateway, comparing the responses.
- Enable and review CloudWatch logs for both API Gateway and your EC2 instance to identify where the process is breaking down.
- Temporarily simplify your API to return a basic JSON response to rule out issues with database interactions or complex processing.
By systematically checking these areas, you should be able to identify the root cause of the discrepancy between direct calls and those routed through API Gateway.
Sources
What causes the API response differences between my local setup and EC2, despite using the same database URL? | AWS re:Post
AWS API gateway routing | AWS re:Post
Cors policy with cloudfront + Api Gateway (VPC Links) | AWS re:Post
CORS Access-Control-Allow-Origin Header missing - Api Gateway HTTP / EC2 | AWS re:Post