4

How to Keep Your AWS Lambda Functions Warm

 2 years ago
source link: https://acloudguru.com/blog/engineering/how-to-keep-your-lambda-functions-warm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Keep Your AWS Lambda Functions Warm

A Cloud Guru News
Jul 18, 2022 9 Minute Read

Learn how to schedule CloudWatch Events Rules to thaw AWS Lambda functions and help reduce response times from 3 seconds to a couple hundred milliseconds.


Keys

Your keys to a better career

Get started with ACG today to transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond.


There are many advantages to using a serverless architecture and there are hundreds of excellent articles extolling its virtues, but one painful downside is the often-slow response time of serverless functions that are infrequently used.

This is because, on occasion, the container that created the function does not get reused. So on some requests, AWS needs to re-provision a container with your code before it can process the request.

For background tasks this obviously isn’t a problem. But if you have a task that is triggered by user input and blocks your app until you receive a response, it’s important that the function is warm so your response time can be a couple hundred milliseconds rather than 3 seconds.

On the other hand, there are times when your code is simply frozen on a container which can be “thawed” before it handles new requests, which only takes milliseconds.

Ideally, AWS would have an option along the lines of “keep warm” that you could toggle and agree to pay more to have AWS keep an unused container running at all times. Alas, this has not (yet?) been implemented, so our next best option is to use a CloudWatch timer.

So how can we keep our AWS Lambda functions warm? As suggested on Stack Overflow, you’ll need to ping your Lambda functions every 5–15 minutes to keep them warm. This is easier than you might expect.

1. Create an Amazon CloudWatch Events Rule

First, open up the AWS console (and yes, there is a way to do this via CLI) and go to CloudWatch. From there, go to Events and click Create rule. Set the event type to Schedule, and run this event every 1 minute.

Create rule in AWS CloudWatch to run every 1 minute.

Then select the Lambda function you want to target from the Targets list and save. you’ll then need to create a name and description.

Select Target to invoke when schedule is triggered.

And that’s it! You’re now pinging your Lambda function every 1 minute.

2. Test the Amazon CloudWatch Events Rule

To test whether or not our timer actually keeps the function alive, we’re going to create a function that stores the unique awsRequestId in a variable called containerID in the global scope (outside of the individual function).

Technically this is not the ID of the container, but it will tell us if a container is being reused for a subsequent request, since we’re only writing to it if a value has not already been defined.

After you deploy this function, set up a timer, and wait some length of time, you can check your CloudWatch logs and you’ll see that the containerID is persisting across requests, which tells us that AWS is keeping the functions warm if there is less than 1 minute between invocations.

If there is less than 1 minute between invocations in your CloudWatch logs AWS is keeping the functions warm.

So for the sake of science, I went ahead and created interval timers for 1–15 minutes, as well as 30, 45, and 60 minute intervals for good measure. This should give us a sense of how long AWS keeps these containers warm.

3. Check the results

Fast forward 8 hours and we have our results. I was hoping the results would be interesting enough to create a fancy chart, but instead, I found this pattern for all of the functions.

1_ngly0c2kazLKzv3ZYTWE0g.png

Basically, all Lambda functions stayed warm for the duration of the experiment except that sometime around 14:00 UTC, AWS decided to reset all containers with no regard to whether or not it had been used recently.

Even the 1-hour ping continued to use the same container for several hours in a row.

Logs showing the 1-hour ping continuing to use the same container for several hours in a row.
These requests were 1 hour apart, but still used the same container.

So it would appear (from this experiment, at least) that the frequency of the pings doesn’t matter all that much…

Either way, pinging every 15 minutes or so still costs approximately nothing, so there’s not reason to be stingy with your CloudWatch timers.


Get the skills you need for a better career.

Master modern tech skills, get certified, and level up your career. Whether you’re starting out or a seasoned pro, you can learn by doing and advance your career in cloud with ACG.


Sam Corcos is the co-founder of CarDash, a full-service automotive concierge provider that eliminates the hassle of auto service, care, and maintenance. He is also the author of Learn Phoenix, and founder of Sightline Maps.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK