Lambda "Task timed out": Causes and Fixes
Usually the default 3-second timeout is too low, or a function in a VPC can't reach the internet or AWS APIs.
You'll see this in CloudWatch Logs:
Task timed out after 3.00 secondsHere are the causes, most common first.
1. The timeout is still the default 3 seconds
New functions time out after 3 seconds. Calling an AI model, a database, or an external API often takes longer. Raise it to fit your real workload (the maximum is 15 minutes):
aws lambda update-function-configuration --function-name my-fn --timeout 302. The function is in a VPC with no route out (very common)
A function attached to private subnets has no internet access. Calls to external APIs, and to AWS services like S3, DynamoDB, or Bedrock, hang until the timeout with no useful error.
Fix one of these ways:
- Add a NAT gateway route for private subnets that need the internet.
- Add VPC endpoints: gateway endpoints for S3 and DynamoDB (free), interface endpoints for other AWS services.
- Or don't attach the function to a VPC at all, if it doesn't need private resources.
3. A slow downstream dependency
Log timings around each external call to find the slow one. Set shorter timeouts on your HTTP and SDK clients than the function's own timeout, so you get a clear error instead of a silent hang.
4. Not enough CPU
Lambda gives CPU in proportion to memory. A CPU-heavy function at 128 MB can be many times slower than at 1 GB. Try raising memory, which may even lower total cost.
5. Behind API Gateway? There's a ~30-second ceiling
API Gateway stops waiting after about 30 seconds (29 by default for REST APIs), even if the function's timeout is longer. For long jobs, return immediately and finish the work asynchronously (SQS, Step Functions).
Use context.get_remaining_time_in_millis() to stop gracefully before the deadline, for example to save partial
progress.
Learn more: Lambda limits & pricing, Public vs private subnets, and Lambda best practices.
Key takeaways
- The default timeout is 3 seconds, so raise it to match real work (max 15 minutes).
- Functions in private subnets need a NAT gateway or VPC endpoints to reach anything outside.
- API Gateway caps synchronous requests at about 30 seconds, so make long jobs asynchronous.