Deleting MANY Lambda Function Versions

I recently came across a challenge where I wanted to purge old Lambda Function versions. Some of the functions had over 65 versions!

The code below will iterate through a text file with a Lambda function defined on each line. It will get a list of all the versions and delete any version that is not the highest version or any versions that are attached to an alias.

import boto3
from botocore.exceptions import ClientError

client = boto3.client("lambda", region_name='us-west-2')
def delete_function_version(function_name, version):
    
    try:
       response = client.delete_function(
           FunctionName=function_name,
           Qualifier=str(version)
       ) 
    except ClientError as e:
        print("Failed to delete version of", function_name, "version number", str(version))
        print(e)
def get_layer_versions(function_name):
    try:
        response = client.list_versions_by_function(
            FunctionName=function_name
        )
        return response['Versions']
    except ClientError as e:
        print('failed to get info for', function_name)
        print(e)

if __name__ == "__main__":
    print("Starting lambda update")
    with open("lambda_list.txt", "r") as text:
        lambda_list = text.read().splitlines()
        text.close()
    version_info_results = {}
    for lambda_function in lambda_list:
        print("Working with lambda", lambda_function)
        lambda_versions = get_layer_versions(lambda_function)
        lambda_version_list = []
        for version in lambda_versions:
            version_number = version['Version']
            if version_number == "$LATEST":
                pass
            else:
                lambda_version_list.append(int(version_number))
        for lambda_version in lambda_version_list:
            if lambda_version == max(lambda_version_list):
                print("This is the latest version skipping", str(lambda_version))
                pass
            else:
                print("Deleting version", lambda_version, "of", lambda_function)
                delete_function_version(lambda_function, lambda_version)
         

To use this function you first need to populate the text file with any Lambda function(s) that you want to evaluate and then execute the Python script.

I hope that this helps you or a coworker!

EDIT: Thanks to Alex Iliev for testing and finding some bugs in this code!

Links:
Github
Twitter


Posted

in

,

by

Comments

Leave a Reply