DEV Community

Cao Duy Khanh
Cao Duy Khanh

Posted on • Updated on

How to Free AWS Lambda Code Storage when Limit Exceeded

Use the following open-source tool on GitHub.

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

Why is it happening?

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 exceed that storage limit. In case you’re using the Serverless Framework, its default is to store a version for every deployment you make. It’s 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: nodejs14.x
  versionFunctions: false
  region: ${opt:region, 'us-east-1'}
  stage: ${opt:stage, 'dev'}
Enter fullscreen mode Exit fullscreen mode

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

clear-lambda-storage: auto-clean old versions

If you prefer to keep older versions (e.g. for being able to rollback quickly), you’ll need to manually clean old ones. 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/khanhcd92/clear-lambda-storage-nodejs.git
cd clear-lambda-storage-nodejs/
npm i
node index.js 
Enter fullscreen mode Exit fullscreen mode

Advanced usage

Provide credentials:

node index.js --access-key <access_key_id> --secret-key <secret_access_key> --num-to-keep <number>
Enter fullscreen mode Exit fullscreen mode

Provide credentials with one region:

node index.js --access-key <access_key_id> --secret-key <secret_access_key> --num-to-keep <number> --regions <region_code>
Enter fullscreen mode Exit fullscreen mode

Provide profile:

node index.js --profile <profile_id> --num-to-keep <number>
Enter fullscreen mode Exit fullscreen mode

Provide profile with one region:

node index.js --profile <profile_id> --num-to-keep <number> --regions <region_code>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)