AWS Lambda / Pyhton. Delete Snapshots after days with filter snapshots by tag

Viewed 27

try to run a pyhton script in AWS Lambda. That use the "aws ec2 describe-snapshots" function to list all snapshots and i want filter the list by tag for. In my opnion is the best way to filter the first output "ec2.snapshots.filter" with tags, my ideas to filter this in pyhton doens't work. The "ec2.snapshots.filter" have a function to filter this by tag (https://aws.amazon.com/de/premiumsupport/knowledge-center/ebs-snapshots-list/)

My idea was to edit this line at the end.

snaps = ec2.snapshots.filter(OwnerIds=[accountId])(Filters=[{'Name': 'tag:NoDelete', 'Values': ['no']}])

Fullcode

import json, boto3, datetime

def lambda_handler(event, context):

# Do not delete these snaps
snap_ignore = ['snap-12718f7046cc2add6','snap-0e738d4c47b2d6e14']
#snap_ignore_tag = [{'Key':NoDelete}]
# Enter the AWS account number here
accountId = '123456789'
# Delete snapshots older than diff_time days   
diff_time = 30
# Constant string
no_snap = 'No snaps were found for clean up.' 
# Constant string
skip_str = 'was skipped because it is in use.'
# Leave it empty
mail_body = ''

 
ec2 = boto3.resource('ec2')
sns = boto3.client('sns')

# Get all the snaps from the account
snaps = ec2.snapshots.filter(OwnerIds=[accountId])(Filters=[{'Name': 'tag:NoDelete', 'Values': ['no']}])
# Get the current time
now_time = datetime.datetime.now().date()
# Iterate through the snaps
for snap in snaps:
    # Check for protected snaps, e.g the ones that we have to skip and ignore
    if (snap.id in snap_ignore): 
        continue
    #if ('snap.tag' in snap_ignore_tag): 
    #    continue
    # Get the creation time of the snap
    snap_time = snap.start_time.date()
    # Get the current time
    now_time = datetime.datetime.now().date()
    # Calculate the difference
    snapage_time = (now_time - snap_time).days
    # If the creation time is older than the required difference
    if(snapage_time > diff_time):
        try:
            # Delete the snap
            # snap.delete(snap.id)
            # Print the snap that will be deleted
            print(snap.id, snap.description,'created on',snap_time,'was deleted')
            mail_body = mail_body + snap.id + snap.description + ' created on ' + str(snap_time) + ' was deleted.\n' 'ignore delete by Tags' + str(snap_ignore_tag_key) + '\n'
        # Catch an exception if the snap is in use
        except:
            print(snap.id, skip_str)
            mail_body = mail_body + snap.id + ' ' + skip_str + '\n'
            continue
if (mail_body == ''):
    mail_body = no_snap
response = sns.publish(
    TopicArn = 'arn:aws:sns:eu-central-1:' + accountId + ':SnapshotDeleteNotification',
    Message = mail_body,
    Subject = 'Snapshot Clean up ' + str(now_time)
)

Have anyone a idea?

1 Answers

The problem is on this line:

    snaps = ec2.snapshots.filter(OwnerIds=[accountId])(Filters=[{'Name': 'tag:NoDelete', 'Values': ['no']}])

It should be:

    snaps = ec2.snapshots.filter(OwnerIds=[accountId],Filters=[{'Name': 'tag:NoDelete', 'Values': ['no']}])
Related