0

Quick context: I want to write a recipe that changes dynamically based on the AWS region that an instance is in. I want to install the CodeDeploy agent which downloads from an S3 bucket based on the region of the instance. As such I need an attribute which is set to the region

The AWS public cookbook has the code to do this but it's not set to an attribute:

def instance_availability_zone
  @@instance_availability_zone ||= query_instance_availability_zone
end
...
 def query_instance_availability_zone
  availability_zone = open('http://169.254.169.254/latest/meta-data/placement/availability-zone/', options = { proxy: false }) { |f| f.gets }
  fail 'Cannot find availability zone!' unless availability_zone
  Chef::Log.debug("Instance's availability zone is #{availability_zone}")
  availability_zone
end

It's even used in the same class in a way that would get the region in the format I want

def create_aws_interface(aws_interface)
  begin
    require 'aws-sdk'
    rescue LoadError
    Chef::Log.error("Missing gem 'aws-sdk'. Use the default aws recipe to install it first.")
  end
  region = instance_availability_zone
  region = region[0, region.length - 1]
...

So I want to have the region above set to the attribute node['was']['region'] or some such, but I lack the skill to do so. I would think I need to put this in a definition and then call it somehow?

Summary of the question: How can I set an attribute in chef via running ruby code (from a library file)?

Here's the cookbook: https://github.com/opscode-cookbooks/aws

yoshiwaan
  • 479
  • 4
  • 11
  • What about `node.set['was']['region']='value'` ? – Leonid Shevtsov Mar 24 '15 at 18:15
  • That's the fallback if I can't get this working. I'd really prefer to just be able to spool up an AWS instance with a runlist and have it be done. Plus if someone forgets to do this then CodeDeploy won't work and it's going to be hard to debug. – yoshiwaan Mar 24 '15 at 18:22

1 Answers1

1

You could let ohai do the work.

node[ec2][placement_availability_zone] is set by ohai. You'd just need to parse out the region from the overall AZ string.

node[ec2][placement_availability_zone].match(/.*-\d/) will do the trick, I believe.

If you are using an VPC node, you will need to add the ec2 hint for ohai. This can be most easily accomplished by passing --hint ec2 when bootstrapping the node.

Tejay Cardon
  • 4,193
  • 2
  • 16
  • 31
  • Excellent, this is a good solution in a totally different way. If you edit your answer to include the dependencies for this to work then I'll mark this as correct. The dependencies are 1) the sigar gem is needed, so your recipe needs chef_gem 'sigar' as well 2) The file /etc/chef/ohai/hints/ec2.json needs to exist on the server for detection to work. This is created automatically by knife ec2 server create but not otherwise and will need to be added by some other means (such as user data or cloud formation template). – yoshiwaan Mar 27 '15 at 16:48
  • It also looks like there may be a bug with this. https://tickets.opscode.com/browse/OHAI-489. This was resolved ages ago but I'm getting this problem on ohai 8.1.1 – yoshiwaan Mar 27 '15 at 17:07
  • Where did you see that the sigar gem was needed? I've been using this approach for a year now without incident, and I never include any chef gems. – Tejay Cardon Mar 28 '15 at 23:08
  • I just booted up an instance with knife ec2 then ran ohai -l debug. I wasn't getting the value in my recipes so had to debug with ohai. I kept getting looks_like_ec2? == false in the oh output with a message saying sigar could not be loaded above. When I put a chef_gem 'sigar' in my recipe I started getting looks_like_ec2? == true. – yoshiwaan Mar 30 '15 at 16:10
  • What OS were you using? – Tejay Cardon Mar 30 '15 at 19:47
  • Amazon Linux. I might spool up another instance and double check this. It might have just been missing the hints file and I took the sigar error as the fault. – yoshiwaan Mar 30 '15 at 22:02
  • Okay yeah it works without the sigar gem. Must have been user error on my part. – yoshiwaan Mar 30 '15 at 22:09