A serverless app could have multiple lambda functions. With continuous deployments, new versions of lambda functions get created, and there is a possibility that certain functions in your app could hit AWS Lambda's function memory allocation limit causing deployment failures. To prevent this from happening, here is a Serverless App that you can deploy independently or nest in your existing app which will clean up old versions of a lambda function on a custom schedule.
This app only deletes a maximum of oldest 10 versions of a given lambda function at any time. It sorts versions based on their LastModified attribute in ascending order, fetches top 10 or number of versions - 10 (whichever is lower) and deletes those versions. If a lambda function has less than or equal to 10 versions, the app skips processing the event. The app proactively excludes $LATEST version of a lambda function from the list of versions to delete.
- This app creates a Lambda function that is invoked by a scheduled event.
- Create an AWS account if you do not already have one and login
- Go to the app's page on the Serverless Application Repository and click "Deploy"
- Provide the required app parameters (see parameter details below) and click "Deploy"
LogLevel
(optional) - Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc. Default: INFOFunctionArn
(required) - Specify ARN of a Lambda function for which you want the versions to be deletedLambdaExectionSchedule
(required) - Specify the schedule expression to invoke LambdaVersionsDeleter using https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html. Default: Scheduled to run once everyday at 12 pm UTC.
LambdaVersionsDeleter
- Lambda Versions deleter.LambdaVersionsDeleterArn
- Lambda Versions deleter ARN.
This code is made available under the Apache License 2.0 license. See the LICENSE file.