Skip to content

Instantly share code, notes, and snippets.

@mochizuki-masao
Last active August 2, 2016 05:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochizuki-masao/d1230e858e0bedd4fac1981451d1d1cf to your computer and use it in GitHub Desktop.
Save mochizuki-masao/d1230e858e0bedd4fac1981451d1d1cf to your computer and use it in GitHub Desktop.
Create EBS Name tag based on attached EC2 instance name.
#!/usr/bin/env ruby
require 'aws-sdk'
ec2 = Aws::EC2::Resource.new
kv = ec2.instances.each do |i|
begin
name = i.tags.find{|t| t["key"] == "Name" }.value
rescue
name = i.instance_id
end
i.block_device_mappings.each do |b|
device_name = b.device_name.split("/").last
vol = Aws::EC2::Volume.new(b.ebs.volume_id)
vol.create_tags(tags: [{key: "Name", value: "#{name}_#{device_name}"}])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment