Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (59 sloc) 1.63 KB
# Tag all Security Groups with correpsonding Environment from instance tags
# 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.security_groups.each do |secgroups|
next unless secgroups
sg = ec2.security_groups({
filters: [
{
name: 'group-id',
values: [secgroups.group_id],
},
],
}).first
i.tags.each do |i_tag, index_num|
if i_tag
if i_tag.key == 'environment' && i_tag.value && i_tag.key
sg_name = i_tag.value
puts ['Adding environment to security group ', secgroups.group_name, ', environment: ', sg_name].join
sg.create_tags(
tags: [
{
key: 'environment',
value: sg_name,
}
]
)
end
end
end
end
end