Fix Cold Start Timeout on AWS Lambda & Azure Functions
Your serverless function is timing out on first run. Here's how to fix it fast — from a simple config tweak to a deeper architecture change.
Why Your Function Times Out on First Call
I know this error is infuriating. You deploy your serverless function, it works fine in tests, but the first real user call hits a timeout. This is the classic cold start problem. When your function hasn't run for a while, the cloud provider needs to load your code and dependencies into a new container. That can take 5–10 seconds. If your function needs to connect to a database or load a big library, it can go way over the default timeout (like 3 seconds on AWS Lambda or 5 seconds on Azure Functions).
Let me walk you through the fixes. I've seen this on AWS Lambda and Azure Functions a lot. Start with the simplest change — it fixes 70% of cases.
1. Quick Fix: Increase Timeout & Memory (30 seconds)
This is the first thing to try. Most default timeouts are too low for cold starts.
On AWS Lambda:
- Go to your Lambda function in the console.
- Click the 'Configuration' tab.
- Under 'General configuration', click 'Edit'.
- Set 'Timeout' to at least 10 seconds (30 is safer for heavy functions).
- Set 'Memory' to 512 MB or more. More memory = faster CPU too. AWS gives you more vCPU as you increase memory.
- Click 'Save'.
On Azure Functions:
- Go to your Function App in the portal.
- Under 'Settings', click 'Configuration'.
- Find 'functionTimeout' and set it to '00:05:00' (5 minutes) for a consumption plan. For premium plans, you can go higher.
- Also increase memory — set WEBSITE_MEMORY_LIMIT_MB to 1024 or 2048.
- Click 'Save' and restart the function app.
Why this works: Cold starts take longer when your function is memory-starved. More memory speeds up the container launch and your code execution. I've seen cold start times drop from 8 seconds to 2 seconds just by going from 128 MB to 512 MB.
Real-world trigger: This happens a lot with Python functions that import heavy libraries like pandas or numpy. The import alone can take 3-4 seconds on a small function.
2. Moderate Fix: Use Provisioned Concurrency or Always Ready Instances (5 minutes)
If increasing timeout doesn't help, you need to keep your function warm. This costs extra but removes the cold start completely.
On AWS Lambda:
- Go to your Lambda function.
- Click 'Configuration', then 'Provisioned concurrency' under 'Asynchronous invocation'.
- Click 'Add provisioned concurrency'.
- Set it to a number based on your traffic. For a low-traffic app, 1 or 2 is enough.
- Click 'Save'.
Cost note: You pay for those warm instances even when idle. But for critical APIs, it's worth it. At $0.0000001 per GB-second, 1 instance costs about $2-3 per month.
On Azure Functions:
- You need to use the Premium plan (not Consumption).
- Go to your Function App, click 'Scale out' under 'Settings'.
- Set 'Always Ready Instances' to 1 or more.
- Click 'Save'.
Alternative cheap trick: Use a CloudWatch event or Azure Scheduler to call your function every 5 minutes. This keeps one container warm. Here's a simple CloudWatch rule:
{
"schedule": "rate(5 minutes)",
"targets": [{
"arn": "arn:aws:lambda:us-east-1:123456789012:function:your-function",
"id": "keep-warm"
}]
}
Replace the ARN with your function's ARN. This is free if you're on a pay-as-you-go plan.
3. Advanced Fix: Reduce Cold Start Time Itself (15+ minutes)
If you can't keep your function warm (maybe it's a rarely-used function), you need to make the cold start faster.
Steps to shrink your cold start:
- Reduce your deployment package size. Bigger packages take longer to download. Use a smaller runtime like Node.js (starts in ~50ms) instead of Java (starts in ~1s). Python and Go are also fast.
- Lazy-load heavy dependencies. Don't import everything at the top of your file. Import only when needed inside the handler. For example on Python:
import json
def lambda_handler(event, context):
# Import pandas only when needed
if event.get('need_pandas'):
import pandas as pd
# do your pandas work
return {'status': 'ok'}
- Use a custom runtime or container image. If you're using AWS Lambda, switch to a container image. You can pre-warm the container by running an init command that loads your libraries. Create a Dockerfile:
FROM public.ecr.aws/lambda/python:3.9
COPY app.py ./
RUN pip install pandas numpy --target .
CMD ["app.lambda_handler"]
This downloads the libraries during build, not at runtime.
- Use SnapStart for Java functions on AWS. If you're stuck with Java, enable SnapStart. It takes a snapshot of your function after initialization and starts from that snapshot. Cold start drops from 6 seconds to under 200ms. Go to Configuration → General configuration → SnapStart and enable it.
When to do this:
This is for functions that must respond in under 500ms even on first call. Think of APIs for payment processing or real-time alerts.
Why Cold Starts Still Happen and What to Watch For
Even with all these fixes, here's a trap I see a lot: VPC cold starts. If your Lambda function is inside a VPC (virtual private cloud), it has to set up an Elastic Network Interface on every cold start. That adds 10–15 seconds to the start time. The fix is to either:
- Move the function outside the VPC if it doesn't need private resources.
- Use a VPC endpoint for the service you're accessing (like an RDS database) instead of putting the function inside the VPC.
- Or use RDS Proxy — it keeps connections alive across invocations.
One more thing: If you're using Azure Functions on a consumption plan, cold starts are worse because Azure may recycle your function after 5 minutes of idle. Premium plan solves this but costs more.
That's the full fix. Start with step 1 — it's the quickest and works for most people. If not, go to step 2. The advanced steps are only for when you need sub-second latency on first call. You've got this.
Was this solution helpful?