You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boto3调用wait_until_exists等待AMI可用时报dict无该属性错误如何解决

错误原因

你调用ec2.create_image()时使用的是boto3的低级别Client接口,该接口的返回值是字典类型,本身不携带wait_until_exists方法,该方法是boto3高层级Resource接口下Image对象的专属方法。

解决方法

提供两种可行实现方案:

方案1:改用EC2 Resource接口实现

import boto3

# 初始化EC2 Resource对象,而非Client
ec2 = boto3.resource('ec2')

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

# 直接调用Image对象的wait_until_exists方法,等待状态变为available
image.wait_until_exists(
    Filters=[
        {
            'Name': 'State',
            'Values': ['available']
        }
    ]
)

方案2:继续使用Client接口,调用内置waiter

如果你不想修改现有Client的使用逻辑,可以直接调用Client自带的image_available等待器实现同等效果:

import boto3

ec2 = boto3.client('ec2')

resp = ec2.create_image(
    Name='test2021-2',
    InstanceId='i-f966b43b',
    Description='This is a test with the latest AMI'    
)
# 获取创建好的AMI ID
ami_id = resp['ImageId']

# 初始化AMI可用等待器
waiter = ec2.get_waiter('image_available')
# 传入AMI ID等待状态变为available
waiter.wait(
    ImageIds=[ami_id]
)

内容的提问来源于stack exchange,提问作者dahwild

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 16:45:04