Task timed out after X seconds

AWS Lambda Timeout Errors – Fix Them in 5 Minutes

Server & Cloud Intermediate 👁 0 views 📅 May 25, 2026

Lambda timeout errors are almost always misconfigured timeout settings or slow downstream calls. Here's how to pinpoint and fix them fast.

1. The Timeout Value Is Set Too Low (Most Common)

I see this all the time. Someone deploys a Lambda and leaves the default timeout at 3 seconds. Then they wonder why it fails for any real workload. The default is 3 seconds in the console and 30 seconds in CloudFormation. For anything beyond a trivial API call, that's not enough.

The fix: Open your Lambda in the AWS Console, go to Configuration -> General configuration, and click Edit. Set the timeout to something realistic. For an API backend, start at 10 seconds. For a data processing job, 5 minutes. The max is 15 minutes (900 seconds).

# Example: Increase timeout via AWS CLI
aws lambda update-function-configuration --function-name my-function --timeout 30

Don't crank it to 900 seconds blindly. That masks real performance issues. But a reasonable bump? Yes, do it. I usually set it to 30 seconds for API Lambdas and 5 minutes for batch jobs.

2. VPC Configuration Is Killing Your Network Calls

If your Lambda is attached to a VPC, you've probably hit this. By default, Lambdas in a VPC don't get a public IP or NAT gateway. So any call to an external service (DynamoDB, S3, an external API) times out after the function's timeout. The function itself runs fine, but the network request hangs forever.

The culprit: Your Lambda's ENI (elastic network interface) can't reach the internet. This isn't a timeout setting issue. It's a networking problem.

The fix: You have two options. Option A: don't put the Lambda in the VPC unless you absolutely need to access RDS or an internal service. Option B: if you must use a VPC, create a NAT gateway in a public subnet and route traffic from your Lambda's private subnet through it. Or use a VPC endpoint for the specific AWS service you need (like DynamoDB or S3).

# Example: Add VPC config via CLI (replace with your subnet and security group IDs)
aws lambda update-function-configuration --function-name my-function --vpc-config SubnetIds=subnet-abc,subnet-def,SecurityGroupIds=sg-123

I've seen teams waste days on this. The error message says "Task timed out" but the real cause is network unreachable. Check CloudWatch logs for obvious connection refused or timeout errors in the application code.

3. The Code or Dependencies Are Too Slow

You increased the timeout and checked the VPC. Still timing out? Now look at the code. Common reasons:

  • You're making synchronous HTTP calls that take 5 seconds each, and you make 10 of them. That's 50 seconds right there.
  • You're using a heavy library (Pandas, numpy, large ML models) that takes 10+ seconds just to load. That counts against your timeout.
  • Your database queries are unoptimized. No indexes, full table scans, or too many round trips.
  • You're reading/writing large files from S3 without streaming. You load the entire file into memory, then process it. That's slow and memory-heavy.

The fix: Profile your Lambda locally or with AWS X-Ray. For HTTP calls, use asyncio in Python or parallel requests in Node.js. For libraries, use Lambda layers and consider cold start optimization (like keeping dependencies warm with provisioned concurrency). For databases, add indexes and batch your queries.

# Python example: async HTTP calls with aiohttp
import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def lambda_handler(event, context):
    async with aiohttp.ClientSession() as session:
        tasks = [fetch(session, f"https://api.example.com/item/{i}") for i in range(10)]
        results = await asyncio.gather(*tasks)
    return results

Don't use synchronous requests library for multiple calls. That's a rookie mistake. Switch to async or use concurrent.futures. I've cut function runtimes from 30 seconds to 4 seconds just by parallelizing HTTP calls.

Quick-Reference Summary Table

Cause Symptom Fix
Timeout too low Function consistently hits exact timeout limit Increase timeout to match expected runtime
VPC without internet Times out only on external calls, works on internal Add NAT gateway or VPC endpoints
Slow code/dependencies Inconsistent timeouts, high duration in CloudWatch Parallelize, optimize queries, reduce library size

That's it. Three causes cover 95% of Lambda timeout errors. Start with the timeout setting, then check VPC, then optimize code. You'll fix it in under 10 minutes.

Was this solution helpful?