You are already up and running with AWS Lambda for several months, and all of a sudden you get the following error:
An error occurred: TestDashdeliveryLambdaFunction — Code storage limit exceeded. (Service: AWSLambda; Status Code: 400; Error Code: CodeStorageExceededException; Request ID: 05d3ae68-e7f6–11e8–948e-41c27396380e).
AWS limits the amount of “code storage” it saves on their internal S3 for Lambda functions of up to 75GB.
Although it sounds like a lot of space, you can easily reach that limit. In case you’re using the Serverless Framework, its default is to store a version for every deployment you make.
If you don’t need to store a version for each deployment (like many of us), you can easily cancel it with the following addition to your serverless.yml file:
provider:name: awsruntime: python3.6versionFunctions: falseregion: ${opt:region, 'us-east-1'}stage: ${opt:stage, 'dev'}
Adding the versionFunctions: false parameter will cancel the version storing.
If you do prefer to keep older versions (e.g. for being able to rollback quickly), you will need to manually clean the old versions. Luckily, at Epsagon we love sharing open source utilities (for example, spotting dead functions)! This time it’s the clear-lambda-storage. As simple as it sounds, it will take care of removing old and unused versions (i.e. that are neither currently deployed nor $LATEST) from every Lambda function, and from every region. Running it is very simple:
git clone https://github.com/epsagon/clear-lambda-storagecd clear-lambda-storage/pip install -r requirements.txtpython clear_lambda_storage.py
It will output how many versions have been removed, from how many functions, and how many MBs were freed:
Deleted 9412 versions from 502 functionsFreed 42056 MBs
Did you find the tool useful? Feel free to request additional features, and contribute yourself.
Happy serverless-ing ⚡!
Originally published at epsagon.com on December 10, 2018.