boto / boto3

AWS SDK for Python

Home Page:https://aws.amazon.com/sdk-for-python/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add short name for region in describe_regions() or describe_endpoints()

bgupta opened this issue · comments

Describe the feature

I find myself frequently using the shorthand abbreviations for regions that were popularized by availability zone "Zone IDs" e.g. use1 for us-east-1. I'd like a way to have this available in boto3.

References this ticket: #3869

The response

Use Case

I commonly use the short name in cost optimization datasets. The short names are also commonly embedded in pricing data, so being able to have that maintained in the boto3 library would be a great help.

Proposed Solution

Extend https://github.com/boto/botocore/blob/develop/botocore/data/endpoints.json to have another dictionary key as well as decribe_endpoints.

Other Information

As proposed in #3869 [DescribeAvailabilityZones] could be used. (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAvailabilityZones.html) but there is an issue.

The specific use case is that in FinOps one may only have access to the Payer account, which may not have access to all the regions that linked accounts do.

Because Boto3 is maintaining a list of regions, it should be fairly easy to maintain this once the initial lift is done to backport support for existing regions.

I am willing to submit a PR, as it would be very helpful to me. I took a look and since the regions dictionary is a nested dictionary, we could add "shortname" right next to "description", so it shouldn't be a breaking change.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1.34.44

Environment details (OS name and version, etc.)

Ubuntu 20.04.6 LTS

Thanks for the feature requests. However, the team does not plan to extend the functionality of the endpoints.json file (for more context on why, see: boto/botocore#3028 (comment) ).

However, I think using the SSM commands documented here will meet your use case: https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-global-infrastructure.html

For example using Boto3:

from pprint import pprint
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,
            'azs': cls._get_region_azs(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

    @classmethod
    def _get_region_azs(cls, region_code):
        azs = []
        for page in ssm.get_paginator('get_parameters_by_path').paginate(
            Path=f'/aws/service/global-infrastructure/regions/{region_code}/availability-zones'
        ):
            azs.extend(p['Value'] for p in page['Parameters'])

        return azs


pprint(Regions.get_regions())

The above code should return a list of dictionaries like this:

 {'azs': ['use1-az1',
          'use1-az2',
          'use1-az3',
          'use1-az4',
          'use1-az5',
          'use1-az6'],
  'code': 'us-east-1',
  'name': 'US East (N. Virginia)'},

I hope that helps. Functionality like this should be provided/updated by service APIs rather than Boto3 for better consistency and maintainability. If you have a request related to an API, you can create a new issue in our cross-SDK repository (https://github.com/aws/aws-sdk) and someone can reach out to the appropriate service team on your behalf.

Confirmed that SSM data has the info needed. (Thanks for the code, tested and it's a good starting place!) My current, internal solution uses three tiers of resolution.

  1. Local python dict with known regions
  2. Uses describeAZ to extract shortname from zoneid if unknown
  3. throw an exception if you are asked for a shortname for a region that can't be resolved via describeAZ (most likely due it not being enabled in the account where I make the call).

I'll replace the describeAZ code with SSM code!

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.