如何使用python查找AMI的AWS区域?

时间:2018-06-25 10:14:26

标签: python amazon-web-services ec2-ami

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

2 个答案:

答案 0 :(得分:1)

这是代码段

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))

答案 1 :(得分:0)

for image_id in ['image_id_1', 'image_id_2', ...]:
    for region in regions:
        ec2 = boto3.resource('ec2', region_name = region)
        try:
            response = ec2.images.filter(ImageIds=[image_id])
            print(region, list(response)[0].name)
        except botocore.exceptions.ClientError as exc:
            if 'InvalidAMIID.NotFound' in str(exc):
                pass
            else:
                raise exc