I need to create a list of instances, the AMI that was used to create it, and the creation date of the AMI.
This is the Python (boto3) code I am using:
import boto3
import datetime
import sys
profile = (sys.argv[1])
boto3.setup_default_session(profile_name=profile)
ec2 = boto3.resource('ec2')
instances = ec2.instances.all()
images = ec2.images.filter(Owners=['self'])
for i in instances:
a = i.id
b = i.state['Name']
c = i.instance_type
if i.platform == 'windows':
d = i.platform
else:
d = 'linux'
e = i.private_ip_address
f = i.image_id
g = i.image.creation_date.strftime("%Y-%m-%d %H:%M:%S")
print('{} {} {} {} {} {}'.format(a, b, c, d, e, f, g))
But this is the error I am getting; pertaining to the creation_date call:
Traceback (most recent call last): File "C:\bin\TestAMI.py", line 23, in g = i.image.creation_date.strftime("%Y-%m-%d %H:%M:%S") File "C:\Python27\lib\site-packages\boto3\resources\factory.py", line 345, in property_loader return self.meta.data.get(name) AttributeError: 'NoneType' object has no attribute 'get'
How can get the creation date of the available AMIs in use?