Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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') to ('N. California', 'N. Virginia', 'Oregon')?

I can get a list of AWS region codes with the following snippet:

 from boto3.session import Session
 s = Session()
 regions = s.get_available_regions('rds')
 print("regions:", regions)

$ python3 regions.py
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']

Is there an equivalent snippet that would give me the AWS region names?

like image 551
boardrider Avatar asked Apr 29 '19 18:04

boardrider


People also ask

How do I get AWS region in Python?

If you want to programmatically retrieve the AWS regions one of the best ways to do this is via Python boto3 package. In boto3, specifically the EC2 client, there is a function named describe_regions . This will retrieve the regions that are in AWS. See different uses of the describe_regions function below.

How do I find my AWS region?

To find your Regions using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . From the navigation bar, choose the Regions selector. Your EC2 resources for this Region are displayed on the EC2 Dashboard in the Resources section.

How do I find my default region name AWS?

If you signed up for an AWS account on or after May 17, 2017, the default Region when you access a resource from the AWS Management Console is US East (Ohio) (us-east-2); for older accounts, the default Region is either US West (Oregon) (us-west-2) or US East (N. Virginia) (us-east-1).

How do I specify AWS region?

Sign in to the AWS Management Console . Choose a service to go to that service's console. In the navigation bar, choose the name of the currently displayed Region. Then choose the Region to which you want to switch.


1 Answers

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.

like image 58
jogold Avatar answered Sep 20 '22 03:09

jogold