This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoutdatedAMIs.py
55 lines (39 loc) · 1.78 KB
/
outdatedAMIs.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
48
49
50
51
52
53
54
55
#!/usr/bin/python3
import concurrent.futures
import datetime
import json
import boto3
import subprocess
def writeIntoFile(filename, stdout, method='w+'):
with open(filename, method) as f: f.write(stdout)
def ec2Client(service):
client = boto3.client(service)
return(client)
def describeAMIsDuration(ec2Client, dateToday, fileName, daysToCheck):
descImg = ec2Client.describe_images(Owners = ['self'])
images = json.loads(json.dumps(descImg))['Images']
for instanceDetails in images:
imageId = instanceDetails['ImageId']
instanceName = instanceDetails['Name']
ebsVolumeEncryption = instanceDetails['BlockDeviceMappings'][0]['Ebs']['Encrypted']
creationDate = instanceDetails['CreationDate']
date = creationDate.split("T")[0].split("-")
year, month, day = int(date[0]), int(date[1]), int(date[2])
amiAgeInDays = str(dateToday - datetime.datetime(year, month, day)).split(" ")[0]
if ":" in amiAgeInDays:
amiAgeInDays = 0
print("{:<3} ~ {} ~ {} ~ {}".format(amiAgeInDays, imageId, instanceName, str(ebsVolumeEncryption)))
if int(amiAgeInDays) > daysToCheck:
writeIntoFile(fileName, stdout="{},{},{},{}\n".format(amiAgeInDays, imageId, instanceName, str(ebsVolumeEncryption)), method='a+')
def main():
daysToCheck = 180
service = 'ec2'
dateToday = datetime.datetime.today()
fileName = "EC2_Outdated_AMIs.csv"
print(f"[~] Grabbing List of AMIs (be patient) -- Checking with respect to '{daysToCheck} days'\n")
writeIntoFile(fileName, stdout="Age (days old),EC2 Instance ID,EC2 Instance Name,EBS Volume Encryption\n", method='w+')
client = ec2Client(service)
images = describeAMIsDuration(client, dateToday, fileName, daysToCheck)
print("\n[~] Script execution completed!\n[~] Data written in {}".format(fileName))
if __name__ == '__main__':
main()