Out from the Cold — A Simple Guide To Avoiding Cold Boots by Warming Your AWS Lambda Functions

Sean Drummy
Analytics Vidhya
Published in
5 min readFeb 3, 2021

There have been plenty of articles written on the ways to optimize your AWS Lambda functions to run quicker and to avoid the dreaded “cold boot” scenario. This is despite the fact that it was reported by Amazon tech advocates that an extreme minority of function invocations are actually cold booted (approximately 0.2%). That said, there are a number of scenarios I’ve encountered where you’re definitely going to hit a lot of cold boots and it’d really be better to avoid them:

  1. Small projects or prototyping with several functions in play, all of which you may call at different, spaced out times. Even if your tinkering with something that will never see the light of day, waiting for Lambdas to spin up gets tedious quick.
  2. You’ve stood up a portfolio or demo website and you’re applying for a job. If you’re trying to make a good impression with something cool you’ve built, it’s best to avoid ruining it by presenting a load spinner for extended periods.
  3. It’s often mentioned that the cold boot wait time is a couple of seconds — that has NOT been my experience at all. For me, I’ve regularly spent 5–6 seconds waiting for my Lambda to roll out of bed and get to work.

With these fairly lightweight scenarios in mind, let’s dive into a simple, straightforward approach to warming your Lambdas.

The Obligatory Opening Explanation

Simply stated, a “cold boot” is the slow first time startup of a Lambda function.

After the function initially “boots”, it is available on a much faster basis until the Lambda goes unused for an extended period of time (or the data center is struck by an asteroid). After this extended period of time (or unfortunate wrath of the heavenly bodies), the Lambda function is destroyed so the whole cold boot process has to start up again.

“Warming” a function is programmatically invoking it on a set schedule so you do not allow the gremlins that power Amazon’s Lambda infrastructure to destroy your Lambda environment. This keeps the function in that quick and responsive state so your application, website, or whatever stays peppy.

Code To Keep Warm

Before we dive into code, first let’s cover some simple but excellent ground rules on warming Lambdas:

  1. Don’t Invoke the Whole Function — You should code directly into your function a very short, quick logic path to warm the function. If you have a Lambda with DB calls, follow-on calls to other Lambdas, or anything taxing, save the compute time and make the warmer call as efficiently and cheaply as possible.
  2. Warm as Sparsely as Possible — If you set the warm interval for about 5 minutes, you will ensure that your function is always ready for a punctual response, but you won’t needlessly tax your infrastructure either.
  3. Stay Close to the Source — If you have Lambda that’s called far down a stack of other infrastructure on top of it (the most common example being an API gateway), avoid invoking the function at the top of the stack. Any cyclical job like this should invoke the function as close to the Lambda itself as you can manage.

The first order of business is how to tell your function you’re only warming it. If your function doesn’t know when it’s only being warmed as opposed to run fully, you can’t adhere to our first ground rule. The simplest way to do this is to pass a Boolean or bit into the event object that all Lambdas receive. That way, the function knows to short-circuit its full run and only return as quickly as possible to keep your microservice alive and warmed. We’ll cover the specifics of how this will work in a bit.

Alright, code time! Below is an heavily simplified example where you receive a widget ID and then return a list of widgets associated:

As you can see, right on line 4 we immediately deal with our warming logic to prevent any potentially costly operations from ever being executed. Given how much cloud tech is based on I/O operations or compute time, this could save us a lot of money or heartache in the future. If we’re not passing in a warming call, then the function will execute as normal.

Now I’m going to assume that you already know the basics of how to deploy and test a Lambda. But because I said this was a simple guide, I wanted to make this as easy as possible to modify my example. If I wanted to locally test this function to make sure just the logic worked, I could simply append the following to the bottom of my function:

context= ''event={'warmer': 1 }print(getWidgets(event, context))

Similarly, changing my event dictionary to make warmer equal 0 would invoke the full function (which in your case will fail unless you absolutely love setting up DynamoDB instances in which case, gold star 🌟).

Let’s Warm a Lambda

Now it’s time to actually set up the warming mechanism in AWS and also touch on our final two ground rules. I’m going to assume you already know how to deploy a Lambda in the many ways that are available. If you don’t, then leave a comment saying “Hey Sean, your caveman brain and 6th grader writing capability somehow makes sense to me — please write an article about deploying Lambdas.”

Now that we’ve deployed our function, let’s give it a test to make sure we can warm it without the AWS infrastructure setup. This can save us a lot of grief if there was a problem in our code or the lambda deployment but we start in on the other infrastructure pieces without testing. Let’s hop into our Lambda on the AWS console and setup a new test, pasting in only this simple JSON.

{ “warmer”: 1 }

Assuming that ran just fine and the function returned “warmed” — let’s configure the real thing. To do this, we’ll want to head to CloudWatch > Events > Rules. Now let’s create a new rule. There’s plenty to fill in and discuss here but I’m going to keep this short and sweet. Make sure you select a Lambda function as your target and feed it the JSON you just pasted in for our test. You could select your API gateway or something higher in the stack of your Lambda but lest we forget the aforementioned rule 3.

Other than that, the rest of the settings should be fairly straightforward. Pursuant to rule 2, we want to only warm on a 5 minute schedule. That should be frequent enough and Save your Event, make sure it’s active and… congrats! You’re now warming your Lambda.

Thanks for reading! Please do leave suggestions for improvement, comments, or withering criticism below. If you’re feeling especially nerdy, connect with me on LinkedIn.

--

--

Sean Drummy
Analytics Vidhya

Hey, I’m Sean. I’m the Senior Director of Product at Vee24. UX Guru Wannabe, Python nerd, closet .NET Fan, Self Deprecation Aficionado