Is there now a way to get “AWS region names” in boto3?

前端 未结 3 793
北恋
北恋 2021-01-18 11:40

Is there now a way in boto3 to convert AWS region codes to AWS region names, e.g to convert (\'us-west-1\', \'us-east-1\', \'us-west-2\'

相关标签:
3条回答
  • 2021-01-18 12:10

    This is the solution I arrived at, which shows all the AWS/EC2 region IDs and names:

    $ cat so_regions2.py
    import boto3
    
    region = "us-east-1"
    print("Using region:", region)
    
    ec2 = boto3.client("ec2", region_name=region)
    ec2_responses = ec2.describe_regions()
    ssm_client = boto3.client('ssm', region_name=region)
    for resp in ec2_responses['Regions']:
        region_id = resp['RegionName']
        tmp = '/aws/service/global-infrastructure/regions/%s/longName' % region_id
        ssm_response = ssm_client.get_parameter(Name = tmp)
        region_name = ssm_response['Parameter']['Value'] 
        print ("region_id:",region_id,"region_name:",region_name)
    
    $ python3 so_regions2.py
    Using region: us-east-1
    region_id: eu-north-1 region_name: EU (Stockholm)
    region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
    region_id: eu-west-3 region_name: EU (Paris)
    region_id: eu-west-2 region_name: EU (London)
    region_id: eu-west-1 region_name: EU (Ireland)
    region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
    region_id: ap-northeast-1 region_name: Asia Pacific (Tokyo)
    region_id: sa-east-1 region_name: South America (Sao Paulo)
    region_id: ca-central-1 region_name: Canada (Central)
    region_id: ap-southeast-1 region_name: Asia Pacific (Singapore)
    region_id: ap-southeast-2 region_name: Asia Pacific (Sydney)
    region_id: eu-central-1 region_name: EU (Frankfurt)
    region_id: us-east-1 region_name: US East (N. Virginia)
    region_id: us-east-2 region_name: US East (Ohio)
    region_id: us-west-1 region_name: US West (N. California)
    region_id: us-west-2 region_name: US West (Oregon)
    $ 
    
    0 讨论(0)
  • 2021-01-18 12:15

    AWS just released a feature that allows to query for AWS Regions, Endpoints, and More Using AWS Systems Manager Parameter Store.

    Using this feature and boto3, you can do something like this:

    import boto3
    
    client = boto3.client('ssm')
    
    response = client.get_parameter(
        Name='/aws/service/global-infrastructure/regions/us-west-1/longName'
    )
    
    region_name = response['Parameter']['Value'] # US West (N. California)
    

    To get all available regions you can first use get_parameters_by_path() using the following path /aws/service/global-infrastructure/regions.

    Note: even though this is public data, it requires proper IAM permissions.

    0 讨论(0)
  • 2021-01-18 12:21

    Edit: As mentioned by @jogold, with the recent launch of Query for AWS Systems Manager Parameter Store, I'd advise using that to directly query from AWS instead of the custom script in my answer.


    As per the boto3 docs, there is no native functionality for describing the colloquial names of the regions.

    Here's a small script with a function, convertRegionCodesToNames(), that takes a list of valid region IDs and converts them to their common names. Add error handling as needed for invalid inputs, zero length arrays, or other possible responses returned by boto3.

    # replace `regions` variable with the output from the get_available_instances() response
    
    regions = ['ap-northeast-1', 'ap-northeast-2', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']
    
    def convertRegionCodesToNames(regions):
        # static dict of all region key-value pairs
        region_dict = {
          "us-east-1": "N. Virginia",
          "us-east-2": "Ohio",
          "us-west-1": "N. California",
          "us-west-2": "Oregon",
          "ca-central-1": "Central",
          "eu-west-1": "Ireland",
          "eu-central-1": "Frankfurt",
          "eu-west-2": "London",
          "eu-west-3": "Paris",
          "eu-north-1": "Stockholm",
          "ap-northeast-1": "Tokyo",
          "ap-northeast-2": "Seoul",
          "ap-southeast-1": "Singapore",
          "ap-southeast-2": "Sydney",
          "ap-south-1": "Mumbai",
          "sa-east-1": "São Paulo",
          "us-gov-west-1": "US Gov West 1",
          "us-gov-east-1": "US Gov East 1"
        } 
    
        for i in range(len(regions)):
            regions[i] = region_dict[regions[i]]
    
        return regions
    
    converted_regions = convertRegionCodesToNames(regions)
    print("regions:", converted_regions) 
    

    Once added, running $ python3 regions.py will output:

    regions: ['Tokyo', 'Seoul', 'Mumbai', 'Singapore', 'Sydney', 'Central', 'Frankfurt', 'Ireland', 'London', 'Paris', 'São Paulo', 'N. Virginia', 'Ohio', 'N. California', 'Oregon']
    
    0 讨论(0)
提交回复
热议问题