Introduction Lambda functions that exceed their configured timeout are terminated by AWS, leaving downstream systems without responses. For synchronous invocations (API Gateway), this returns a 504 Gateway Timeout to users. For async invocations, the event is retried up to twice before going to a Dead Letter Queue.
Symptoms - API Gateway returns 504 Gateway Timeout - CloudWatch Logs show "Task timed out after X.XX seconds" - Lambda Duration metric approaching or hitting Timeout configuration - Increased Error count and Throttles in CloudWatch metrics - Downstream systems show missing data or incomplete transactions
Common Causes - Timeout set too low for actual execution time - Cold start latency on VPC-attached functions (ENI creation adds 1-15 seconds) - Insufficient memory causing slow execution (Lambda CPU scales with memory) - Downstream API or database responding slowly - Infinite loops or unbounded iterations in code
Step-by-Step Fix 1. **Analyze Lambda duration metrics**: ```bash aws cloudwatch get-metric-statistics \ --namespace AWS/Lambda --metric-name Duration \ --dimensions Name=FunctionName,Value=my-function \ --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ) \ --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \ --period 300 --statistics Maximum Average p90 ```
- 1.Check for cold start impact:
- 2.```bash
- 3.aws logs filter-log-events \
- 4.--log-group-name /aws/lambda/my-function \
- 5.--filter-pattern "REPORT" \
- 6.--query 'events[].message' | grep -E "Init Duration|Billed Duration"
- 7.
` - 8.Init Duration > 1000ms indicates cold start is significant.
- 9.Increase memory to improve CPU and reduce execution time:
- 10.```bash
- 11.aws lambda update-function-configuration \
- 12.--function-name my-function \
- 13.--memory-size 1024 --timeout 30
- 14.
` - 15.Doubling memory from 256MB to 512MB often reduces execution time by 40-60%.
- 16.For VPC functions, enable Provisioned Concurrency:
- 17.```bash
- 18.aws lambda put-provisioned-concurrency-config \
- 19.--function-name my-function --qualifier prod \
- 20.--provisioned-concurrent-executions 5
- 21.
` - 22.Configure Dead Letter Queue for async functions:
- 23.```bash
- 24.aws lambda update-function-configuration \
- 25.--function-name my-function \
- 26.--dead-letter-config TargetArn=arn:aws:sqs:us-east-1:<account>:my-dlq
- 27.
`