Python boto ec2 - how to wait for image to be created or executed

I am writing code to iterate over all available instances and create AMIs for them as shown below:

for reservation in reservations:
    ......
    ami_id = ec2_conn.create_image(instance.id, ami_name, description=ami_desc, no_reboot=True)

      

But how do you wait for the image to be created before starting to create the next image? Because I need to keep track of the status of each ami created.

I know I can get the state using:

image_status = get_image(ami_id).state

      

So, am I iterating over the list of created ami_ids and then fetching a state for each one? If so, what if the image is still waiting for me to read the state of the image? How can I tell if the image has failed to create?

Thank.

+3


source to share


4 answers


If I understand correctly, you want to initiate the call create_image

and then wait for the server-side operation to complete before moving on. To do this, you need to periodically check the EC2 service until the status of the image is available

(which means it succeeded) or failed

(which means it failed). The code will look something like this:



import time
...
image_id = ec2_conn.create_image(instance.id, ...)
image = ec2_conn.get_all_images(image_ids=[image_id])[0]
while image.state == 'pending':
    time.sleep(5)
    image.update()
if image.state == 'available':
    # success, do something here
else:
    # handle failure here

      

+6


source


For those using boto3 the following will work:

import boto3
import time
region = 'us-west-1'
client = boto3.client('ec2', region_name=region)
def is_image_available(image_id):
    try:
        available = 0
        while available == 0:
            print "Not created yet.. Gonna sleep for 10 seconds"
            time.sleep(10)
            image = client.describe_images(ImageIds=[image_id])
            if image['Images'][0]['State'] == 'available':
                available = 1
        if available == 1:
            print "Image is now available for use."
            return True
    except Exception, e:
        print e

      

With this function, you will be able to pass the image_id and get the status as true if available. You can use it in an if condition like this:



if is_image_available(image_id):
# Do something if image is available

      

Hope it helps.

+1


source


Boto now has a method wait_until_running

that lets you copy your own polling code:

http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Instance.wait_until_running

0


source


I will answer the same question.

Update the state variable of the image while it is not running. At any time, if the state is running, abort the loop. You can also check the completion or failure status.

image = ec2.Image(image_id)
if(image.state == 'pending'):
        print("Waiting for image to be available.")
        while(image.state != 'available'):
            image = ec2.Image(image_id)
        print("Image Available to use")

      

TIP: Don't wait for images to be available when you create them. Check for images available when instantiating from these images. This will save you a lot of time because many pending images will be able to transition to an available state at the same time.

If you are waiting for each image to appear, you are simply adding all the creation time to your program.

Hope it helps.

0


source







All Articles