首页 > 解决方案 > 如何使用 python 查找 AMI 的 AWS 区域?

问题描述

我正在尝试在 Amazon Web Services 中为 ami 找到一个区域,我只知道 ami id,最好使用 python。怎么做?

标签: pythonamazon-web-servicesec2-ami

解决方案


这是片段

import boto3

ami_id = "HERE IS YOUR AMI ID"

client = boto3.client('ec2')
ec2 = boto3.resource('ec2')

regions = client.describe_regions()
for region in regions["Regions"]:
    region_name = region["RegionName"]
    print(region_name)
    try:
        image = ec2.Image(ami_id)
        response = image.describe_attribute(Attribute='description')
        print("Found ami in region {}".format(region_name))
        break
    except: 
        print("Ami doesnt exist in {} region".format(region_name))

推荐阅读