Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandhoffman/f42b85cbc1b19aec482f to your computer and use it in GitHub Desktop.
Save evandhoffman/f42b85cbc1b19aec482f to your computer and use it in GitHub Desktop.

What is it?

It's a script that renames all your EBS volumes to the name+device of the EC2 instance they're attached to. It also applies the "environment" tag to each volume, read from the instance. (I use the 'environment' tag in billing reports.)

So if your instance's "Name" tag is backend-1234.prod.example.com and the volume is mapped to /dev/sdh, this script would apply the tag Name=backend-1234-prod-example-com-dev-sdh to the volume, and set the environment tag to match the instance's.

require 'rubygems'
require 'aws-sdk'
ec2 = AWS::EC2.new
ec2.instances.each_with_index do |i, index_num|
i.block_devices.each do |blockdev|
next unless blockdev[:ebs]
vol = ec2.volumes[blockdev[:ebs][:volume_id]]
vol_name = [i.tags.Name, blockdev[:device_name] ].join("-").gsub(/[^\w\d]+/,'-')
puts ['Applying tags to volume ',blockdev[:ebs][:volume_id], '(#', index_num, ')'].join
vol.tag('Name', :value => vol_name)
vol.tag('environment',:value => i.tags.environment)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment