Search code examples
pythonpython-3.xboto3botoamazon-ami

Boto3 wait_until_exists for available image (object has not attribute)


I'm creating an AMI and I want to wait until the AMI is 'available'. I'm using the following code:

import json
import boto3
import os
import logging

ec2 = boto3.client('ec2')

images = ec2.create_image(
    Name='test2021-2',
    InstanceId='i-f966b43b',
    #DryRun=True,
    Description='This is a test with the latest AMI'    
)

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

But I get the following error: AttributeError: 'dict' object has no attribute 'wait_until_exists'

The waiter is not waiting until the AMI is available.


Solution

  • You are missing one step. First you have to create the waiter using get_waiter:

    images = ec2.create_image(
        Name='test2021-2',
        InstanceId='i-f966b43b',
        #DryRun=True,
        Description='This is a test with the latest AMI'    
    )
    
    waiter = ec2.get_waiter('image_exists')
    
    # you may need to fill out the proper arguments for `wait`
    
    waiter.wait(
        ImageIds=[
            'string',
        ], 
        Filters=[
            {
                'Name': 'state',
                'Values': [
                    'available',
                ]
            },
        ],
    )