all blog posts


Retrieving all Region Codes and Names with Boto3

For the CloudBanshee pricing tools we need to always have an up-to-date list of regions available. To this end, we have a periodically running lambda that fetches the regions and their names from SSM.

As described in the blog post Using the EC2 Price List API, the pricing API accepts only the long name of regions as input. However, there is no easy API call to retrieve all long names and match them to the short api codes, for example [{'name': 'US West (Oregon)', 'code': 'us-west-2'}].

In this blog post we will show how to use SSM and Boto3 to retrieve all long names and short codes and return them in a single array. Without further ado, here's all you need:

import boto3

ssm = boto3.client('ssm')


class Regions:
    @classmethod
    def get_regions(cls):
        short_codes = cls._get_region_short_codes()

        regions = [{
            'name': cls._get_region_long_name(sc),
            'code': sc
        } for sc in short_codes]

        regions_sorted = sorted(
            regions,
            key=lambda k: k['name']
        )

        return regions_sorted

    @classmethod
    def _get_region_long_name(cls, short_code):
        param_name = (
            '/aws/service/global-infrastructure/regions/'
            f'{short_code}/longName'
        )
        response = ssm.get_parameters(
            Names=[param_name]
        )
        return response['Parameters'][0]['Value']

    @classmethod
    def _get_region_short_codes(cls):
        output = set()
        for page in ssm.get_paginator('get_parameters_by_path').paginate(
            Path='/aws/service/global-infrastructure/regions'
        ):
            output.update(p['Value'] for p in page['Parameters'])

        return output

To retrieve the regions in your project, import the class and use it like this:

for region in Regions.get_regions():
    print(region)

The Region class does three things:

  1. It fetches all short codes from SSM in the _get_region_short_codes() function.
  2. For every short code, it fetches the corresponding long name from SSM in _get_region_long_name().
  3. It sorts the dictionary by name.

The result, when executed as print(Regions.get_regions()):

[
    {
        "name": "AWS GovCloud (US-East)",
        "code": "us-gov-east-1"
    },
    {
        "name": "AWS GovCloud (US-West)",
        "code": "us-gov-west-1"
    },
    {
        "name": "Asia Pacific (Hong Kong)",
        "code": "ap-east-1"
    },
    {
        "name": "Asia Pacific (Mumbai)",
        "code": "ap-south-1"
    },
    {
        "name": "Asia Pacific (Osaka-Local)",
        "code": "ap-northeast-3"
    },
    {
        "name": "Asia Pacific (Seoul)",
        "code": "ap-northeast-2"
    },
    {
        "name": "Asia Pacific (Singapore)",
        "code": "ap-southeast-1"
    },
    {
        "name": "Asia Pacific (Sydney)",
        "code": "ap-southeast-2"
    },
    {
        "name": "Asia Pacific (Tokyo)",
        "code": "ap-northeast-1"
    },
    {
        "name": "Canada (Central)",
        "code": "ca-central-1"
    },
    {
        "name": "China (Beijing)",
        "code": "cn-north-1"
    },
    {
        "name": "China (Ningxia)",
        "code": "cn-northwest-1"
    },
    {
        "name": "EU (Frankfurt)",
        "code": "eu-central-1"
    },
    {
        "name": "EU (Ireland)",
        "code": "eu-west-1"
    },
    {
        "name": "EU (London)",
        "code": "eu-west-2"
    },
    {
        "name": "EU (Paris)",
        "code": "eu-west-3"
    },
    {
        "name": "EU (Stockholm)",
        "code": "eu-north-1"
    },
    {
        "name": "South America (Sao Paulo)",
        "code": "sa-east-1"
    },
    {
        "name": "US East (N. Virginia)",
        "code": "us-east-1"
    },
    {
        "name": "US East (Ohio)",
        "code": "us-east-2"
    },
    {
        "name": "US West (N. California)",
        "code": "us-west-1"
    },
    {
        "name": "US West (Oregon)",
        "code": "us-west-2"
    }
]

Shout out to Ben Bridts, a fellow AWS APN Ambassador who pointed me in the right direction. As always, reach out to me on Twitter if you have any remarks or questions.


Related blog posts


all blog posts