amazon-web-servicesserverlessaws-lambda-layers

Are old AWS Lambda layers automatically removed? If not, how to delete them?


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?


Solution

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