Skip to content

Instantly share code, notes, and snippets.

@igorlg
Last active July 31, 2017 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorlg/1eae38b8e12d5cd2d58d07a5b67cdd15 to your computer and use it in GitHub Desktop.
Save igorlg/1eae38b8e12d5cd2d58d07a5b67cdd15 to your computer and use it in GitHub Desktop.
List AWS AZs and Regions

Required Gems

  • aws-sdk
  • terminal-table

Install: gem install aws-sdk terminal-table

Usage

Run ruby awsazs-update.rb to generate the JSON file.

Run ruby awsazs.rb to see the output:

+----------------+-------------+------------------------------------------------------------------------+
| Code           | Region Name | AZs                                                                    |
+----------------+-------------+------------------------------------------------------------------------+
| ap-northeast-1 | Tokyo       | ap-northeast-1a, ap-northeast-1c                                       |
| ap-northeast-2 | Seoul       | ap-northeast-2a, ap-northeast-2c                                       |
| ap-south-1     | Mumbai      | ap-south-1a, ap-south-1b                                               |
| ap-southeast-1 | Singapore   | ap-southeast-1a, ap-southeast-1b                                       |
| ap-southeast-2 | Sydney      | ap-southeast-2a, ap-southeast-2b, ap-southeast-2c                      |
+----------------+-------------+------------------------------------------------------------------------+
| ca-central-1   | Canada      | ca-central-1a, ca-central-1b                                           |
+----------------+-------------+------------------------------------------------------------------------+
| eu-central-1   | Frankfurt   | eu-central-1a, eu-central-1b, eu-central-1c                            |
| eu-west-1      | Ireland     | eu-west-1a, eu-west-1b, eu-west-1c                                     |
| eu-west-2      | London      | eu-west-2a, eu-west-2b                                                 |
+----------------+-------------+------------------------------------------------------------------------+
| sa-east-1      | Saopaulo    | sa-east-1a, sa-east-1b, sa-east-1c                                     |
+----------------+-------------+------------------------------------------------------------------------+
| us-east-1      | Virginia    | us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1e, us-east-1f |
| us-east-2      | Ohio        | us-east-2a, us-east-2b, us-east-2c                                     |
| us-west-1      | California  | us-west-1b, us-west-1c                                                 |
| us-west-2      | Oregon      | us-west-2a, us-west-2b, us-west-2c                                     |
+----------------+-------------+------------------------------------------------------------------------+
{
"ap-south-1": {
"name": "mumbai",
"azs": [
"ap-south-1a",
"ap-south-1b"
]
},
"eu-west-2": {
"name": "london",
"azs": [
"eu-west-2a",
"eu-west-2b"
]
},
"eu-west-1": {
"name": "ireland",
"azs": [
"eu-west-1a",
"eu-west-1b",
"eu-west-1c"
]
},
"ap-northeast-2": {
"name": "seoul",
"azs": [
"ap-northeast-2a",
"ap-northeast-2c"
]
},
"ap-northeast-1": {
"name": "tokyo",
"azs": [
"ap-northeast-1a",
"ap-northeast-1c"
]
},
"sa-east-1": {
"name": "saopaulo",
"azs": [
"sa-east-1a",
"sa-east-1b",
"sa-east-1c"
]
},
"ca-central-1": {
"name": "canada",
"azs": [
"ca-central-1a",
"ca-central-1b"
]
},
"ap-southeast-1": {
"name": "singapore",
"azs": [
"ap-southeast-1a",
"ap-southeast-1b"
]
},
"ap-southeast-2": {
"name": "sydney",
"azs": [
"ap-southeast-2a",
"ap-southeast-2b",
"ap-southeast-2c"
]
},
"eu-central-1": {
"name": "frankfurt",
"azs": [
"eu-central-1a",
"eu-central-1b",
"eu-central-1c"
]
},
"us-east-1": {
"name": "virginia",
"azs": [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
]
},
"us-east-2": {
"name": "ohio",
"azs": [
"us-east-2a",
"us-east-2b",
"us-east-2c"
]
},
"us-west-1": {
"name": "california",
"azs": [
"us-west-1b",
"us-west-1c"
]
},
"us-west-2": {
"name": "oregon",
"azs": [
"us-west-2a",
"us-west-2b",
"us-west-2c"
]
}
}
require 'aws-sdk'
require 'json'
def region_name(r)
regions = {
'us-east-1' => 'virginia',
'us-east-2' => 'ohio',
'us-west-1' => 'california',
'us-west-2' => 'oregon',
'ca-central-1' => 'canada',
'eu-west-1' => 'ireland',
'eu-central-1' => 'frankfurt',
'eu-west-2' => 'london',
'ap-northeast-1' => 'tokyo',
'ap-northeast-2' => 'seoul',
'ap-southeast-1' => 'singapore',
'ap-southeast-2' => 'sydney',
'ap-south-1' => 'mumbai',
'sa-east-1' => 'saopaulo',
}
return regions[r]
end
output = Hash.new
ec2 = Aws::EC2::Client.new
ec2.describe_regions.regions.each do |r|
reg = r.region_name
output[reg] = { name: region_name(reg), azs: []}
ec2_r = Aws::EC2::Client.new region: r.region_name
ec2_r.describe_availability_zones.availability_zones.each { |az| output[r.region_name][:azs] << az.zone_name }
end
file = File.join(File.expand_path(File.dirname(__FILE__)), 'awsazs-data.json')
File.write(file, JSON.dump(output))
puts "Output: #{JSON.pretty_generate(output)}"
puts "Saved to file #{file}"
require 'json'
require 'terminal-table'
class String
def bold; "\e[1m#{self}\e[22m" end
end
file = File.join(File.expand_path(File.dirname(__FILE__)), 'awsazs-data.json')
data = JSON.load(File.read(file))
division = ''
table = Terminal::Table.new do |t|
t.headings = ['Code'.bold, 'Region Name'.bold, 'AZs'.bold]
data.sort.each do |k,v|
t.add_separator unless division == k[0..1] or division == ''
division = k[0..1]
t.add_row [k.bold, v['name'].capitalize.bold, v['azs'].join(', ')]
end
end
print table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment