Using Versions and Aliases to Pre-Warm Lambda Execution Environments when Handling Bursts of Traffic

Want to talk with other .NET on AWS developers, ask questions, and share what you know? Join us on Slack!

In previous posts, I showed how to warm up execution environments for your Lambda functions so you can avoid the cold start penalty when you have a burst of traffic.

In the first example, I showed how to do this with a simple Lambda function. In a follow-up, I showed how to do the same for a full Web API application. Both used the idea of a specially crafted invocation message that allowed multiple execution environments to be warmed.

One issue with the above approach is that it may cause difficulties for the Lambda function while it is serving normal traffic - you are deliberately sending it messages that cause new execution environments to be created, but some of your normal traffic may get caught up in this process, thus suffering from the cold start penalty.

But working around this is easy. You will create two versions of the Lambda function, and one alias. One version for normal workloads, and one for bursts.

The alias will point to the normal traffic version of the function most of the time. This is your day-to-day usage.

When a burst is expected, you will perform the warmup technique on the burst version of the function. See the previous two posts on how to do that.

Then point the alias to the burst version.

When the burst is over, you will point the alias back to the normal version.

The commands

I’m not going to show the code for the Lambda function, or how to deploy it, as they are covered in the previous posts.

Instead, I’ll show how to use the AWS CLI to create the versions, alias, and how to switch the alias between the versions.

Assuming the name of the function is PreWarmEnvs, start by creating the “Normal_Traffic” version of the function -

aws lambda publish-version --function-name PreWarmEnvs --description Normal_Traffic

Before you can publish the “Burst_Traffic” version, you must make some change to the function. You can add a space at the end of a line, edit a comment, etc., an actual code change is not required.

Then publish the “Burst_Traffic” version of the function -

aws lambda publish-version --function-name PreWarmEnvs --description Burst_Traffic

Note the function versions that are returned, it will probably be 1, and 2. You will need them in the next step.

Create the alias named “Prod”, pointing to the “Normal_Traffic” version -

aws lambda create-alias --function-name PreWarmEnvs --name Prod --function-version 1

When you are expecting a burst, you will first warm up the burst version of the function. Again, see the previous blog posts. You can reference the “Burst_Traffic” version by adding the version number to the end of the function name, PreWarmEnvs:2.

Then switch the alias to point to the burst version -

aws lambda update-alias --function-name PreWarmEnvs --name Prod --function-version 2

comments powered by Disqus

Related