AWS Builder Center

What I check first when a Lambda times out

CloudWatch Logs first, then the timeout setting, the slow call, memory, and throttling. A short checklist for a Lambda that times out.

Full-stack Developer & Security Researcher
Start in CloudWatch Logs, not in the timeout setting. Open the log group for the function and find the last lines before the timeout. If the log stops in the middle of a call, the function was still waiting when Lambda cut it off. If there is no log at all for the invoke, the problem is before your code runs.
Check the timeout value next. It tells you how long Lambda waited, not why it waited. Raising it hides a slow call. Fix the slow call first, then set the timeout to a value that fits the real work.
The slow call is usually downstream. A database query, an HTTP call to another service, or an S3 call that waits on a retry. Look at the timestamps in the logs and find the gap. A gap of seconds between two log lines points at the call in between.
A cold start looks different. The log shows INIT_START and a long init time before the first line of your code. If cold starts are the cause, they hit the first invoke after a gap, not every invoke.
A Lambda in a VPC can also wait on the network. If the function calls an AWS service and there is no VPC endpoint for it, the call goes through a NAT gateway or it never leaves. A timeout with no error from the downstream service and no log line after the call is a sign to check the route and the endpoint.
Memory is CPU as well as memory. Lambda gives more CPU when you give more memory. A function that times out while doing real work, not waiting, is often short on CPU. Raise the memory in steps, watch the billed duration and the max memory used in the logs, and stop when the duration stops falling.
A timeout is not a throttling error. A timeout ends with Task timed out after X seconds in the log. Throttling is a 429 error, or TooManyRequestsException, and it happens before your code runs. The fix for throttling is reserved concurrency or a slower caller. The fix for a timeout is in the code or the call it waits on.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article