Lambda versions and Serverless deploys
Using the Serverless framework we have come across an issue where the deploys suddenly stopped. The error that came up was:
Running "serverless" from node_moduleserror:CodeStorageExceededException: Code storage limit exceeded.
The message didn’t appear to make much sense as the Lamda functions were pretty small and the storage for Lambda functions is 75GB. The problem turned out to be that a new version is saved for the function for every deploy and in our case that turned out to be well over 1000 versions.
- Issue: Serverless deploy creates a new version of the lambda function for each deploy
- Issue: Lambda has a maximum code storage of 75GB for functions
- Issue: AWS does not have an option to retain X number of versions
- Issue: Versions can only be deleted 1 by 1
Solutions
Looking into solutions on this I came across 3 which all would work but each had different pros and cons
- Solution 1 — Turn off versions in Serverless
The first solution and the easiest is to just disable function versioning in the serverless.yml
- Solution 2 — Serverless plugin, Set up auto clean up on each deploy, Manual cleanup
A more graceful and better solution was to look at the Serverless Prune Plugin. This gives the option to manually run a clean-up of previous versions or to automate this during the deployment.
Whilst this solution would work for us, I was looking for something a bit cleaner that would not add to the deployment time.
https://www.serverless.com/plugins/serverless-prune-plugin
- Solution 3 — Lambda Janitor
The third solution was to use the Lambda Janitor. This is a Lambda function, triggered by the EventBridge every hour (you can adjust this as it really does not need to be so frequent). During the setup you have the option to set how many versions you want to retain for all functions and on each run it cleans out all the versions above this count.
This solution keeps all your functions in control and is completely independent from the deploy process.
Thanks for reading, I hope this helps!