Serverless Function Times Out After 30 Seconds? Here’s the Fix

Server & Cloud Intermediate 👁 6 views 📅 Jun 29, 2026

Your function keeps timing out. Start by checking the timeout setting, then move to code or config fixes. I’ll walk you through it.

Start Here: Check Your Timeout Setting (30 seconds)

I know this error is infuriating. You’re running a serverless function—maybe on AWS Lambda, Azure Functions, or Google Cloud Functions—and it just dies after 30 seconds. No error messages except maybe a 504 time-out or a generic “function execution timed out”. The first thing I always check is the timeout setting itself.

Most providers set a default timeout of 3 seconds or 30 seconds. For a simple HTTP request, that’s fine. But if your function calls an external API, reads a file, or processes data, 30 seconds can be too short. Go to your function’s configuration page and look for “Timeout”. Change it to something higher—like 5 minutes (300 seconds) for AWS Lambda, or up to 10 minutes for Azure Functions. Don’t go crazy though; some providers cap it at 15 minutes. I recommend starting with 60 seconds and see if that helps.

Here’s the catch: increasing timeout alone won’t fix slow code or database calls. But it’s the simplest fix. Try it. If your function still times out, move to the next step.

Moderate Fix: Optimize Your Code (5 minutes)

So you bumped the timeout to 60 seconds, and it still fails. That’s annoying, but it’s telling you something important: your function is taking too long to run. Let’s look at the code.

Common culprit number one: synchronous HTTP requests inside the function. If your function calls an external API with requests.get() in Python or fetch() in Node.js without a timeout, it can hang forever. Always set a timeout on your HTTP calls. For example, in Python:

import requests
try:
    response = requests.get('https://api.example.com/data', timeout=10)
except requests.except.Timeout:
    # handle timeout gracefully
    return {'error': 'API call timed out'}

Same for Node.js with fetch():

const response = await fetch('https://api.example.com/data', {
    signal: AbortSignal.timeout(10000)  // 10 seconds
});

Another big one: database queries. If you’re doing a slow SQL query that scans a huge table, that’s going to kill your timeout. Add indexes, limit the results, or paginate. For example, in SQL, change SELECT * FROM orders to SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '7 days'. This reduces the data the function needs to process.

Also, check if you’re using a loop that’s too large. A for loop processing 10,000 items one by one can take forever. Use batch operations instead. In Python with boto3 for AWS, use batch_write_item instead of individual put_item calls.

After you make these changes, redeploy and test. If it still times out, we go deeper.

Advanced Fix: Rethink Your Architecture (15+ minutes)

If the function still times out after optimizing code, the problem is likely the architecture itself. Serverless functions aren’t designed for long-running tasks. I’ve seen this trip up people who try to run ETL jobs or bulk file processing directly in a Lambda function. It won’t work.

First, check if your function is doing a cold start. Cold starts happen when the function hasn’t been called in a while and the provider needs to initialize the runtime. This can add 5-10 seconds alone. To avoid it, you can keep the function warm by pinging it every 5 minutes (using a CloudWatch event or a cron job). But that’s a hack. Better solution: if your function is time-sensitive, use a provisioned concurrency setting (AWS Lambda) or always-on instances (Azure Functions Premium plan). This costs more but removes cold start.

Second, consider splitting your function into smaller parts. For example, if your function processes a CSV file and sends emails, do the processing in one function and the email sending in another. Use a queue like SQS or a pub/sub service to chain them. This way each function runs under 30 seconds.

Third, and this is my favorite move: switch to a long-running service instead of a function. If your task takes more than 15 minutes, serverless functions are the wrong tool. Use AWS ECS with Fargate, Azure Container Instances, or a simple VM. You pay for the time, but it won’t time out.

Here’s a specific scenario: I had a customer whose function processed large PDF files. It timed out at 5 minutes. We moved the processing to a Docker container on ECS, and it finished in 8 minutes. Problem solved.

One more thing—check your function’s memory setting. Some providers (like AWS Lambda) give you CPU power proportional to memory. If you set memory too low, your code runs slower and times out faster. Bump memory from 128 MB to 512 MB or 1 GB. Yes, it costs more, but it might save you from rewriting everything.

If none of these work, look at the logs. In AWS CloudWatch, Azure Monitor, or Google Cloud Logging, search for “timeout” or “error”. You might see a specific error code like “Task timed out after 30.03 seconds”. That confirms it. Then go back to the code and check the slowest part.

One last tip: add a health check endpoint to your function. If it’s behind a load balancer, the load balancer might have its own timeout. For example, AWS ALB has a 60-second idle timeout. If your function takes 70 seconds, the ALB kills the connection. Raise that timeout in the load balancer settings too.

I know this is a lot. Take it step by step. Start with the timeout setting, then code, then architecture. You’ll get it.

Was this solution helpful?