HOWTO: list EC2 availability zone placement with knife

Hi all,

I just wrote a blog post about how to retrieve and summarize AZ info for
your chef nodes. Here’s the content of the post, in case it comes in handy
for anyone:

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.ec2.placement_availability_zone}”}’ | sort
node1: us-east-1b
node2: us-east-1c
node3: us-east-1b
node4: us-east-1a

Or 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-1c

Gathering 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: