Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to keep an AWS Lambda function warm?

There are a few pieces of my app that cannot afford the additional 1-2 second delay caused by the "freeze-thaw" cycle that Lambda functions go through when they're new or unused for some period of time.

How can I keep these Lambda functions warm so AWS doesn't have to re-provision them all the time? This goes for both 1) infrequently-used functions and 2) recently-deployed functions.

Ideally, there is a setting that I missed called "keep warm" that increases the cost of the Lambda functions, but always keeps a function warm and ready to respond, but I'm pretty sure this does not exist.

I suppose an option is to use a CloudWatch timer to ping the functions every so often... but this feels wrong to me. Also, I don't know the interval that AWS uses to put Lambda functions on ice.

like image 658
samcorcos Avatar asked Mar 18 '17 17:03

samcorcos


People also ask

How long does AWS keep Lambda warm?

AWS needs a ready supply of containers to spin up when functions are invoked. That means that functions are kept warm for a limited amount of time (usually 30 – 45 minutes) after executing, before being spun down so that container is ready for any new function to be invoked.

How long does a lambda function stay alive?

AWS Lambda only allows a maximum of 15 mins runtime before the function times out and the container dies, confining all un-persisted work (and running computations) to the graveyard of dead containers never to be restarted.


2 Answers

BBC has published a nice article on iPlayer engineering where they describe a similar issue.

They have chosen to call the function every few minutes using CloudWatch Scheduled Events.

So in theory, it should just stay there, except it might not. So we have set up a scheduled event to keep the container ‘warm’. We invoke the function every couple of minutes not to do any processing, but to make sure we’ve got the model ready. This accounts for a very small percentage of invocations but helps us mitigate race conditions in the model download.(We also limit artificially how many lambdas we invoke in parallel as an additional measure).

like image 129
Jonathan Avatar answered Sep 18 '22 03:09

Jonathan


UPDATE DEC 2019

AWS now also offers 'Provisioned Concurrency'. https://aws.amazon.com/blogs/aws/new-provisioned-concurrency-for-lambda-functions/

Basically you pay around 10$/month (for a 1GB Lambda) per instance that you want to keep 'warm'.

like image 29
GeertPt Avatar answered Sep 21 '22 03:09

GeertPt