Fixing the AWS API Gateway 504 Gateway Timeout Limit (29-Second Hard Limit)
Resolving AWS API Gateway 504 Gateway Timeout errors. Learn why the 29-second hard limit exists, how integration timeouts work, and architectural patterns to fi
- AWS API Gateway has a hard, unchangeable 29-second integration timeout for REST APIs (30 seconds for HTTP APIs).
- A 504 Gateway Timeout error occurs when your backend (Lambda, EC2, ECS) fails to respond within this limit.
- AWS Support cannot increase this timeout limit; it is a fundamental architectural constraint of the service.
- To resolve long-running request timeouts, you must transition to an asynchronous architecture using SQS, Step Functions, or Webhooks.
- If your backend only occasionally breaches the limit, optimizing database queries, adding caching, or increasing Lambda memory can provide a quick fix.
| Method | When to Use | Implementation Time | Risk / Complexity |
|---|---|---|---|
| Optimize Backend (DB/Code) | Requests usually take 10-20s but occasionally spike over 29s. | Hours to Days | Low |
| Async API with SQS/EventBridge | Workloads inherently take minutes or hours (e.g., video processing, massive reports). | Days to Weeks | High |
| Migrate to ALB (Application Load Balancer) | Legacy applications that strictly require synchronous long-lived HTTP connections (up to 4000s timeout). | Days | Medium |
| WebSocket APIs | Clients need real-time progress updates for long-running background tasks. | Weeks | High |
Understanding the AWS API Gateway Timeout Limit
When building serverless applications on AWS, one of the most common and frustrating roadblocks developers hit is the AWS API Gateway timeout limit. If you are seeing HTTP 504 Gateway Time-out responses or errors in CloudWatch stating Execution failed due to a timeout error, you have collided with a fundamental constraint of the AWS ecosystem.
By design, the AWS API Gateway integration timeout is capped at 29 seconds for REST APIs and a maximum 30 second timeout for HTTP APIs. This limit governs the maximum amount of time API Gateway will wait for a backend integration—whether that is an AWS Lambda function, an EC2 instance, an ECS container, or an external HTTP endpoint—to return a response.
The Immutable 29-Second Hard Limit
A frequent question in DevOps communities is: "How do I increase the AWS API Gateway timeout limit?"
The hard truth is: You cannot.
Unlike many AWS quotas that can be raised by submitting a support ticket, the 29-second timeout limit is a hard quota. AWS enforces this to ensure API Gateway remains a high-performance, synchronous routing layer rather than a long-polling connection manager.
The Lambda Timeout Discrepancy
Developers often get confused by the AWS API Gateway Lambda timeout discrepancy. AWS Lambda functions can run for up to 15 minutes. However, if API Gateway triggers a Lambda function synchronously, API Gateway will drop the connection and return a 504 Gateway Timeout to the client after 29 seconds, even if the Lambda function continues to run successfully in the background.
This results in a scenario where the client thinks the request failed, but the backend process eventually completes. This can lead to dangerous retry loops, duplicate database entries, and severe data inconsistency if not handled correctly.
Step 1: Diagnosing the Timeout Error
Before ripping apart your architecture, you must confirm that the API Gateway timeout setting is actually the culprit, rather than a networking failure or an explicit error thrown by your backend.
Inspecting CloudWatch Logs
Enable Execution Logging for your API Gateway stage. If you hit the 29-second limit, you will see a log entry similar to this:
(a1b2c3d4-5678-90ab-cdef-1234567890ab) Endpoint request timed out
(a1b2c3d4-5678-90ab-cdef-1234567890ab) Execution failed due to a timeout error
(a1b2c3d4-5678-90ab-cdef-1234567890ab) Method completed with status: 504
If you see Endpoint request timed out well before 29 seconds (e.g., at 3 seconds), it means your AWS API Gateway default timeout or custom integration timeout setting was manually lowered. You can verify and fix this in the API Gateway Console under your specific Resource > Integration Request > Timeout setting.
Step 2: Immediate Fixes (Optimization)
If your API normally responds in 5-10 seconds but occasionally spikes past 29 seconds, you may not need a full architectural rewrite. Focus on performance optimization:
- Increase Lambda Memory (and CPU): In AWS Lambda, CPU power scales linearly with allocated memory. Increasing a Lambda function from 512MB to 2048MB can drastically cut execution time, potentially bringing a 35-second process down to 10 seconds, safely under the API Gateway limit.
- Database Query Optimization: Analyze your database layer. Are you missing an index? Is an RDS instance suffering from CPU credit exhaustion? Slow database queries are the #1 cause of API Gateway 504 errors.
- Implement Caching: If the request involves fetching data that changes infrequently, enable API Gateway Caching or use an intermediate layer like Amazon ElastiCache (Redis) to serve responses in milliseconds.
- Connection Re-use: If using Node.js or Python in Lambda, ensure you are persisting database connections outside the function handler to avoid the overhead of establishing a new TCP/TLS connection on every invocation.
Step 3: Architectural Fixes (Asynchronous Patterns)
If your workload fundamentally takes longer than 29 seconds—such as generating a complex PDF report, processing a video file, or querying a massive data warehouse—you must change your API design from a Synchronous pattern to an Asynchronous pattern.
The SQS / EventBridge Decoupling Pattern
Instead of making the client wait for the final result, API Gateway should instantly place the request into a queue and return a 202 Accepted status to the client, along with a Job ID.
The Flow:
- Client sends an HTTP POST request to API Gateway.
- API Gateway uses a native AWS Service Integration (no Lambda required!) to push the payload directly into an Amazon SQS queue or Amazon EventBridge bus.
- API Gateway immediately responds to the client with an HTTP
202 Acceptedand a uniquejob_id. - A backend Lambda Function (or ECS task) picks up the message from SQS and processes the heavy workload over the next 5, 10, or 15 minutes.
- Once finished, the backend worker writes the result to a database (like DynamoDB) or an S3 bucket.
Handling the Client Experience
Because the API Gateway drops the connection immediately, the client application needs a way to get the final result. There are two standard approaches:
1. The Polling Pattern (Easiest)
The client uses the job_id to make periodic HTTP GET requests to a status endpoint (e.g., /jobs/{job_id}/status). This endpoint quickly queries DynamoDB. If the job is still running, it returns {"status": "processing"}. If finished, it returns the final data or a presigned S3 URL.
2. The Webhook / WebSocket Pattern (Advanced)
Instead of forcing the client to poll, the backend pushes the result to the client when it's ready. You can use AWS API Gateway WebSocket APIs (which allow idle connections for up to 2 hours) or AWS IoT Core to push messages directly to a frontend React/Vue application. Alternatively, for B2B APIs, you can require the caller to provide a callback_url in their initial request, and your backend will make an HTTP POST to that URL once the job completes.
What if I absolutely need a synchronous response over 29 seconds?
If you have a legacy application that strictly expects an HTTP request to stay open for 5 minutes and wait for a response, API Gateway is the wrong service. You must migrate away from API Gateway and place an Application Load Balancer (ALB) in front of your ECS containers or EC2 instances. ALBs support idle timeouts of up to 4000 seconds (over an hour). Note that ALBs can also trigger Lambda functions, but the Lambda 15-minute maximum execution limit still applies.
Frequently Asked Questions
# Use the AWS CLI to query CloudWatch Logs Insights
# This query finds all API Gateway requests that resulted in a 504 Gateway Timeout
# allowing you to identify which endpoints are hitting the 29-second limit.
aws logs start-query \
--log-group-name "API-Gateway-Execution-Logs_api-id/stage-name" \
--start-time $(date -v-7d +%s) \
--end-time $(date +%s) \
--query-string '
fields @timestamp, @message
| filter status == "504"
| parse @message "Execution failed due to a timeout error" as timeout_error
| display @timestamp, httpMethod, resourcePath, status
| sort @timestamp desc
| limit 50
'Error Medic Editorial
Our editorial team consists of AWS Certified Solutions Architects and Senior DevOps Engineers dedicated to solving complex cloud infrastructure challenges and documenting reliable, production-ready workarounds.