Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: Identifying cold starts

Is there a clear way to identify "cold starts"? Either in runtime in the Lambda itself, or via the logs? I know that cold starts are characterized by longer runtimes, which I can actually see, but I'm looking for a clear cut way. I'm using Node.js if that matters.

Update: There are two good answers below, for two use cases: - Identifying the cold start as the lambda runs. - Identifying the cold start from the CloudWatch log.

like image 784
Bachman Avatar asked Nov 01 '17 18:11

Bachman


People also ask

How do you know if Lambda has cold start?

When you look at an X-Ray trace for a Lambda cold start, you will see an Initialization subsegment. This subsegment represents “the function's initialization code that is run before the handler”.

Does AWS Lambda have cold start?

According to an analysis of production Lambda workloads, cold starts typically occur in under 1% of invocations. The duration of a cold start varies from under 100 ms to over 1 second.

Does Lambda layer help with cold start?

Amazon Lambda provides Provisioned Concurrency, a feature that gives you more control over the performance of serverless applications. Using Provisioned Concurrency, you can avoid cold starts and startup latency issues for your Lambda functions.


2 Answers

As an update, AWS now provide visible info on cold starts in the form of "Init Duration" , inside the Report section of a Cloudwatch Log. The calls that do not suffer from a cold start will not contains this information in the log

Duration: 1866.19 ms Billed Duration: 1900 ms Memory Size: 512 MB Max Memory Used: 163 MB Init Duration: 2172.14 ms

like image 155
qkhanhpro Avatar answered Oct 19 '22 04:10

qkhanhpro


If you add some initialization code to the top of your NodeJS script, you will be able to tell in the code that it is a cold start, and you will then be able to log that if you want to see it in the logs. For example:

var coldStart = true;
console.log("This line of code exists outside the handler, and only executes on a cold start");


exports.myHandler = function(event, context, callback) {
  if (coldStart) {
    console.log("First time the handler was called since this function was deployed in this container");
  }
  coldStart = false;

   ...
   
  callback(...);
}

Update:

If you only care about seeing cold starts in the logs, Lambda now logs an extra "Init Duration" value in CloudWatch Logs for cold starts.

like image 29
Mark B Avatar answered Oct 19 '22 05:10

Mark B