如何获取附加到OpsWorks实例的EBS卷ID?

时间:2014-09-24 15:10:56

标签: amazon-web-services chef aws-opsworks

我正在使用OpsWorks来部署一堆应用程序,我想标记实例及其所有相关资源。我正在使用opscode aws cookbook(https://github.com/opscode-cookbooks/aws)来标记我的实例,使用以下方法可以正常工作:

include_recipe 'aws'

custom_tags = node.fetch('aws_tag', {}).fetch('tags', nil)
instance_id = node.fetch('ec2', {}).fetch('instance_id', nil)

unless custom_tags.nil? || custom_tags.empty?
  aws_resource_tag 'tag instance' do
    tags custom_tags
    resource_id instance_id
    action :update
  end
end

我想扩展此配方以标记附加到实例的EBS卷。 aws_resource_tag()可以标记实例,快照和卷,但我需要提供要标记的卷列表。

如何将卷ID附加到实例?

2 个答案:

答案 0 :(得分:0)

我在http://docs.aws.amazon.com/opsworks/latest/userguide/attributes-json-opsworks-instance.html中看不到任何内容,因此您可能只需要使用标准的ohai数据。连接到计算机并运行ohai ec2,您将看到完整的元数据树。

答案 1 :(得分:0)

first of all you need know OpsWorks automatically tag Layer or Stack resources associated but the Tags cannot currently be applied to the root, or default, EBS volume of an instance.

If you using OpsWorks for Windows Stack, I suggest install the following cookbook from Supermarket:

File metadata.rb

depends 'aws', '4.2.2'
depends 'ohai', '4.2.3'
depends 'compat_resource', '12.19.1'

Next add to your stack a IAM role with necessary permission to perform list-tags for OpsWorks and create-tags in the EC2 service.

At the end you can use this recipes add-tags.rb:

Chef::Log.info("******TAGS VOLUME******")
#Chef::Log.level = :debug
instance = search("aws_opsworks_instance", "self:true").first
stack = search("aws_opsworks_stack").first
arnstack = "#{stack['arn']}"
cmd = "aws opsworks list-tags --resource-arn #{arnstack} --region eu-west-1"
Chef::Log.info("****** #{arnstack} ******")
batch  'find_tags' do
  cwd "C:\\Program Files\\Amazon\\AWSCLI"
  code <<-EOH
  #{cmd} > C:\\tmp\\res.json
  EOH
end
if ::File.exist?('C:\\tmp\\res.json')
  myjsonfile = ::File.read('C:\\tmp\\res.json').chomp
  data = JSON.parse("#{myjsonfile}")
  data['Tags'].each do |key, value|
    aws_resource_tag 'Boot Volume' do
        resource_id lazy {instance['root_device_volume_id']}
        tags(key => value)
    end
  end
end

That recipe add all TAG founded on stack only to root volume of my instance.