Alexandre Abreu
Alexandre Abreu

Reputation: 1437

AWS OpsWorks Layer during Chef run

I want to pass a custom JSON to my chef run, but apparently this only can be done for the entire stack. I could use the layers name/id on the JSON with the desired data, but how can i check the layer which the instace is being labored by chef?

Upvotes: 4

Views: 1150

Answers (2)

Paulius Dragunas
Paulius Dragunas

Reputation: 1712

With chef 12:

from: https://forums.aws.amazon.com/thread.jspa?threadID=190405

node_instance = search(:node, "hostname:#
{node['hostname'].upcase}").first
Chef::Log.info "This is an instance object as returned by Chef Server. 
EC2 Instance ID is #{node_instance['ec2']['instance_id']}"

aws_instance = search(:aws_opsworks_instance, "hostname:#
{node['hostname'].upcase}").first
Chef::Log.info "This is the AWS OpsWorks instance object. It has AWS 
OpsWorks specific attributes, like the Layer IDs for the instance: #
{aws_instance['layer_ids'].join(',')}"

aws_instance['layer_ids'].each do |layer_id|
  layer = search(:aws_opsworks_layer, "layer_id:#{layer_id}").first
  Chef::Log.info "The instance belongs to layer #{layer['layer_id']}, 
it's name is #{layer['name']}"
end

Upvotes: 2

Alexandre Abreu
Alexandre Abreu

Reputation: 1437

Solved. SSH connect to my instance, then :

sudo opsworks-agent-cli get_json

This shows me the Opsworks JSON that is merged with Chef Custom JSON... and there is my layer's name:

node["opsworks"]["instance"]["layers"][0]

Then i used some logic in my recipe...

Upvotes: 8

Related Questions