Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (62 sloc) 1.74 KB
# Name all EBS volumes with Instance Name, Volume Name and tag with Environment
# Written by Jared Williams <thisis@jared.nyc>
#
# Variable Description:
# AWSAccess Your Access Key
# AWSSecret Your Secret Access Key
# AWSRegion The Region to run the script against
#
# If you have any questions or comments, heres the ways to reach me:
# email: thisis@jared.nyc
# slack: https://devopschat.co signing up is free and my username is @jaredwilliams
# github: @jaredmichaelwilliams
# reddit: jaredw
#
# Instructions
# 1. Install the following gems: json, aws-sdk (version 2 only!)
# 2. Change the variables to fit your info
# 3. Run and watch!
#
require 'rubygems'
require 'json'
require 'aws-sdk'
AWSAccess = "ACCESS_KEY"
AWSSecret = "SECRET_ACCESS_KEY"
AWSRegion = "us-east-1"
Aws.config.update(
region: AWSRegion,
credentials: Aws::Credentials.new(AWSAccess, AWSSecret)
)
ec2 = Aws::EC2::Resource.new(region: AWSRegion)
ec2.instances.each_with_index do |i, index_num|
i.volumes.each do |blockdev|
next unless blockdev
vol = blockdev
i.tags.each do |i_tag, index_num|
if i_tag
if i_tag.key == 'Name' && i_tag.value && i_tag.key
vol_name = [i_tag.value].join('-').gsub(/[^\w\d]+/, '-')
puts ['Applying tags to volume ', blockdev.attachments[0].device, '(#', index_num, '), new name: ', vol_name].join
vol.create_tags(
tags: [
{
key: 'Name',
value: vol_name
}
]
)
end
if i_tag.key == 'environment' && i_tag.value && i_tag.key
vol.create_tags(
tags: [
{
key: 'environment',
value: i_tag.value
}
]
)
end
end
end
end
end