How to Fix AWS API Gateway 504 Integration Timeout
In this tutorial, you'll learn about How to Fix AWS API Gateway 504 Integration Timeout. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Your API Gateway endpoint returns 504 Endpoint Request Timed Out — the integration (Lambda, HTTP, or VPC Link) did not respond within the timeout limit.
Step-by-Step Fix
1. Check integration timeout settings
aws apigateway get-integration --rest-api-id abc123 --resource-id def456 --http-method POST
Look for the timeoutInMillis field (default is 29000 ms, max is 29000 for Lambda):
{
"timeoutInMillis": 29000,
"type": "AWS_PROXY"
}
2. Increase API Gateway timeout (HTTP integration only)
aws apigateway update-integration --rest-api-id abc123 --resource-id def456 --http-method POST --patch-operations op=replace,path=/timeoutInMillis,value=29000
3. Optimize Lambda for faster response
import json
def lambda_handler(event, context):
result = quick_computation(event)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(result)
}
4. Use asynchronous integration
Configure the Lambda to return immediately and process in the background:
{
"type": "AWS",
"httpMethod": "POST",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:my-function/invocations",
"credentials": "arn:aws:iam::123456789012:role/APIGatewayLambdaRole",
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
}
}
5. Check VPC Link connectivity
aws apigatewayv2 get-vpc-link --vpc-link-id vpce-12345678
Common Mistakes
| Mistake | Fix |
|---|---|
| Lambda function timeout < 29s | Increase Lambda timeout in function config |
| VPC Link target unreachable | Check NLB target group health |
| Lambda not in same region | Ensure all services are in the same region |
| HTTP integration timeout set to 0 | Set a positive timeout value |
| No response from backend | Check backend server access logs |
Prevention
- Set Lambda timeout to less than 29 seconds for API Gateway.
- Use WebSocket API for real-time streaming.
- Enable API Gateway Caching to reduce Lambda calls.
- Monitor integration latency with CloudWatch.
Common Mistakes with API Gateway 504
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world AWS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro