By default, AWS accounts have a Lambda storage limit of 75 GB. Uploading new packages of code to the lambda service counts towards this limit. Additionally, when using Lambda managed versions, the storage size of each version of your lambdas will also count towards this limit. Without pruning those older versions, you may find yourself greeted with the
Code storage limit exceeded error.
You’ve typically got two options here. Option one, switch off version management of your lambda functions. If you are not using lambda versions for something like canary rollouts or rollbacks, maybe you don’t require them in the first place. YAGNI doesn’t just apply to code! Option two, prune the versions so you only hold on to the latest
n versions. Either way, if you’re seeing the error above, you’ve got some versions to clean up before you can start deploying again.
There are some great tools out there already that can integrate into existing workflows.
- If you’re using the Serverless Framework, there’s the Prune Plugin.
- Lumigo’s Lambda Janitor will provision a lambda that cleans up old versions for you with the use of a periodic CloudWatch event schedule.
At the core of the script is the
aws lambda list-versions-by-function and subsequent piped commands. To delete all the lambda versions except the number you want to keep the script performs the following steps.
- Grab a list of all the versions available for a given lambda and return the JSON object.
- Using JQ to select the
Versionsarray, we sort by
.LastModified, reverse the list (putting newest at the top) and return an array of version qualifiers.
- Remove the
$LATESTversion from the list using grep and the invert match flag (
-v). We certainly don’t want to remove that.
tailto return the list of versions minus the latest
nversions that we want to keep.
- Reverse the list again using the
tailreverse flag (
-r) so we delete from the oldest version to newest. Just in case a panicked CTRL+C is in order.
- If we’re not performing a dry run, delete the function version for each qualifier.
There is something reassuring about a shell script. To shell script is to capture the unexpected tasks of the day. The unforeseen tasks, the laborious tasks, the kind of tasks you didn’t expect to be fighting with. It’s a good feeling, cherry-picking your terminal history and committing it for relative eternity. To take a “step-by-step” guide and turn it into a simple dot slash command. If only to help the next engineer who finds themselves as you did, an unforeseen task in hand, with a newly open terminal on screen.