How to Fix AWS Lambda Function Timeout Error
In this tutorial, you'll learn about How to Fix AWS Lambda Function Timeout Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Your Lambda function returns Task timed out after 3.00 seconds — the execution duration exceeds the configured timeout.
Step-by-Step Fix
1. Increase the timeout
aws lambda update-function-configuration --function-name my-function --timeout 30
2. Check current configuration
aws lambda get-function-configuration --function-name my-function --query 'Timeout'
Expected output:
30
3. Optimize the Lambda code
import json
def lambda_handler(event, context):
data = event.get("payload", {})
result = process_data(data)
return {"statusCode": 200, "body": json.dumps(result)}
def process_data(data):
return {"processed": True, "input": data}
4. Increase memory (also increases CPU)
aws lambda update-function-configuration --function-name my-function --memory-size 1024
5. Configure reserved concurrency
aws lambda put-function-concurrency --function-name my-function --reserved-concurrent-executions 10
6. Use async invocation for long tasks
aws lambda invoke --function-name my-function --invocation-type Event --payload file://input.json output.txt
7. Check CloudWatch logs
aws logs describe-log-streams --log-group-name /aws/lambda/my-function --max-items 5
Common Mistakes
| Mistake | Fix |
|---|---|
| Timeout too low for the workload | Increase timeout to 30s or higher |
| Function makes slow network calls | Use connection pooling or async HTTP |
| Cold start delay | Increase memory or use Provisioned Concurrency |
| Infinite loop in code | Add a loop counter or timeout in code |
| Database connection timeout | Use RDS Proxy or connection pooling |
Prevention
- Set timeout based on the function's actual runtime plus 20% buffer.
- Use CloudWatch Lambda Insights for performance monitoring.
- Implement idempotent retry logic.
- Use Step Functions for workflows exceeding 15 minutes.
Common Mistakes with lambda timeout
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
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