An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
How to prevent Lambda Cold Starts using Serverless Framework?
What are AWS Lambda Cold Starts?
Whenever we call a lambda function it doesn't just start instantly if you haven't called it recently, it takes time before it starts executing the code, this happens because AWS shuts down the container responsible for executing the code inside our lambda function if it's not called for some time then, so the time taken to start up a new container before executing the code inside the lambda function is called a cold start or we can say that it is the delay before actual code execution starts inside our lambda function.
How to deal with Cold Starts?
If we are using AWS Serverless framework then there is a plugin called serverless-plugin-warmup
it is a third party NPM (Node package module) which polls the lambda function of our choice, this way that lambda function stays warm and AWS doesn't drop off the container where it executes the code.
Setting up the plugin
Let's go through each step which is required to setup this warmup plugin for your lambda functions.
Installation
npm install --save-dev serverless-plugin-warmup
First thing is to install this plugin in the root directory of your project, we are adding this package as a dev dependency.
Import the plugin in serverless.yml file
plugins: - serverless-plugin-warmup
Adding warmup plugin configuration
custom: warmup: default: enabled: true folderName: '.warmup' memorySize: 256 name: warmer-default events: - schedule: rate(2 minutes) package: individually: true patterns: - '!../**' - '!../../**' - ./** timeout: 20 concurrency: 1
custom
- We need to define all our warmers in custom
section of our serverless.yml file, here we are setting the default warmer configuration.folderName
- This is the name of the folder where our warmer temporary files will be stored for deploying them to AWS.memorySize
- Memory size we want to give to our warmer function.name
- Name of the warmer lambda function.events
- This is the setting which will determine when to call or in what interval to call the lambda functions to make them warm through the warmer lambda, so here we are scheduling it for every 2 minutes.package
- This is just to exclude and include the package.timeout
- Every time our warmer lambda calls other Lambda functions to make them warm, this is the number of seconds it will wait before exiting the function.concurrency
- Number of parallel calls made to our lambda function for warming them up.
Enable warmup for the lambda function
Now we are ready to enable this warmup setting for our lambda function, here is how we can do it.
warmup: default: enabled: true
We can just add this section to any of our lambda function which we want to warm using our warmer.
Updating our lambda function to handle warmup call
Now we need to update the lambda function which we are warming up using the warmup plugin.
exports.identifyImg = async (event, callback) => { if (event.source === 'serverless-plugin-warmup') { console.log('WarmUP - Lambda is warm!') return 'Lambda is warm!'; }//rest of the code
Here we are just checking if this lambda got called by the warmup lambda, if that is the case we are just returning from it and exiting the lambda execution.
Takeaway
This is one of the way we can deal with lambda cold starts or prevent it, but there are many other ways to do this as well, like optimising our code, avoiding http/https calls inside our lambda functions, using provisional concurrency and many more.
Here is the official documentation for this warmup plugin Serverless WarmUp Plugin
Check out more:
Original Link: https://dev.to/shivangchauhan7/how-to-prevent-lambda-cold-starts-using-serverless-framework-m44
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To