How to get the Region Name for AWS Price List API using Python and Boto3

When using the AWS Price List API, one of the most used attribute is the location attribute. This attribute is used to filter the region of your target resource.

Unfortunately, the location attribute is not in the region code that we use when programming in Python and Boto3, like us-east-1. Instead, it is descriptive like US East (N. Virginia). This is not readily available in Boto3 client or resource.

Below is the python function get_region_name, this converts the region code, eu-west-2, to the region name that AWS Price List API attribute location will accept, EU (London).

import json
from pkg_resources import resource_filename

def get_region_name(region_code):

    endpoint_file = resource_filename('botocore', 'data/endpoints.json')

    with open(endpoint_file, 'r') as f:
        endpoint_data = json.load(f)

    region_name = endpoint_data['partitions'][0]['regions'][region_code]['description']

    region_name = region_name.replace('Europe', 'EU')

    return region_name


if __name__ == "__main__":
    region_code = 'ap-south-1'
    region_name = get_region_name(region_code)
    print(region_name)

    region_code = 'eu-west-2'
    region_name = get_region_name(region_code)
    print(region_name)

    region_code = 'us-east-2'
    region_name = get_region_name(region_code)
    print(region_name)

Output

Asia Pacific (Mumbai)
EU (London)
US East (Ohio)
Continue reading How to get the Region Name for AWS Price List API using Python and Boto3