Serverless Pruning What is it? When and How to use it?

Joshi
3 min readFeb 17, 2020

--

Hmmm… When I first listened of Serverless? I wondered how a server can be serverless? Let me discuss briefly serverless before going to Serverless Pruning itself.

Serverless. Wait! Serverless? You are probably thinking serverless as a server with no server, But this is not correct. Serverless Framework still uses Servers but the servers it uses are of different Cloud Service Providers like AWS, Microsoft Azure and others.

Serverless

Serverless Framework is not restricted to Amazon Service Provider (AWS). It gives us complete freedom to use it with any other Cloud Service Provider. I will be discussing Serverless with Amazon Web Services. So Serverless functions live on AWS and only executed when they are called or when you use them. This is I love the most about these services. You don't have to pay for these if these are idle.

So we can say that ,
Serverless Architecture mean
You are using a cloud provider’ server.
You pay only for what you use.
More secure.
More Scalable.
You don't have to worry about Maintenance.

Coming to Serverless Pruning…

What is it?

According to google Pruning mean to reduce the extent of (something) by removing superfluous or unwanted parts.

So which unwanted parts we are going to remove? What can be the things which we want to remove from our Serverless System? Lets Discuss…

When a Lambda is deployed through serverless what AWS does is every time a lambda is deployed It creates a different version for your lambda. If you deploy your lambda 100 times it will create 100 different versions for your lambda, and lambda deployment multiple times is common but what if we only want to save the latest version for that specific lambda.

You might have a question in your mind why I can't just keep all the versions for lambdas? When I first came to know about this I also have this question let’s try to find out the answer to this question.

AWS Lambda limits the amount of compute and storage resources that you can use to run and store functions.

AWS Lambda storage limit given by AWS itself is 75GB. It means you can’t store Lambda and layers if this limit exceeded.

Let's suppose you have 25 lambdas running on one serverless project each time deployment is done 25 different versions. One of each lambda is created in AWS. And this is a common case when you deploy your lambda function multiple times. So, these multiple deployments will create different versions adding up to the storage.

If this limit exceeded serverless will throw this error

Code storage limit exceeded. (Service: AWSLambdaInternal; Status Code: 400; Error Code: CodeStorageExceededException; Request ID: 185d95fb-31cc-430b-9129-0551ee4f7667).

To overcome this we have two options

  1. You just need to set version function to false in a serverless file. If you don't specify this it will automatically set version function to true which means you are allowing AWS to create a different version of lambdas.

In serverless.yml

provider:
versionFunctions:true

2. You can use the Serverless Pruning Plugin

Serverless Pruning plugin restricts AWS to create multiple version for lambdas. You can also set how many latest versions you want to keep

How Serverless Pruning can be used?

Install serverless pruning plugin

https://www.npmjs.com/package/serverless-prune-plugin

npm install — save-dev serverless-prune-plugin

And then add the plugin to your serverless.yml file:

plugins:- serverless-prune-plugin

If you want to delete older version of lambdas and still want to keep ’n’ latest versions You can use

sls prune -n <number of version to keep>

Single Function

A single function can be targeted for cleanup:

sls prune -n <number of version to keep> -f helloWorld

Automatic Pruning

This plugin can also be configured to run automatically, following a deployment. Configuration of automatic pruning is within the custom property of serverless.yml. For example:

Thanks for reading!.

--

--