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

boto3在ap-east-1调用describe_instance_status返回空列表问题咨询

问题:ap-east-1区域使用boto3查询运行中EC2实例返回空列表

我使用最新版本的boto3,在ap-east-1区域查询状态为"running"的EC2实例,客户端已指定该区域,代码如下:

try:
    running_instances = ec2.describe_instance_status(
        Filters=[
            {
                "Name": "instance-state-name",
                "Values": ["running"],
            },
        ],
        InstanceIds=<list of instance_ids>,
    )
except ClientError as e:
    <catch exception>

明明该区域有运行中的EC2实例,但代码返回空列表,且这段代码在其他区域都能正常运行。用相同筛选条件的AWS命令却能正确返回结果:

aws ec2 describe-instance-status --region ap-east-1 --filter Name="instance-state-name",Values="running" --instance-id <list of instance ids>

请问针对ap-east-1区域,使用boto3时我遗漏了什么配置?有没有适配该区域的特定boto3版本?


解决方法

  • 手动指定区域端点:ap-east-1(香港区域)的端点信息可能在部分环境中未被自动识别,手动指定端点试试:

    ec2 = boto3.client('ec2', region_name='ap-east-1', endpoint_url='https://ec2.ap-east-1.amazonaws.com')
    

    指定后再执行查询,看是否能返回预期结果。

  • 核对实例ID的区域归属:确认传入的<list of instance_ids>里的实例确实属于ap-east-1区域,且当前状态为running。如果不小心传入了其他区域的实例ID,自然会返回空。

  • 验证权限与区域激活状态:确保AWS账号已经激活了ap-east-1区域,同时boto3使用的IAM凭证拥有该区域的ec2:DescribeInstanceStatus权限。另外检查boto3的凭证是否和AWS CLI一致,避免用了不同的配置文件导致权限差异。

  • 确保boto3和botocore版本足够新:ap-east-1在2019年就已正式上线,boto3版本1.10.0及以上就支持该区域。可以用pip show boto3查看当前版本,若版本过低则升级:

    pip install --upgrade boto3 botocore
    

    注意要同时升级botocore,因为区域的端点配置信息是由botocore维护的。

  • 调整查询逻辑:尝试先不指定InstanceIds参数,查询所有running状态的实例,再在结果中筛选目标ID:

    # 先查所有running实例
    running_instances = ec2.describe_instance_status(
        Filters=[
            {
                "Name": "instance-state-name",
                "Values": ["running"],
            },
        ]
    )
    # 筛选目标实例
    target_instances = [inst for inst in running_instances['InstanceStatuses'] if inst['InstanceId'] in <list of instance_ids>]
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:40:31