Friday, July 25, 2014

AWS CLI memorandum - Describe all Availability Zones for each region

I was creating CloudFormation template and wanted to list all of the Availability Zones for each regions to create the following Mappings in the template.

  "Mappings": {
    "AvailabilityZoneMap": {
       "ap-northeast-1": { "AZa": "ap-northeast-1a", "AZb": "ap-northeast-1b", "AZc": "ap-northeast-1c" }
      ...
       "ap-northeast-2": { "AZa": "ap-northeast-2a", "AZb": "ap-northeast-2b", "AZc": "ap-northeast-2c" 
    }
  },

Describing all the names of the regions
aws ec2 describe-regions | jq -r '.Regions[].RegionName'
eu-west-1
sa-east-1
us-east-1
ap-northeast-1
us-west-2
us-west-1
ap-southeast-1
ap-southeast-2

Describing all the AZs for each regions
$ for i in `aws ec2 describe-regions | jq -r '.Regions[].RegionName'` ; do
ec2-describe-availability-zones --region $i
done

AVAILABILITYZONE        eu-west-1a      available       eu-west-1       
AVAILABILITYZONE        eu-west-1b      available       eu-west-1       
AVAILABILITYZONE        eu-west-1c      available       eu-west-1       
AVAILABILITYZONE        sa-east-1a      available       sa-east-1       
AVAILABILITYZONE        sa-east-1b      available       sa-east-1       
AVAILABILITYZONE        us-east-1a      available       us-east-1       
AVAILABILITYZONE        us-east-1b      available       us-east-1       
AVAILABILITYZONE        us-east-1c      available       us-east-1       
AVAILABILITYZONE        ap-northeast-1a available       ap-northeast-1  
AVAILABILITYZONE        ap-northeast-1c available       ap-northeast-1  
AVAILABILITYZONE        us-west-2a      available       us-west-2       
AVAILABILITYZONE        us-west-2b      available       us-west-2       
AVAILABILITYZONE        us-west-2c      available       us-west-2       
AVAILABILITYZONE        us-west-1a      available       us-west-1       
AVAILABILITYZONE        us-west-1c      available       us-west-1       
AVAILABILITYZONE        ap-southeast-1a available       ap-southeast-1  
AVAILABILITYZONE        ap-southeast-1b available       ap-southeast-1  
AVAILABILITYZONE        ap-southeast-2a available       ap-southeast-2  
AVAILABILITYZONE        ap-southeast-2b available       ap-southeast-2  

I tried using AWS CLI to do the same, but AWS CLI only includes the zones for the region that you're currently using and it does not show the AZs for the other regions.
Describes one or more of the Availability Zones that are available to you. The results include zones only for the region you're currently using. If there is an event impacting an Availability Zone, you can use this request to view the state and any provided message for that Availability Zone.

$ for i in `aws ec2 describe-regions | jq -r '.Regions[].RegionName'` ; do
aws ec2 describe-availability-zones --zone-names $i
done
A client error (InvalidParameterValue) occurred: Invalid availability zone: [eu-west-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [sa-east-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [us-east-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [ap-northeast-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [us-west-2]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [us-west-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [ap-southeast-1]
A client error (InvalidParameterValue) occurred: Invalid availability zone: [ap-southeast-2]

I need to think about how to convert the results into JSON format next.