AWSSenior

Lambda function is slow, times out or loops

Lambda timeouts trace to too little memory and CPU, large payloads, a slow downstream service, or a recursive loop between the function and its trigger.

What you see

  • The function runs far longer than on your laptop
  • Invocations hit the configured timeout
  • A function keeps being invoked without end

What it means

When your code runs much longer in Lambda than locally, it is often constrained by the memory or CPU it has. In Lambda, memory and CPU scale together.

Timeouts also come from large payloads, slow downstream services, and loops. A function that is invoked by an event, then changes the same resource that triggers it, invokes itself again.

How to confirm the cause

  1. 1
    Check the duration against the timeout
    # CloudWatch: look at Duration (p99) and Errors for the function, and the REPORT line in its logs

    Compare the longest runs with the configured timeout.

  2. 2
    Check the payload size
    # The maximum size of a Lambda event payload is 1 MB.

    Large payloads mean more work per invocation.

  3. 3
    Check the function's downstream calls
    # Look for calls to databases, third-party APIs or other services in the function code.

    Slow or unavailable downstream services show up as timeouts.

Causes and fixes

  • Not enough memory (and so CPU)

    HOW TO TELL
    The same code is much slower than locally, and memory use is near the limit.
    ROOT CAUSE
    Memory sets CPU. A small allocation means a small slice of CPU.
    FIX
    Configure the function with more memory, which also raises CPU.
  • A large payload outruns the timeout

    HOW TO TELL
    Small events succeed; larger ones time out at a predictable item count.
    ROOT CAUSE
    The docs' example: one record per 100 ms with a 3 second timeout works up to about 29 items and fails above 30.
    FIX
    Set the timeout for the largest expected payload, test with upper-bound data, and validate payload sizes.
  • A slow or unavailable downstream service

    HOW TO TELL
    Timeouts line up with another system's slowness or outage.
    ROOT CAUSE
    Lambda scales fast, and a non-serverless dependency can fall over under the spike. The function then waits.
    FIX
    Handle errors and retries in code, cache results, put a queue such as SQS in front of the dependency, and use a connection proxy such as RDS Proxy for databases.
  • A recursive loop with the trigger

    HOW TO TELL
    Invocation counts keep climbing, and the function writes to the same bucket or table that invokes it.
    ROOT CAUSE
    Each write creates an event that invokes the function again. The docs describe this for S3, SQS and DynamoDB sources.
    FIX
    Write to a different resource, or a different prefix or suffix than the trigger. Use event filtering, and use recursive loop detection to find these.

How to stop it coming back

  • Alarm on Duration p99 and on throttles.
  • Load test with maximum-size payloads.
  • Never write to the same resource that triggers the function without a filter.

Keep going

Sources

AWS documentation (docs.aws.amazon.com), read 18 Nov 2026.

Search

Search cheat sheets and projects