-
Notifications
You must be signed in to change notification settings - Fork 7
/
clean_orphaned_snapshots.py
37 lines (29 loc) · 1.3 KB
/
clean_orphaned_snapshots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import boto3
# set your api username
profile_name = 'aws-api-user'
def clean_snapshot(client, snap_id):
return 1 if client.delete_snapshot(SnapshotId=snap_id)['ResponseMetadata']['HTTPStatusCode'] == 200 else 0
def main():
"""
Detect orphaned snapshots from an EC2 account. Requires configured aws profile on machine
http://docs.aws.amazon.com/cli/latest/reference/configure/
"""
aws_session = boto3.Session(profile_name=profile_name)
ec2 = aws_session.client('ec2')
alive_ami = {}
for ami in ec2.describe_images(Owners=['self'])['Images']:
id = ami['ImageId']
if id not in alive_ami.keys():
alive_ami[id] = []
alive_ami[id].extend(list([snap['Ebs']['SnapshotId'] for snap in ami['BlockDeviceMappings']]))
# list of attached to AMI snapshots
alive_snaps = list([item for sublist in alive_ami.values() for item in sublist])
snap_list = list([s['SnapshotId'] for s in ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']])
orphaned_snaps = list(set(snap_list) - set(alive_snaps))
if orphaned_snaps:
print('Orphaned snapshots: %s' % orphaned_snaps)
[clean_snapshot(ec2, snap_id) for snap_id in orphaned_snaps]
else:
print('Seems that all snapshots are alright')
if __name__ == '__main__':
main()