Skip to content

Get Availability Zones with boto3  #19

@sesas

Description

@sesas

This piece of code doesn't handle us-east-1 region well:

def add_vpc_az_mapping(self,
boto_config,
az_count=2):
"""
Method gets the AZs within the given account where subnets can be created/deployed
This is necessary due to some accounts having 4 subnets available within ec2 classic and only 3 within vpc
which causes the Select by index method of picking azs unpredictable for all accounts
@param boto_config [dict] collection of boto configuration values as set by the configuration file
@param az_count [int] number of AWS availability zones to include in the VPC mapping
"""
az_dict = {}
region_list = []
aws_auth_info = {}
if 'aws_access_key_id' in boto_config and 'aws_secret_access_key' in boto_config:
aws_auth_info['aws_access_key_id'] = boto_config.get('aws_access_key_id')
aws_auth_info['aws_secret_access_key'] = boto_config.get('aws_secret_access_key')
conn = boto.vpc.connect_to_region(region_name=boto_config.get('region_name', 'us-east-1'), **aws_auth_info)
for region in conn.get_all_regions():
region_list.append(region.name)
az_list = boto.vpc.connect_to_region(region.name, **aws_auth_info).get_all_zones()
if len(az_list) > 1:
temp_dict = {}
x = 0
for availability_zone in az_list:
temp_dict['az' + str(x) + 'Name'] = availability_zone.name
x += 1
if len(temp_dict) >= az_count:
az_dict[region.name] = {}
for item in temp_dict:
self.template.add_region_map_value(region.name, item, temp_dict[item])

This line in particular:
az_list = boto.vpc.connect_to_region(region.name, **aws_auth_info).get_all_zones()

Source: http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_availability_zones

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions