How to Free AWS Lambda Code Storage (CodeStorageExceeded)

What is it about?

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).

What’s the problem?

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. It is also affected by the programming language you are using.

Solving the issue

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: aws
  runtime: python3.6
  versionFunctions: false
  region: ${opt:region, 'us-east-1'}
  stage: ${opt:stage, 'dev'}

Adding the versionFunctions: false parameter will cancel the version storing.

clear-lambda-storage: auto-clean old versions

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-storage
cd clear-lambda-storage/
pip install -r requirements.txt
python 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 functions
Freed 42056 MBs

Did you find the tool useful? Feel free to request additional features, and contribute yourself.

Happy serverless-ing !