Warm tip: This article is reproduced from serverfault.com, please click

Boto3 wait_until_exists for available image not waiting

发布于 2019-12-19 11:45:20

Morning all,

I'm running into an interesting issue, I'm not sure if it's a bug or just an incorrect application from my side (likely the latter):

I am creating an image and then want to wait for the image to become 'available'. I am using the following code sample to test out the commands and see how they work.

import boto3
from datetime import datetime

ec2 = boto3.resource('ec2', region_name='eu-north-1');
instance = ec2.Instance('<some id value>')
create_ami = instance.create_image(Name='Test8') ;

try:
    print(datetime.now())
    create_ami.wait_until_exists({'Name': 'image-id', 'Values':[create_ami]},{'Name':'state', 'Values':['available']});
    print('success: ami now available');
    print(datetime.now())
except Exception as e:
    print (e)

Problem is that it appears as though the wait_until_exists is not waiting. The timestamps returned are almost identical and when I look in the AWS console I can see the AMI 'pending'. Not sure why this is happening - I thought the code would stay with the wait_until_exists until the state was 'available' and then continue to process. I was expecting the second timestamp to pop up after the wait_unti_exists validated as true.

Have I misunderstood the command?

Many thanks, Ben

Questioner
user7925487
Viewed
0
Vikyol 2019-12-19 22:30:17

It looks like the filter is not correctly set. Remove the first one and try again.

create_ami.wait_until_exists(Filters=[{'Name': 'state', 'Values': ['available']}])