代码之家  ›  专栏  ›  技术社区  ›  Lech Migdal

如何用python找到ami的aws区域?

  •  0
  • Lech Migdal  · 技术社区  · 7 年前

    我试图在AmazonWeb服务中为AMI找到一个区域,在那里我只知道AMI ID,最好是使用Python。怎么做?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Lech Migdal    7 年前

    这是片段

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