If you find yourself authoring several AWS lambdas for a serverless application architecture, you might have encountered this error:
An error occurred: mySweetLambda– Code storage limit exceeded. (Service: AWSLambda; Status Code: 400; Error Code: CodeStorageExceededException; Request ID: 05d3ae68-a7c2-a3e8-948e-41c2739638af).
The first time I encountered this error I wasn’t quite sure what was happening, but after some quick web searches I learned that AWS has a limit on Lambda storage that maxes out at 75Gb.
Additionally, I also learned that AWS retains all the previous versions of all my lambdas. That’s all fine, I should probably go do some “spring cleaning” and remove the unused versions. AWS does expose the functionality to remove former versions through the console. However, in my scenario I had over 500+ versions for some of my older lambdas. Clicking through 500+ versions is not how I want to spend my time. So what options are available? Surely there has to be some better options out there.
TLDR: Visit https://github.com/karl-cardenas-coding/go-lambda-cleanup for simple and easy to use solution.
Automation is your friend!
There are a handful of open source solutions available that I stumbled upon early on in my clean up journey.
- clear-lambda-storage (python)
- aws-lambda-es-cleanup (Cloudformation)
- SAR-Lambda-Janitor (CloudFormation)
- Serverless framework prune plugin (plugin)
While these options are all great and incredible useful, something didn’t feel right about this approach. I didn’t want to install python on my system (local/CI-CD) unless I absolutely needed it. I also didn’t like the idea of having to deploy infrastructure just for the purpose of cleaning up old lambda versions.
Ideally, I just want to run a single command that cleans up old lambdas, without having to install a language runtime, and/or requiring to setup additional infrastructure for the sole purpose of cleaning up lambdas. Is there such a solution available? The answer is go-lambda-cleanup!
Go-lambda-cleanup is distributed as a single binary and it’s open source. The tool is available for Windows, Mac (Including M1 support), and Linux. No complicated install process, no need for additional infrastructure. Simply issue the command glc clean and the tool will start the clean up process.
Confirm glc is installed correctly by issuing the version command. If you encounter and error then this is a good indicator that the binary is not found in the system path.
$ glc version go-lambda-cleanup v1.0.8
To start cleaning you must have valid AWS credentials. Go-lambda-cleanup utilizes the default AWS Go SDK credentials provider to find AWS credentials.
glc clean command will by default retain all
$LATEST versions and remove the rest.
glc clean -r us-east-1
If you want to retain
$LATEST and the last two versions but remove the remaining versions, simply use the
-c flag parameter.
glc clean -r us-east-1 -c 2
There is also a dry run option available if you want to get a preview of an actual execution.
glc clean -r us-east-1 -d
I encourage you to go checkout go-lambda-cleanup if you have been wanting a simpler solution to conduct AWS lambda cleanup. More details can be found in the project README. Feel free to open op issues if you encounter/observe something odd with the tool!
2021-04-02 23:47 +0000 (Last updated: 2021-04-03 00:12 +0000)
badf7c2 @ 2021-04-03