title: OpsWorks: How to set Chef environment?
body: <p>Using <strong>AWS OpsWorks</strong>, how would you set environment (staging, production, etc.), which will be applied to <strong><code>node.chef_environment</code></strong> attribute, and then cookbooks will be able to read this attribute? (I guess this can be done using custom JSON, right?)</p>
  
  <p>I came across this <a href="http://lists.opscode.com/sympa/arc/chef/2013-08/msg00380.html" rel="nofollow"><strong>link</strong></a> but still do not know how to do.</p>
  
  <p>With <strong>Vagrant</strong>, I can set it in Vagrantfile like this:</p>
  
  <pre><code>config.vm.provision :chef_solo do |chef|
    ...
    chef.environment = "local"
    ...
  end
  </code></pre>
  
  <p>Thank you.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I ended up solving the problem by writing <code>my_environment_cookbook</code> which only has this code in its default recipe:</p>
  
  <pre><code>if node[:chef_environment] != nil
    node.chef_environment = node[:chef_environment]
  end
  </code></pre>
  
  <p>And in the custom JSON of the OpsWorks stack, I had something like this:</p>
  
  <pre><code>{
    "chef_environment": "staging"
  }
  </code></pre>
  
  <p>Then I included <code>my_environment_cookbook</code> in the run list, before the cookbook in which I wanted to get <code>node.chef_environment</code> attribute.</p>