Buy @ Amazon

Pre-Warming AWS Lambda Functions



Do you know that if your Lambda function is not invoked for a while and then invoked later, it may not be as responsive as you would wish it be?

And that is because if a Lambda function is not used for a long time (which is subjective to AWS systems as it deems right perhaps based on the demand against available capacity), AWS would re-cycle the container hosting the Lambda function. Subsequent to that for any new requests to this lambda function, AWS needs to deploy the container hosting it for the lambda function execution to happen. This overhead time of launching the container will make your new requests look a little unresponsive.

This can potentially be an issue that might surprise your team and business alike when you least expect it. To avoid such unpleasant and unwanted surprises, you can keep the container active discouraging it to be recycled by AWS infrastructure systems. AWS calls this technique as pre-warming the lambda function.

But what is the best way to put this technique in practice? In order to pre-warm the lamnda function, the best way is to call it using a schedule.

"If schedule, then AWS CloudWatch". Did your mind ring that?

AWS implements scheduling as an event source type (aka triggers) via CloudWatch Events. You can now invoke a Lambda function on a regular, scheduled basis. You can specify a fixed rate (number of minutes, hours, or days between invocations) or you can specify a Cron-like expression. In our case, we can configure a CloudWatch Event rule and select the lambda function as its target. The event is executed every minute to warm up the function so that the function stays active.

You can define this trigger:
  1. for a new lambda function at the time of defining it from Lambda Console like below:



  2. for an existing lambda function that is already deployed from Lambda Console like below:

  3. for an existing lambda function that is already deployed from CloudWatch Management Console like below:
Hope that helps!