AWS Lambda: Identifying cold starts

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.


If you're looking at CloudWatch logs, each LogGroup for your Lambda function represents a separate container and therefore the first invocation for that LogGroup is your cold start.


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