MattoTodd
MattoTodd

Reputation: 15219

Using Boto to determine if an AWS AMI is available

I am using Boto to create an AMI of one of my EC2 boxes, and then I would like to spin up more boxes with that AMI, but the run_instances command is barking that my AMI is not available yet.

How can I use boto to query aws to find out when my ami is ready?

The EC2 connection supports a method to get_image

But the Image does not have any sort of status attribute

Upvotes: 4

Views: 5549

Answers (4)

rakeshz
rakeshz

Reputation: 377

I can write a simple function using boto3

def check_ami_exists(ami_id, region): 
  client = boto3.client('ec2', region_name = region)
  response = client.describe_images()
  for image in response['Images']:
    if ami_id == image['ImageId']:
      return True
  return False

Upvotes: 0

Ezos
Ezos

Reputation: 117

I used the above method but it took a little while for me to figure it out. Not a python person but here is what I did. Hope it helps someone.

#EC2 Connection
conn = boto.ec2.connect()

image_status = conn.get_all_images(image_ids='ami-XXX')[0]
image_state = image_status.state
print image_state

Upvotes: -1

Okezie
Okezie

Reputation: 5118

In addition to "pending" and "available" there is also the "failed" state. This is the full set of AMI states available.

Upvotes: 1

MattoTodd
MattoTodd

Reputation: 15219

a quick dir of Image led me to Image.state with values like "pending" and "available"

Upvotes: 5

Related Questions