# Aws cookbook and tags

**URL:** <https://discourse.chef.io/t/aws-cookbook-and-tags/4250>\
**Category:** Chef Infra (archive)\
**Created:** [July 25, 2013, 11:38pm UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250 "2013-07-25T23:38:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Montgomery](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@David\_Montgomery](https://discourse.chef.io/u/David_Montgomery)\
**Post date:** [July 25, 2013, 11:38pm UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250/1 "2013-07-25T23:38:44Z")

</div>

Hi,

Trying to add tags to ec2. Using the below…and tryng ti use the example  
on [http://community.opscode.com/cookbooks/aws](http://community.opscode.com/cookbooks/aws)

I get the below error…why? s the example incorrect?

include\_recipe "aws"  
data\_bag(“my\_data\_bag”)  
db = data\_bag\_item(“my\_data\_bag”, “my”)  
aws = db[node.chef\_environment][‘aws’]  
AWS\_ACCESS\_KEY\_ID = aws[‘AWS\_ACCESS\_KEY\_ID’]  
AWS\_SECRET\_ACCESS\_KEY = aws[‘AWS\_SECRET\_ACCESS\_KEY’]

aws\_resource\_tag node[‘ec2’][‘instance\_id’] do  
aws\_access\_key aws[‘AWS\_ACCESS\_KEY\_ID’]  
aws\_secret\_access\_key aws[‘AWS\_SECRET\_ACCESS\_KEY’]  
tags({“Name” =\> “Monitor”,  
“node\_name” =\> “#{[node.name](http://node.name)}”,  
“environment” =\> node.chef\_environment})  
action :update  
end

# ================================================================================ Recipe Compile Error in /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb

## NoMethodError

undefined method `[]’ for nil:NilClass

## Cookbook Trace:

/var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:8:in `from\_file’

## Relevant File Content:

/var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:

1: include\_recipe "aws"  
2: data\_bag(“my\_data\_bag”)  
3: db = data\_bag\_item(“my\_data\_bag”, “my”)  
4: aws = db[node.chef\_environment][‘aws’]  
5: AWS\_ACCESS\_KEY\_ID = aws[‘AWS\_ACCESS\_KEY\_ID’]  
6: AWS\_SECRET\_ACCESS\_KEY = aws[‘AWS\_SECRET\_ACCESS\_KEY’]  
7:  
8\>\> aws\_resource\_tag node[‘ec2’][‘instance\_id’] do  
9: aws\_access\_key aws[‘AWS\_ACCESS\_KEY\_ID’]  
10: aws\_secret\_access\_key aws[‘AWS\_SECRET\_ACCESS\_KEY’]  
11: tags({“Name” =\> “Monitor”,  
12: “node\_name” =\> “#{[node.name](http://node.name)}”,  
13: “environment” =\> node.chef\_environment})  
14: action :update  
15: end 16:

[2013-07-25T23:32:48+00:00] ERROR: Running exception handlers  
[2013-07-25T23:32:48+00:00] FATAL: Saving node information to  
/var/chef/cache/failed-run-data.json  
[2013-07-25T23:32:48+00:00] ERROR: Exception handlers complete  
Chef Client failed. 0 resources updated  
[2013-07-25T23:32:49+00:00] FATAL: Stacktrace dumped to  
/var/chef/cache/chef-stacktrace.out  
[2013-07-25T23:32:49+00:00] FATAL: NoMethodError: undefined method `[]’ for  
nil:NilClass

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [July 26, 2013, 3:38pm UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250/2 "2013-07-26T15:38:24Z")

</div>

On Thursday, July 25, 2013 at 4:38 PM, David Montgomery wrote:

> Hi,
> 
> Trying to add tags to ec2. Using the below...and tryng ti use the example on [http://community.opscode.com/cookbooks/aws](http://community.opscode.com/cookbooks/aws)
> 
> I get the below error......why? s the example incorrect?
> 
> include\_recipe "aws"  
> data\_bag("my\_data\_bag")  
> db = data\_bag\_item("my\_data\_bag", "my")  
> aws = db[node.chef\_environment]['aws']  
> AWS\_ACCESS\_KEY\_ID = aws['AWS\_ACCESS\_KEY\_ID']  
> AWS\_SECRET\_ACCESS\_KEY = aws['AWS\_SECRET\_ACCESS\_KEY']
> 
> aws\_resource\_tag node['ec2']['instance\_id'] do  
> aws\_access\_key aws['AWS\_ACCESS\_KEY\_ID']  
> aws\_secret\_access\_key aws['AWS\_SECRET\_ACCESS\_KEY']  
> tags({"Name" =\> "Monitor",  
> "node\_name" =\> "#{node.name ([http://node.name](http://node.name))}",  
> "environment" =\> node.chef\_environment})  
> action :update  
> end
> 
> # ================================================================================ Recipe Compile Error in /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb
> 
> ## NoMethodError
> 
> undefined method `' for nil:NilClass
> 
> ## Cookbook Trace:
> 
> /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:8:in `from\_file'
> 
> ## Relevant File Content:
> 
> /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:
> 
> 1: include\_recipe "aws"  
> 2: data\_bag("my\_data\_bag")  
> 3: db = data\_bag\_item("my\_data\_bag", "my")  
> 4: aws = db[node.chef\_environment]['aws']  
> 5: AWS\_ACCESS\_KEY\_ID = aws['AWS\_ACCESS\_KEY\_ID']  
> 6: AWS\_SECRET\_ACCESS\_KEY = aws['AWS\_SECRET\_ACCESS\_KEY']  
> 7:  
> 8\>\> aws\_resource\_tag node['ec2']['instance\_id'] do

Your error is in this line. Most likely `node['ec2']` is nil, so `node['ec2']['instance_id']` evaluates to `nil['instance_id']` which causes the error.

If you're using EC2 VPC, the cause of your problem is likely this: [http://tickets.opscode.com/browse/OHAI-310](http://tickets.opscode.com/browse/OHAI-310)

Try touching this file: /etc/chef/ohai/hints/ec2.json

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![David\_Montgomery](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@David\_Montgomery](https://discourse.chef.io/u/David_Montgomery)\
**Post date:** [July 27, 2013, 1:46am UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250/3 "2013-07-27T01:46:05Z")

</div>

But I am not using VPC.

Also..looking at hosted chef..I do not see ec2 attribute..yet I am using  
ec2? Did ohai fail? I though chef would auto detect ec2.....

On Fri, Jul 26, 2013 at 11:38 PM, Daniel DeLeo [dan@kallistec.com](mailto:dan@kallistec.com) wrote:

> On Thursday, July 25, 2013 at 4:38 PM, David Montgomery wrote:
> 
> Hi,
> 
> Trying to add tags to ec2. Using the below...and tryng ti use the example  
> on [http://community.opscode.com/cookbooks/aws](http://community.opscode.com/cookbooks/aws)
> 
> I get the below error......why? s the example incorrect?
> 
> include\_recipe "aws"  
> data\_bag("my\_data\_bag")  
> db = data\_bag\_item("my\_data\_bag", "my")  
> aws = db[node.chef\_environment]['aws']  
> AWS\_ACCESS\_KEY\_ID = aws['AWS\_ACCESS\_KEY\_ID']  
> AWS\_SECRET\_ACCESS\_KEY = aws['AWS\_SECRET\_ACCESS\_KEY']
> 
> aws\_resource\_tag node['ec2']['instance\_id'] do  
> aws\_access\_key aws['AWS\_ACCESS\_KEY\_ID']  
> aws\_secret\_access\_key aws['AWS\_SECRET\_ACCESS\_KEY']  
> tags({"Name" =\> "Monitor",  
> "node\_name" =\> "#{node.name}",  
> "environment" =\> node.chef\_environment})  
> action :update  
> end
> 
> ================================================================================  
> Recipe Compile Error in  
> /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb
> 
> ================================================================================
> 
> ## NoMethodError
> 
> undefined method `' for nil:NilClass
> 
> ## Cookbook Trace:
> 
> /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:8:in  
> `from\_file'
> 
> ## Relevant File Content:
> 
> /var/chef/cache/cookbooks/base/recipes/aws\_monitor\_tag.rb:
> 
> 1: include\_recipe "aws"  
> 2: data\_bag("my\_data\_bag")  
> 3: db = data\_bag\_item("my\_data\_bag", "my")  
> 4: aws = db[node.chef\_environment]['aws']  
> 5: AWS\_ACCESS\_KEY\_ID = aws['AWS\_ACCESS\_KEY\_ID']  
> 6: AWS\_SECRET\_ACCESS\_KEY = aws['AWS\_SECRET\_ACCESS\_KEY']  
> 7:  
> 8\>\> aws\_resource\_tag node['ec2']['instance\_id'] do
> 
> Your error is in this line. Most likely `node['ec2']` is nil, so  
> `node['ec2']['instance_id']` evaluates to `nil['instance_id']` which causes  
> the error.
> 
> If you're using EC2 VPC, the cause of your problem is likely this:  
> [http://tickets.opscode.com/browse/OHAI-310](http://tickets.opscode.com/browse/OHAI-310)
> 
> Try touching this file: /etc/chef/ohai/hints/ec2.json
> 
> --  
> Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![zts](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/zts/32/111_2.png) [@zts](https://discourse.chef.io/u/zts)\
**Post date:** [July 27, 2013, 5:53pm UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250/4 "2013-07-27T17:53:07Z")

</div>

Hi David,

On Sat, Jul 27, 2013 at 2:46 AM, David Montgomery \<[davidmontgomery@gmail.com](mailto:davidmontgomery@gmail.com)

> wrote:

> But I am not using VPC.

Are you absolutely certain? If you started using a region in the last few  
months, you'll automatically be creating instances in a VPC (see  
[http://aws.typepad.com/aws/2013/03/amazon-ec2-update-virtual-private-clouds-for-everyone.html](http://aws.typepad.com/aws/2013/03/amazon-ec2-update-virtual-private-clouds-for-everyone.html)  
)

> Also..looking at hosted chef..I do not see ec2 attribute..yet I am using  
> ec2? Did ohai fail? I though chef would auto detect ec2.....

Daniel's suggestion (creating the ohai hint) is the best thing to try.  
Does it fix your problem?  
If so, the problem is that Ohai cannot automatically detect that you are  
running on EC2. Creating the hint file is the correct way to fix that.

Zac

---

<div class="post-metadata">

**Author:** ![Andrew\_Gross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/andrew_gross/32/320_2.png) [@Andrew\_Gross](https://discourse.chef.io/u/Andrew_Gross)\
**Post date:** [July 29, 2013, 2:14pm UTC](https://discourse.chef.io/t/aws-cookbook-and-tags/4250/5 "2013-07-29T14:14:07Z")

</div>

This is what we use in our EC2/VPC bootstrap script

mkdir -p /etc/chef/ohai/hints && touch /etc/chef/ohai/hints/ec2.json

On Sat, Jul 27, 2013 at 1:53 PM, Zac Stevens [zts@cryptocracy.com](mailto:zts@cryptocracy.com) wrote:

> Hi David,
> 
> On Sat, Jul 27, 2013 at 2:46 AM, David Montgomery \<  
> [davidmontgomery@gmail.com](mailto:davidmontgomery@gmail.com)\> wrote:
> 
> > But I am not using VPC.
> 
> Are you absolutely certain? If you started using a region in the last few  
> months, you'll automatically be creating instances in a VPC (see  
> [http://aws.typepad.com/aws/2013/03/amazon-ec2-update-virtual-private-clouds-for-everyone.html](http://aws.typepad.com/aws/2013/03/amazon-ec2-update-virtual-private-clouds-for-everyone.html)  
> )
> 
> > Also..looking at hosted chef..I do not see ec2 attribute..yet I am using  
> > ec2? Did ohai fail? I though chef would auto detect ec2.....
> 
> Daniel's suggestion (creating the ohai hint) is the best thing to try.  
> Does it fix your problem?  
> If so, the problem is that Ohai cannot automatically detect that you are  
> running on EC2. Creating the hint file is the correct way to fix that.
> 
> Zac
