I have lambda versions enabled in my CFN template. Over the time, I see that versions are incrementing, and now it consumes code storage space allocated by AWS.
I would like to know what is the best approach to remove old versions. i.e. I just want to keep current version and one before that, remaining all should be deleted.
Option-1:
- Create a script and run it periodically.
- I am seeing many examples of this. People are referring/following this approach.
- https://github.com/NiteDesign/Lambda-Version-Cleanup/blob/master/Infra-Lambda-Cleanup.js
- https://github.com/epsagon/clear-lambda-storage
Option-2 (not sure if this is possible or not)
- CFN solution
- i.e. I use
AutoPublishAliasattribute to enable lambda versions like below. Is there a way to limit versions in the template itself?
- i.e. I use
Type: AWS::Serverless::Function
Properties:
AutoPublishAlias: $LATEST
Handler: yourHandler.handler
FunctionName: !Sub "YOUR-FUNCTION-NAME"
CodeUri: dist
Events:
healthGet:
Type: Api
Properties:
Path: /YOUR-FUNCTION-NAME
Method: GET