Updated Nov 20, 20266 min read

Lambda 'Task Timed Out After X Seconds': Causes and Fixes

What AWS Lambda's 'Task timed out' log line means, the 3-second default and 1,800-second maximum, the callers with shorter deadlines, and the causes that a higher timeout does not fix.

Illustration for Lambda 'Task Timed Out After X Seconds': Causes and Fixes

Task timed out after 3.00 seconds means Lambda stopped your function because it ran past its configured timeout, which is 3 seconds unless someone changed it. The number in the message is that setting, not how long the work needed.

Raising it fixes the case where the work is slower than the default. For a function stuck on a network call, it only makes the failure take longer.

What does the error mean?

Lambda gives every invocation a deadline equal to the function's timeout, and when it passes, the runtime is stopped mid-execution and the log gets a Task timed out line before the usual REPORT line. Per the timeout docs, the default is 3 seconds and the maximum is 1,800 seconds, or 5,400 for asynchronous and event source mapping invocations on Lambda Managed Instances. Whatever the function was doing is lost, and the caller retries or not depending on how it was invoked.

How do you raise the timeout?

aws lambda update-function-configuration \ --function-name resize-image \ --timeout 60

In AWS SAM it is Timeout: 60 on the function, in Terraform timeout = 60 on aws_lambda_function, and in CDK timeout: Duration.seconds(60). Set it from measured duration with headroom, since a timeout close to the average fails on every slow invocation.

Which callers stop waiting before Lambda does?

CallerIts own limit
API Gateway REST API29 s by default, raisable for regional and private APIs
API Gateway HTTP API30 s, fixed
Application Load BalancerIdle timeout, 60 s by default
SQS event sourceVisibility timeout, which should be at least 6x the function timeout

The API Gateway values are from the REST API quotas and HTTP API quotas, checked on November 20, 2026. If the caller gives up first, the user sees a 504 while the function runs on, and raising the Lambda timeout past the caller's limit does nothing for them.

For SQS, the event source docs ask for a visibility timeout of at least six times the function timeout. With a shorter one, a message becomes visible again while the first invocation is still running and a second one processes it too.

What causes timeouts that a higher limit will not fix?

  • A VPC function calling the internet. A function in private subnets has no route out without a NAT gateway, so a call to a payment API or a public AWS endpoint hangs until the timeout. Add a NAT gateway or VPC endpoints for S3, SQS and the other AWS services it uses.
  • Downstream calls with no timeout. HTTP clients and database drivers often wait far longer than the function's budget by default. Give each call a deadline shorter than the function's, so the error names the slow dependency.
  • Database connections. Opening a new Postgres connection per invocation adds latency, and with hundreds of concurrent invocations the database runs out of connections and new ones wait. Create the client outside the handler and put RDS Proxy in front.
  • Work that is long by design. Video transcoding, large exports and batch jobs that need more than a few minutes belong in a queue consumer or a container task, not a function invoked per request.

How do you find the slow step?

Log the remaining budget at each step with context.getRemainingTimeInMillis(), and wrap outbound calls with explicit timeouts:

// handler.ts (Node.js 22 runtime) import type { Context } from "aws-lambda"; export const handler = async (event: { url: string }, context: Context) => { console.log("start", context.getRemainingTimeInMillis()); // Fail at 5 s with a clear error instead of hanging until the function times out. const res = await fetch(event.url, { signal: AbortSignal.timeout(5_000) }); console.log("fetched", res.status, context.getRemainingTimeInMillis()); return { status: res.status }; };

AWS X-Ray tracing on the function shows the same breakdown per downstream call without the log lines.

How does Encore avoid per-invocation timeouts on AWS?

Encore

Encore is a backend framework with built-in infrastructure, and on AWS your services run as long-lived containers on Fargate in your own account, per the AWS infrastructure docs. There is no per-invocation timeout to configure, and long work goes to Pub/Sub subscriptions, which Encore provisions as SNS topics with SQS queues and dead-letter queues, with the subscription's ack deadline set in code.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

An image resize that used to run in an API Gateway-backed Lambda, split into an endpoint that answers right away and a subscriber that does the work:

// images/images.go package images import ( "bytes" "context" "time" "encore.dev/pubsub" "encore.dev/storage/objects" ) var originals = objects.NewBucket("originals", objects.BucketConfig{}) var thumbnails = objects.NewBucket("thumbnails", objects.BucketConfig{}) type ResizeRequested struct { Key string } var resizes = pubsub.NewTopic[*ResizeRequested]("resize-requested", pubsub.TopicConfig{ DeliveryGuarantee: pubsub.AtLeastOnce, }) type ResizeParams struct { Key string } type ResizeResponse struct { Queued bool } //encore:api public method=POST path=/images/resize func Resize(ctx context.Context, p *ResizeParams) (*ResizeResponse, error) { if _, err := resizes.Publish(ctx, &ResizeRequested{Key: p.Key}); err != nil { return nil, err } return &ResizeResponse{Queued: true}, nil } var _ = pubsub.NewSubscription(resizes, "make-thumbnail", pubsub.SubscriptionConfig[*ResizeRequested]{ Handler: makeThumbnail, AckDeadline: 5 * time.Minute, RetryPolicy: &pubsub.RetryPolicy{MaxRetries: 5}, }) func makeThumbnail(ctx context.Context, msg *ResizeRequested) error { r := originals.Download(ctx, msg.Key) defer r.Close() var src bytes.Buffer if _, err := src.ReadFrom(r); err != nil { return err } out := thumbnail(src.Bytes()) // Same key on a retry, so a redelivered message overwrites the earlier result. w := thumbnails.Upload(ctx, msg.Key) if _, err := w.Write(out); err != nil { w.Abort(err) return err } return w.Close() } // thumbnail stands in for your image library of choice. func thumbnail(src []byte) []byte { return src }

The buckets become S3 buckets in your account, as the object storage docs describe. Locally, encore run runs the topic and buckets for the same code, so the flow is testable without deploying.

A coding agent fixing a timeout like this one can move the work into a subscription in the same change, run it locally and open a pull request, with no queue, event source mapping or IAM policy to set up by hand.

Best fit: Teams on AWS whose functions keep hitting timeouts and who want long work in queue consumers they declare in code.

What is the checklist?

  1. Compare the Duration in the REPORT lines of successful runs with the configured timeout.
  2. Check the caller's limit: API Gateway, ALB or the SQS visibility timeout.
  3. For VPC functions, confirm a NAT gateway or VPC endpoints.
  4. Put a timeout on every outbound call and log the remaining time.
  5. Raise the function timeout only to measured duration plus headroom.
  6. Move work longer than a minute or two to a queue consumer.

Frequently asked questions

What is the default timeout for AWS Lambda?

The default is 3 seconds, and you can raise it in 1-second steps up to 1,800 seconds (30 minutes), and functions on Lambda Managed Instances allow up to 5,400 seconds for asynchronous and event source mapping invocations.

Why does my Lambda time out only in a VPC?

A function attached to a VPC has no internet access unless its subnets route through a NAT gateway, so calls to public APIs, including some AWS service endpoints, hang until the function times out. Add a NAT gateway or VPC endpoints for the AWS services it calls.

Does raising the Lambda timeout fix API Gateway timeouts?

No. API Gateway REST APIs stop waiting at 29 seconds by default and HTTP APIs at 30 seconds, so a longer function timeout only means the function keeps running after the client already got a 504.

How do I find what is slow inside a timed-out Lambda?

Add timeouts to every outbound call so the slow one fails with its own error, and log the remaining time from context.getRemainingTimeInMillis() at each step. AWS X-Ray tracing shows the time spent in each downstream call.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders