Skip to content

How do I troubleshoot Lambda function cold start issues?

4 minute read
1

My AWS Lambda function has a high latency cold start duration.

Resolution

To troubleshoot Lambda function high latency and cold start issues, follow these instructions to check your configuration requirements and review best practices.

Lambda function code and configuration best practices

Take the following actions:

  • Increase the memory allocated to the Lambda function. The cold start range depends on the size of your function, the amount of memory that you allocated, and the complexity of your code. Add more memory proportionally to increase the amount of CPU and available computational power. For more information, see Determining the appropriate memory setting for a Lambda function.
  • Minimize the size of your deployment package. The smaller your deployment package, the faster your function starts up. Minimize the number of dependencies and external libraries that your function imports. Also, make sure that your deployment package size is less than 50 MB zipped. For more information, see Deploying Lambda functions as .zip file archives.
  • If you use container-based deployments, then remove unnecessary artifacts to keep your image sizes minimal. For more information, see Deploy Lambda functions with container images.
  • Optimize your Lambda function code to minimize the time that it takes to initialize. Reduce the number of dependencies and external libraries that your function imports. Reduce the amount of code run during initialization. For more information, see Operating Lambda: Performance optimization – Part 1.
  • Avoid complex computation at startup. If your function requires complex computation at startup, such as loading large datasets, then run the computation in the background during the Init phase. Then, cache the results for subsequent invocations. Caching the results reduces the time for complex computation at startup.
  • Reuse Amazon Relational Database Service (Amazon RDS) database connections. If your function connects to an Amazon RDS database, then create an Amazon RDS Proxy database proxy for your function. A database proxy manages a pool of database connections. Reusing Amazon RDS database connections reduces the time to get a connection for each time your function's invoked.
  • Configure provisioned concurrency for your function. Functions that use provisioned concurrency don't show cold start behavior because the execution environment is prepared before invocation. Specify the number of function instances to keep warm and manage traffic.
  • Activate Lambda SnapStart to improve startup latency. When you publish a function version, SnapStart creates an encrypted snapshot of the initialized execution environment and then restores new environments from that cached snapshot. It's a best practice to use SnapStart with function invocations at scale. For more information, see Improving startup performance with Lambda SnapStart.
    Note: SnapStart doesn't support all features such as provisioned concurrency and can incur additional costs. For more information, see Supported features and limitations and SnapStart pricing.
  • Minimize the complexity of your dependencies. Use frameworks that load quickly on execution environment startup.
  • To reduce the time that it takes Lambda to unpack deployment packages authored in Java, put your dependency .jar files in a separate /lib directory. For more information, see Deploy Java Lambda functions with .zip or JAR file archives.
  • Use monitoring to discover issues and observability to discover why. Monitor the cold start performance of your functions, and use Lambda Insights in Amazon CloudWatch to troubleshoot performance issues.

For more information, see Best practices for working with Lambda functions.

Long INIT duration in provisioned concurrency

For on-demand Lambda functions, the static initializer runs after a request is received but before the handler is invoked. This results in latency for the requester and contributes to the cold start duration.

Take the following actions:

API Gateway requests integrated with Lambda

If you make requests to an Amazon API Gateway API that's integrated with a Lambda function and experience latency, then check your configuration requirements. Review your Lambda function's CloudWatch Logs to find the cause of the latency.

For more information, see How do I troubleshoot high latency in my API Gateway requests that are integrated with Lambda?

Related information

How do I reduce initialization and invocation duration latency for my Java Lambda function?

Troubleshoot invocation issues in Lambda

AWS OFFICIALUpdated a month ago