Home > front end >  How can I get the AMI Id through the AMI name with boto3?
How can I get the AMI Id through the AMI name with boto3?

Time:09-10

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!

CodePudding user response:

response = ec2.create_image(InstanceId="i-f234nr1kdd", Name="abc")
response['ImageId']

CodePudding user response:

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"]

  • Related