Task timed out after X seconds means your Lambda function didn’t finish within its configured timeout (default 3 seconds, max 15 minutes).
What causes this error
- Timeout too short — default is 3 seconds, which is too short for most real work
- Slow downstream service — database, API, or S3 call taking too long
- Cold start — first invocation takes longer due to initialization
- Infinite loop or deadlock — code never completes
Fix 1: Increase the timeout
In serverless.yml, CloudFormation, or the AWS Console:
functions:
myFunction:
timeout: 30 # seconds (max 900 for 15 min)
Fix 2: Optimize cold starts
- Use smaller deployment packages
- Use provisioned concurrency for critical functions
- Avoid heavy imports at the top level — lazy-load instead
Fix 3: Add timeouts to downstream calls
// Don't let an external API hang your Lambda
const response = await fetch(url, { signal: AbortSignal.timeout(5000) });
Fix 4: Check for connection issues
Lambda functions in a VPC need a NAT Gateway to reach the internet. Without it, external API calls hang until timeout.