-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete-2days-old-ami-and-snapshot.py
47 lines (43 loc) · 1.18 KB
/
Delete-2days-old-ami-and-snapshot.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
38
39
40
41
42
43
44
45
46
47
import collections
import datetime
import sys
import boto3
s = boto3.Session(profile_name='xxxxxxxxxxx')
ec = s.client('ec2', region_name='xxxxxxxxxxxxxx')
d = datetime.today() - timedelta(days=days_to_subtract)
def delete_ami(ami_id):
ec.deregister_image(ImageId=image["ImageId"])
return "Deleted AMI"
def deleted_snapshot(Snapshot_id):
ec.delete_snapshot(SnapshotId=id)
return "Deleted Snapshot"
images = ec.describe_images(
Filters=[
{
'Name':'description',
'Values': ['* ' +'2020-05-11']
},
{
'Name': 'tag:backup',
'Values': ['dailyami']
}
]
)["Images"]
count = 0
im = []
for image in images:
print(image["ImageId"])
print(image["BlockDeviceMappings"])
im.append(image["ImageId"])
delete_ami(image["ImageId"])
sn = []
for i in range(0,len(image["BlockDeviceMappings"])):
try:
sn.append(image["BlockDeviceMappings"][i]["Ebs"]["SnapshotId"])
id = image["BlockDeviceMappings"][i]["Ebs"]["SnapshotId"]
deleted_snapshot(id)
except:
pass
print(sn)
print(im)
print(len(im))