Issue
I'm trying to find the snapshots that don't have a certain tag.
For snapshots, I want all snapshots that don't have the Do-Not-Delete
tag. No matter what is the value of a tag.
This is what I'm doing now:
snaps_to_remove = ec2_client.describe_snapshots(OwnerIds=account_ids)
for snap in snaps_to_remove['Snapshots']:
# Remove all snapshots with the tag Do-Not-Delete functionality goes here
print(snap)
I don't think if there's a filter for negative comparison based. What is the correct way to loop through and get filter out the list with the specific tags?
Solution
If the snapshot contains Tags and one of the tags has a Key of 'Do-Not-Delete', skip the snapshot:
snaps_to_remove = ec2_client.describe_snapshots(OwnerIds=account_ids)
for snap in snaps_to_remove['Snapshots']:
# Skip snapshots with a Do-Not-Delete tag
if 'Tags' in snap and [tag for tag in snap['Tags'] if tag['Key'] == 'Do-Not-Delete']:
continue
print(snap)
Answered By - John Rotenstein