Chef - Ohai in AWS EC2 VPC

This is a quick tip to those of you who are using Chef inside an AWS VPC. The EC2 Ohai plugin does not run by default, which prevents some meaningful node attributes from being collected.

The EC2-specific node attributes I find most useful are:

[code language=“ruby”] node[‘ec2’][‘instance_id’] # => Instance’s ID node[‘ec2’][‘local_ipv4’] # => Instance’s IPv4 Address node[‘ec2’][‘placement_availability_zone’] # => Instance’s Region & Availability Zone node[‘ec2’][‘ami_id’] # => Instance’s Baseline AMI [/code]

To get your instances inside a VPC to pick up meaningful node attributes related to EC2, you have to create an Ohai hint file for the EC2 plugin. To do so, simply throw this into your initial bootstrap.

[code language=“bash”] mkdir -p /etc/chef/ohai/hints && touch ${_}/ec2.json [/code]

Make sure you don’t do that blindly on non-EC2 instances, as it will significantly increase the execution time of Ohai.  You might want to wrap this in an if statement, and use something like the example below.

[code language=“bash”] if [[ $(dmidecode | grep -i amazon) ]] ; then mkdir -p /etc/chef/ohai/hints && touch ${_}/ec2.json fi [/code]

comments powered by Disqus