ashishrana.cse
ashishrana.cse

Reputation: 11

How to tag multiple resources reading lazily them from an attribute?

I want to tag multiple AWS Instances, the instance-ID for them is stored in an attribute, e.g. -

aws
  |_ instances
            |____ 0: i-12sdfddf
            |____ 1: i-23fdgfdg
            |____ 2: i-34rtetio

Here, when reading the node data, I have to lazily load the same. Following works fine for me -

    aws_resource_tag 'tag_instances' do
        resource_id lazy{node['aws']['instances'][0]}
        tags (tag)
    end

    aws_resource_tag 'tag_instances' do
        resource_id lazy{node['aws']['instances'][1]}
        tags (tag)
    end
.. and so on

But I want to tag all instances in a loop. Something like this -

aws_resource_tag 'tag_instances' do
  resource_id lazy {node['aws']['instances'].values}
  tags (tag)
end

I am new to all of this, please help.

Update:

As per @seshadri_c 's answer, there is an existing bug here - bug#243.

Is there a workaround for this? Something like this would help -

instance_values = lazy {node['aws']['instances']}

instance_values.each do |index, instance_id|
  aws_resource_tag 'ec2_instances' do
    resource_id instance_id
    tags(tags)
  end
end

Upvotes: 1

Views: 48

Answers (1)

seshadri_c
seshadri_c

Reputation: 7340

The resource_id property of aws_resource_tag resource can take an array of instances. So we can just take the values out of the node['aws']['instances'] hash.

aws_resource_tag 'tag_instances' do
  resource_id lazy { node['aws']['instances'].values }
  tags (tag)
end

Update:

I think we are running into a bug in upstream code. It's been open for more than 3 years now.

But a typical way to loop through a Hash or Array is to use the .each iterator. If you can get the node['aws']['instances'] without lazy, then you can use something like below:

node['aws']['instances'].each do |indx, instance_id|
  aws_resource_tag 'ec2_instances' do
    resource_id instance_id
    tags(tags)
  end
end

Upvotes: 0

Related Questions