How can I get the AMI Id through the AMI name with boto3?

Question:

I’m trying to automate the creation of an autoscaling group Cloudformation Template using an EC2 instance already deployed and running so, for making an exact copy I need to have the AMI of the current instance. In order to get the Image I run the following script:

ec2 = boto3.client('ec2')
ec2.create_image(InstanceId="i-f234nr1kdd", Name="abc")

and it works, now what I need is to get the AMI Id using boto3 and the AMI name for pass the AMI Id to the cloudformation template.

I just need to get the AMI id value programmatically with the AMI name as parameter in order to pass the AMI Id to the part of my code that generates the Cloudformation template

Thanks since now!

Asked By: Juanpa1103

||

Answers:

response = ec2.create_image(InstanceId="i-f234nr1kdd", Name="abc")
response('ImageId')
Answered By: Goffredo Bosco

The docs here: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image indicate that the id of the AMI is in the dictionary returned by the method call.

It should be in response["ImageId"]

Answered By: brunson