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. 1.Check for cold start impact:
  2. 2.```bash
  3. 3.aws logs filter-log-events \
  4. 4.--log-group-name /aws/lambda/my-function \
  5. 5.--filter-pattern "REPORT" \
  6. 6.--query 'events[].message' | grep -E "Init Duration|Billed Duration"
  7. 7.`
  8. 8.Init Duration > 1000ms indicates cold start is significant.
  9. 9.Increase memory to improve CPU and reduce execution time:
  10. 10.```bash
  11. 11.aws lambda update-function-configuration \
  12. 12.--function-name my-function \
  13. 13.--memory-size 1024 --timeout 30
  14. 14.`
  15. 15.Doubling memory from 256MB to 512MB often reduces execution time by 40-60%.
  16. 16.For VPC functions, enable Provisioned Concurrency:
  17. 17.```bash
  18. 18.aws lambda put-provisioned-concurrency-config \
  19. 19.--function-name my-function --qualifier prod \
  20. 20.--provisioned-concurrent-executions 5
  21. 21.`
  22. 22.Configure Dead Letter Queue for async functions:
  23. 23.```bash
  24. 24.aws lambda update-function-configuration \
  25. 25.--function-name my-function \
  26. 26.--dead-letter-config TargetArn=arn:aws:sqs:us-east-1:<account>:my-dlq
  27. 27.`

Prevention - Set timeout to 2-3x the p99 duration, never the default 3 seconds - Use Lambda Power Tuning tool to find optimal memory/price ratio - Enable AWS Lambda Service Lens for end-to-end tracing - Configure Provisioned Concurrency for latency-sensitive functions - Set downstream connection timeouts lower than Lambda timeout