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

前端 未结 3 794
北恋
北恋 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: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']
    

提交回复
热议问题