34

As AWS expands and adds new regions, I'd like to have my code automatically detect that. Currently, the "Select your region" is hard coded but I would like to parse the following for just the RegionName.

import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions()
print(regions)

My output is JSON like so:

{'Regions': [{'Endpoint': 'ec2.ap-south-1.amazonaws.com', 'RegionName': 'ap-south-1'}, {'Endpoint': 'ec2.eu-west-1.amazonaws.com', 'RegionName': 'eu-west-1'}, {'Endpoint': 'ec2.ap-southeast-1.amazonaws.com', 'RegionName': 'ap-southeast-1'}]}

I've trimmed off the repeating data and the ResponseMetadata for the sake of space.

How can I parse the RegionName into a list?

Shawn
  • 561
  • 1
  • 5
  • 12

2 Answers2

51

In addition to Frédéric's answer, you can also get known regions for each service without making any service calls. I will caution you, however, that since this is pulling from botocore's local models rather than hitting an endpoint, it will not always be exhaustive since you need to update botocore to update the list.

from boto3.session import Session

s = Session()
dynamodb_regions = s.get_available_regions('dynamodb')

Additionally, you are not restricted to the regions in this list. If you are using an older version of botocore you can still use new regions by specifying them. They just won't appear in this list.

Jordon Phillips
  • 14,963
  • 4
  • 35
  • 42
  • This option was great for me - I needed to make something in a library that makes no service calls. Thanks for the pointing out that distinction! – killthrush Jan 04 '19 at 21:37
  • Just to add: `get_available_regions('ec2', 'aws-cn')` can get you China mainland regions. The same goes for gov regions. – dz902 Jun 15 '21 at 14:55
  • This will give you the regions that boto3 knows about, not the regions that are actually enabled within the AWS account. – Wayne Workman Mar 29 '23 at 21:33
41

The following will return you the RegionName and Endpoint for each region.

# List all regions
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
Community
  • 1
  • 1
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • 6
    `client = boto3.client('ec2')` fails with the error "botocore.exceptions.NoRegionError: You must specify a region." – tadasajon Mar 31 '17 at 17:07
  • 1
    There are multiple ways to set the region (aws has good doc for that) but you can get straight from `client = boto3.client('ec2', region_name='us-west-2')` – Frederic Henri Mar 31 '17 at 17:25
  • how can i give multiple regions names ? can i give it in this way `client = boto3.client('ec2',region_name='us-west-2' && 'us-west-1')` @FrédéricHenri – vishal Jan 09 '18 at 09:41
  • @vishal.k you cannot have a client that spans multiple region, you would need to create one client for each region you plan to work on – Frederic Henri Jan 09 '18 at 09:43
  • I also wanted to avoid making service calls (and to use the the ec2 client at all, since I wanted to know the regions for SSM specifically), but this turned out to be the only solution that worked, since other methods also listed *opt-in* regions (like `ap-east-1` or `me-south-1`), which resulted in an `UnrecognizedClientException` when I made subsequent boto calls targeting those regions. – Phil Gref Oct 11 '19 at 23:06
  • This will give you the regions that boto3 knows about, not the regions that are actually enabled within the AWS account. – Wayne Workman Mar 29 '23 at 21:33
  • Use describe_regions(AllRegions=True) if you want to get all AWS regions, not only the ones enabled in your account. – Anatoly Alekseev Apr 30 '23 at 18:45