Upon reading of this article which discusses the main drawback of using AWS Lambda functions, I'm trying to avoid what the author had to deal with and perform some cleanup on my own code:
"Lambda versions every function. We use the Serverless Framework for developing Lambda application. This means that Serverless creates Lambda functions. Our average Lambda function is about 60MB. [...]
When you couple CI/CD with rapid development and Lambda functions, you get many versions. Hundreds even. And Lambda code storage is limited to 75GB. We hit that limit, and we hit it hard. After two years of CI/CD driven development, our lack of version cleanup led to complete gridlock in our development process."
(emphasis mine)
About my architecture. I have lambda functions that call methods from homemade python packages, which are stored in layers. The goal being to avoid having too much code in the lambdas and easily reuse features among them.
Now, I have found how to remove old versions of my lambda functions (I too am using the Serverless framework, which offers a nice plugin for automatically pruning them upon deployment of the stack). However, I don't know whether old layers are automatically deleted by AWS: I have yet to find a dashboard that summarizes the total code size of all my layer versions, and I couldn't find a plugin that automatically removes them.
Are old AWS Lambda layers automatically removed? If not, how could I batch delete them?
To delete a specific function version, use the Qualifier parameter. Otherwise, all versions and aliases are deleted. To delete Lambda event source mappings that invoke a function, use DeleteEventSourceMapping.
Currently there's no way to do this. Layer Versions are immutable, they cannot be updated or modified, you can only delete and publish new layer versions. Once a layer version is 'stamped' -- there is no way (AFAIK) that you can go back and get back that layer version.
You upload the . zip file archive to your layer from Amazon Simple Storage Service (Amazon S3) or your local machine. Lambda extracts the layer contents into the /opt directory when setting up the execution environment for the function.
Fortunately, you can assign lambda expressions to variables and reuse them, as you would with objects.
Old layers are not automatically deleted. You can run a CLI command such as the following to delete a layer version:
$ aws lambda delete-layer-version --layer-name my-layer --version-number 1
They key point here is that you are deleting a version of the layer. You could run the command above in a loop to delete a bunch of layer versions in bulk, perhaps in combination with a call to list-layer-versions. You can also delete layer versions via the console. Once you remove all the versions of a layer, the layer itself is removed. Some other interesting points from that link are that
When you delete a layer version, you can no longer configure functions to use it. However, any function that already uses the version continues to have access to it. Version numbers are never re-used for a layer name.
The documentation doesn't specify if a "deleted" layer which is retained in lambda while a function still references it counts against your total size quota.
Also see
To avoid breaking functions, a copy of the version remains in Lambda until no functions refer to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With