Describing EC2 instance AZ placement with Knife
With Chef's knife utility, it's easy to get information about your EC2 environment's node distribution across availability zones:
For example, to list nodes and their AZ placement:
$ knife exec -E 'nodes.transform("*:*") {|n| puts "#{n.name}: #{n.ec2.placement_availability_zone}"}' | sort
node1: us-east-1b
node2: us-east-1c
node3: us-east-1b
node4: us-east-1aOr to count AZ placements:
$ knife exec -E 'nodes.transform("*:*") {|n| puts "#{n.ec2.placement_availability_zone}"}' | awk ' { for (i=1;i<=NF;i++) count[$i]++ } END { for (i in count) print count[i], i }' $* | sort -r
2 us-east-1b
1 us-east-1a
1 us-east-1cGathering and summarizing AZ data in this fashion can help you verify that your application is properly distributed across AZs. It also can come in handy when purchasing reserved instances. You can of course modify the search query from *:* to something more targeted. And since there's always more than one way to do it, please feel free to share any alternative methods in the comments.
Here's a gist for the above; I'll update it with any future tweaks or additional commands:
