amazon-web-servicesaws-lambdaserverlesscold-start

How can I detect cold starts in AWS Lambda?


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:


Solution

  • 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.