How do I troubleshoot HTTP 504 errors from my API Gateway REST API?
Last updated: 2021-02-23
I get an HTTP 504 status code when I invoke my REST API using Amazon API Gateway. How do I troubleshoot the error?
If an integration request takes longer than your API Gateway REST API maximum integration timeout parameter, API Gateway returns an HTTP 504 status code.
To troubleshoot 504 errors from API Gateway, first identify and verify the source of the error in your Amazon CloudWatch execution logs. Then, use one or more of the following methods to reduce the runtime of your integration requests until they don't timeout.
To identify and verify the source of the 504 error in your Amazon CloudWatch logs
1. Configure API Gateway logging for the API returning the 504 errors.
2. Manually reproduce the 504 error.
4. Track the request ID in your CloudWatch logs. If there are timeouts on the integration, you will see an Execution failed due to a timeout error message after the "Endpoint request body after transformations:" line. For more information, see How do I find API Gateway REST API errors in my CloudWatch logs?
5. Determine the source of the error by doing the following:
Verify that the associated integration endpoint was invoked.
Confirm how long the integration logic took to complete and respond to API Gateway.
6. If the integration wasn't invoked, retry the request. (The error might have resulted from a temporary network failure in the API Gateway service.)
Note: Make sure that your application is idempotent. This avoids data conflicts when you retry the API request.
If the integration was invoked but returned a 504 error message, try either increasing your maximum integration request timeout parameter or reducing your integration's runtime.
Note: API Gateway's default maximum integration timeout is 30 seconds.
To reduce your integration's runtime
- Make sure that your backend integration includes only the logic needed for API Gateway to send an HTTP response to the client. Consider moving any nondependent or post-processing logic to another service, such as AWS Lambda.
- If network latencies are causing the 504 error, implement retry logic on the client-side application.
- Improve the backend integration by following the optimization best practices for your platform.
If none of these actions achieve your desired response speeds, consider implementing asynchronous API Gateway integration calls.