Skip to content

Instantly share code, notes, and snippets.

@nanki
Created April 2, 2015 23:32
Show Gist options
  • Save nanki/980ae2b15253baef1a59 to your computer and use it in GitHub Desktop.
Save nanki/980ae2b15253baef1a59 to your computer and use it in GitHub Desktop.
tag EBS volumes
require 'aws-sdk-core'
require 'aws-sdk-resources'
require 'yaml'
config = YAML.load_file('config.yml')[:tag]
Aws.config.update(
region: 'ap-northeast-1',
credentials: Aws::Credentials.new(config[:access_key_id], config[:secret_access_key])
)
ec2 = Aws::EC2::Resource.new
ec2.vpcs.each do |vpc|
tags = vpc.tags.select{|tag|["Product", "Environment"].include? tag.key}
next unless tags.count == 2
vpc.instances.each do |i|
i.volumes.each do |volume|
volume.create_tags tags: tags if volume.tags.empty?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment