Last active
August 2, 2016 05:12
-
-
Save mochizuki-masao/d1230e858e0bedd4fac1981451d1d1cf to your computer and use it in GitHub Desktop.
Create EBS Name tag based on attached EC2 instance name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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