Boto3: List of available regions for a given service

Created on 21 Jul 2015  路  12Comments  路  Source: boto/boto3

In boto there was a call to get a list of regions available to a given service. i.e. boto.cloudformation.regions(). I don't see any sort of equivalent call in boto3. Am I missing something or is this related to https://github.com/boto/botocore/issues/339?

enhancement feature-request

Most helpful comment

import boto3
boto3.session.Session().get_available_regions('ec2')

All 12 comments

What I usually do for this is use a client (ec2c = boto3.client('ec2'))and run describe_availability_zones like so:

availZones = []
for zone in ec2c.describe_availability_zones()['AvailabilityZones']:
    if zone['State'] == 'available':
        availZones.append(zone['ZoneName'])

@SamCyanide thanks. That seems straightforward enough. It would be nice to have something similar provided.

I'll close this since since I can make that work.

That only works for EC2 because the service itself provides a request to return all availability zones. That doesn't help you if you want to find all regions for another service, e.g. CloudFormation or Lambda.

Yep @garnaat is correct. The describe_regions call only applies for EC2.

Being able to list regions for a specific service is something we want to add support for. Thus, reopening the issue and labeling it as a feature request.

In the meantime, I usually use this bit of documentation: http://docs.aws.amazon.com/general/latest/gr/rande.html to figure out if a service is available in a specific region.

This is important for monitoring purposes in a company which is growing fast and might use a new region after a few months. If this feature is implemented, it will help reduce the manual work involved in updating the region list often.

:+1:

We need this Feature also for our account configuration scripts.

I have written a blog post on this. Someone might find it useful..
https://inquisitiveindianspeaks.wordpress.com/2015/11/03/get-all-aws-regions-using-aws-cli-on-python/

@shyampsunder2003 you don't need a subprocess...

import boto3
from pprint import pprint
ec2c = boto3.client('ec2')
pprint(ec2c.describe_regions())

returns

{'Regions': [{'Endpoint': 'ec2.eu-west-1.amazonaws.com',
              'RegionName': 'eu-west-1'},
             {'Endpoint': 'ec2.ap-southeast-1.amazonaws.com',
              'RegionName': 'ap-southeast-1'},
             {'Endpoint': 'ec2.ap-southeast-2.amazonaws.com',
              'RegionName': 'ap-southeast-2'},
             {'Endpoint': 'ec2.eu-central-1.amazonaws.com',
              'RegionName': 'eu-central-1'},
             {'Endpoint': 'ec2.ap-northeast-2.amazonaws.com',
              'RegionName': 'ap-northeast-2'},
             {'Endpoint': 'ec2.ap-northeast-1.amazonaws.com',
              'RegionName': 'ap-northeast-1'},
             {'Endpoint': 'ec2.us-east-1.amazonaws.com',
              'RegionName': 'us-east-1'},
             {'Endpoint': 'ec2.sa-east-1.amazonaws.com',
              'RegionName': 'sa-east-1'},
             {'Endpoint': 'ec2.us-west-1.amazonaws.com',
              'RegionName': 'us-west-1'},
             {'Endpoint': 'ec2.us-west-2.amazonaws.com',
              'RegionName': 'us-west-2'}],
 'ResponseMetadata': {'HTTPStatusCode': 200,
                      'RequestId': '123456789-1234-1234-1234-123456789'}}

But this is only for EC2, not for all other services

I create me a helper for this Issue.

import boto3
boto3.session.Session().get_available_regions('ec2')

looks like the waf list is empty, but I was able to use EFS as an analagous list, as they appear to be fairly similar

>>> boto3.session.Session().get_available_regions('waf')
[]
>>> boto3.session.Session().get_available_regions('efs')
['ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']

looks like the waf list is empty, but I was able to use EFS as an analagous list, as they appear to be fairly similar

>>> boto3.session.Session().get_available_regions('waf')
[]
>>> boto3.session.Session().get_available_regions('efs')
['ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']

You may have to use waf-regional, waf which is applied for CF is a global service

boto3.session.Session().get_available_regions('waf-regional')
['ap-northeast-1', 'ap-southeast-2', 'eu-central-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2']

Was this page helpful?
0 / 5 - 0 ratings