Search code examples
pythonamazon-web-servicesec2-ami

How to find AWS region of AMI with python?


I am trying to find a region for an ami in Amazon Web Services, where I know only ami id, preferably with python. How to do it?


Solution

  • Here is the snippet

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