Tung Nguyen
Tung Nguyen

Reputation: 1924

OpsWorks: How to set Chef environment?

Using AWS OpsWorks, how would you set environment (staging, production, etc.), which will be applied to node.chef_environment attribute, and then cookbooks will be able to read this attribute? (I guess this can be done using custom JSON, right?)

I came across this link but still do not know how to do.

With Vagrant, I can set it in Vagrantfile like this:

config.vm.provision :chef_solo do |chef|
  ...
  chef.environment = "local"
  ...
end

Thank you.

UPDATE:

I ended up solving the problem by writing my_environment_cookbook which only has this code in its default recipe:

if node[:chef_environment] != nil
  node.chef_environment = node[:chef_environment]
end

And in the custom JSON of the OpsWorks stack, I had something like this:

{
  "chef_environment": "staging"
}

Then I included my_environment_cookbook in the run list, before the cookbook in which I wanted to get node.chef_environment attribute.

Upvotes: 3

Views: 3580

Answers (1)

sethvargo
sethvargo

Reputation: 26997

Amazon Opsworks does not support native Chef environments at this time. You can manually set the node's environment in a recipe:

node.chef_environment = "environment_name"

Since Opsworks does not persist data between Chef Client runs, you will need to include this recipe each time you run the cookbook.

You can pull the value for "environment_name" from whatever data source you would like: hard code, data bag, node attribute, etc.

Upvotes: 4

Related Questions