Troubleshoot AWS Lambda Timeout issues

Lambda provides an option to configure timeout value for a function to any value up to 15 minutes. When the specified timeout is reached, AWS Lambda terminates execution of your Lambda function. As a best practice, you should set the timeout value based on your expected execution time to prevent your function from running longer than intended.

Lambda CloudWatch logs can indicate If the lambda function has timed out. You can use following log insight query can be run to filter out lambda invocations that have failed due to time out.

fields @timestamp, @requestId, @message, @logStream
| filter @message like "Task timed out"
| sort @timestamp desc
| limit 100

Below is what an example of a lambda invocation log looks like.

2022-12-20T01:00:00.000-08:00 START RequestId: XXX Version: $LATEST
2022-12-20T01:00:02.500-08:00 END RequestId: XXX
2022-12-20T01:00:02.500-08:00 REPORT RequestId: XXX Duration: 3022.91 ms
Billed Duration: 3000 ms Memory Size: 512 MB Max Memory Used: 157 MB

Following are some common reasons behind lambda function timing out :

  • Lambda function is using default timeout of 3 seconds or the timeout configured is less than the actual time the function needs to complete. Analyze a couple of invocation logs to find out what is the right duration function needs to complete.
  • Lambda function does not have enough compute power to finish all the code before configured timeout. You can configure the amount of memory allocated to a Lambda function, between 128 MB and 10,240 MB default being 128 MB. The amount of memory also determines the amount of virtual CPU available to a function. Adding more memory proportionally increases the amount of CPU, increasing the overall computational power available. If a function is CPU-, network- or memory-bound, then changing the memory setting can dramatically improve its performance. Analyze a couple of invocation logs to find out Max Memory Used by invocation to decide If you need to increase the memory allocated to function.
  • Lambda function is making API calls to public endpoints that are unreachable. A common issue is when lambda function is inside vpc without internet connectivity and lambda function code trying to make HTTP request to a public endpoint. If this is the case, follow the instructions in the following documentation to resolve the internet connectivity related issues. https://repost.aws/knowledge-center/internet-access-lambda-function
  • AWS Lambda functions execute in a container (sandbox) that isolates them from other functions and provides the resources, such as memory, specified in the function’s configuration. Lambda creates and reuses these containers. After the function and all extensions have completed, Lambda maintains the execution environment for some time in anticipation of another function invocation. In effect, Lambda freezes the execution environment. When the function is invoked again, Lambda thaws the environment for reuse. In certain scenarios, It is possible that background processes from previous invocations are slowing the subsequent executions down.
  • Another reason is when an AWS SDK method returns a timeout due to a network connectivity issue or the method that takes longer than the default timeout to complete. Usually, the SDKs have a default timeout value of 60 seconds. Following documentation provides details on how to configure the default timeout when creating a service client. https://repost.aws/knowledge-center/lambda-function-retry-timeout-sdk