AWS Lambda Cold Start fixed! for good! (censored)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
aws lambda cold starts can be a real pain but now there is the ultimate solution provisioned concurrency hi i'm rafael and this is codercave if you code lambdas you surely enjoy the benefits of serverless but while that lambda may seem serverless to you it is definitely not serverless for aws in other words your code has to run somewhere which means that an environment needs to be loaded and initialized and that is our cold start in this episode we will explore the hacky way to fix it and the proper solution using provisioned concurrency and stay tuned till the end because there is a what moment waiting for you so let's have a look at our test lambda it's nothing special really it just gets a file from an s3 bucket computes a sha 56 for each line in this file and then returns so let's go ahead and start deploying this this stack now let's go back to our code and have a look at the template file this code is provisioned with a half gigabyte of ram runs on a python 3.7 framework and has x-ray tracing activated now this lambda is also accessible via api gateway but this is not really the point of this exercise all right so now that the code the stack has been deployed let's go to aws console so we should see our lambda in here and there it is so this is our lambda let's hit it with a test payload now it doesn't really matter what's in this payload because we are not using any parameter so let's just run the lambda and then once it's run let's have a look at the x-ray so the successful execution is done let's go into x-ray and look at the one trace that we expect so here is our one trace so if we open this we see that one of the steps is initialization so as you can see nearly 300 milliseconds of the execution time have been spent in initialization now if our goal is to keep this lambda warm we can try one hacky solution so let's get back to our code so in the template i'm going to define define a new lambda so i will get this whole thing copy paste to avoid making stupid mistakes here i will call this lambda a warm up uh the this is going still to be a python 3.7 the handler will be called warmup and here i only need 128 megabytes of memory this code is only going to kick off the other lambda now if i want to keep that demo service lambda worm i will want to hit it every let's say 10 minutes so to do that i will create an event and the event will be i don't know warmup.lab and the type is going to be scheduled and i will want this thing to fire every 10 minutes so what this is going to do this is going to start this one lambda every 10 minutes and this lambda can fire off fire the other lambda now we need there is one one detail that we need to take care of and i have a snippet for this and this is the policy that allows this lambda to call the other lambda so i will type it in here i already have it ready so this this allows the warm-up lambda to start the demo service function all right so there is one last thing we'll need here is an environment variable that tells us the arn of the demo lambda so an environment variables and i'll call this target function so what this is going to do this is going to add an environment variable in the execution environment of the lambda so we can use it in our warm up function so let's go to the code we have here so because we are going to access an environment variable i will want to import os actually i don't need to import it all os we can say from os import get n so this is the function that lets us get an environment variable so let's create our handler fine we called it warm up and what we are going to do here is first of all create a client so that's a bottle 3 client for lambda execution then the client we'll have to invoke and for this i have a snippet already prepared for you so what this is going to do is going to invoke the lambda it needs the function name and the function name is taken by from the environment variable target function target function was defined here so in this step we have the arn of our demo function no payload the payload needs to be encoded all right so here we have our lambda and we can go ahead and redeploy our stack so all right there is a typo in in the template so let's go back to the template let's correct that and uh and let's try again the deployment all right now our stack has been updated let's have a look at the lambdas now we will have two lambdas in our console so there we go we have two numbers we have the warm-up and the demo service now because we set the event to fire every 10 minutes the lambda will fire 10 minutes after it was deployed so that's a pretty long wait so let's trigger it by launching a test again so what this is supposed to do it's going to call the other lambda so let's just test it and we are done so now let's go back to our demo service and let's go into the tracing so the this is the latest invocation we go in and here is our initialization again now this was shorter 255 earlier we had almost 300 milliseconds now if i go back to my lambda over here the demo service i can test it again and once i'm done i will go back into the traces i will update it and i will have one more execution 10 seconds ago if i go into this execution there is no initialization because the index the execution has already been uh performed the initialization was already done so there is no cold start all right this will work but it sucks it sucks on two different levels so on the first level there is code that is there only to make that other lambda slightly faster on the other level we don't have much control over scalability so if we want to spin let's say 10 other executions we will have to multi-thread the execution of lambdas in the warm-up also there will be a sub-optimal amount of calls to the demo service our warm-up lambda will run every 10 minutes and it may try to warm up executions even if it's not totally needed okay let's implement the proper solution so first step let's throw away all this unnecessary code let's grab the configuration and throw it in the bin okay this is done now let's go to the actual python code let's throw this away and i guess also this reference is not needed anymore it's import all right now to use the provisioned concurrency we will need to add just a few properties to our lambda so we go back into the properties and we will add the autopublish alias and we'll call it live i will clarify what this is for later the other one will be provisioned concurrency configuration and we will give it as you guessed it i have a snippet for that we'll give it a value of one which means there will be one execution worm for us all right now this is all we need to have the con the provisioned concurrency so let's deploy our code again all right the stack has been updated so now we can go back to the console now we can see that there is only one lambda right now and this uh it's our demo service the warm up function has been erased now let's hit it we have our test so let's test it and then we go into x-ray to see what happened to the the cold start so execution successful let's move to x-ray and which is which one is our execution this one is our execution and oh what what wasn't this supposed to be initialized well actually here's here's the what moment i promised at the beginning this happens because we hit the latest version of this uh lambda what we need to hit is the lambda with the alias for which we defined the provisioned concurrency so if we hit this one and we test and then we go into x-ray all right so here's our execution trace and we do see an initialization over here but look at the diagram this is this looks really weird you see the initialization block is over here and then there is absolutely nothing for minutes so what does this mean well this is our provision concurrency that initializes the execution and then keeps it warm and when we hit it we have our actual invocation here now this is happening only for the trace of the first call for some reason so if we go back to our lambda and we hit live again with the test and we go back into x-ray we reload this trace here hasn't responded yet okay now it's done this one will not show the initialization as expected be aware that provisioned concurrency comes at a cost so now you have it the ultimate solution to aws lambda called start now go to github.com codercave and fork the repos or issue pull requests subscribe to the channel and let me know in the comments down below what's your experience with aws lambda called starts
Info
Channel: Coder Cave
Views: 1,588
Rating: undefined out of 5
Keywords: aws lambda, codercave, aws lambda cold start, aws python lambda
Id: 08NuuWw4Vek
Channel Id: undefined
Length: 13min 57sec (837 seconds)
Published: Thu Apr 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.