Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@affix
Last active July 15, 2018 19:23
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 affix/c3aaff3a78b3355d051bffbb8999112c to your computer and use it in GitHub Desktop.
Save affix/c3aaff3a78b3355d051bffbb8999112c to your computer and use it in GitHub Desktop.
A ruby script to update your AutoScaling Group by taking an image of an existing instance
#!/usr/bin/env ruby
# UpdateASGroup.rb
# Update AutoScaling Group with a new Image based from an running Instance
# (c) 2018 Keiran Smith <opensource_at_keiran.scot>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'aws-sdk'
require 'json'
require 'date'
# CONFIG
# PLEASE NOTE : You Must have AWS_ACCESS_KEY, AWS_SECRET_KEY,AWS_REGION env vars
# set for this script to work correctly.
image_prefix = "XXXXXX" # A prefix for the image name
group_name = "XXXXXX" # Name of AutoScaling Group
instance_id = "i-xxxxxx" # ID Of image to Instance
instance_type = "m3.large" # Instance Type
keypair = "Xxxxxx" # String of Key Pair Name
security_groups = ["sg-xxxxxxxx", "sg-xxxxxxxx"] # Security Groups for Instance
# ENDCONFIG
ec2 = Aws::EC2::Resource.new
as = Aws::AutoScaling::Client.new
def rand_string
(0..6).map{ ('a'..'z').to_a[rand(26)] }.join
end
puts "Newspaper Club AWS AutoScaling Updater"
puts "Updating AutoScaling Group #{group_name} using instance #{instance_id}"
instance = ec2.instance(instance_id)
puts ""
image_name = "#{image_prefix}-#{DateTime.now.strftime("%Y-%d-%m")}-#{rand_string}"
image_id = nil
image = instance.create_image({
dry_run: false,
name: image_name, # required
description: "Auto Scaling Image",
no_reboot: true,
})
image_id = image.id
puts "AMI ID: #{image_id}"
puts "Waiting for image to become ready"
while ec2.image(image_id).state != "available"
printf "."
# Sleep so we don't hit the rate limit
sleep 5
end
puts ""
puts "Image is available Creating Auto Scaling Launch Config"
as.create_launch_configuration({
launch_configuration_name: image_name,
image_id: image_id,
key_name: keypair,
security_groups: security_groups,
instance_type: instance_type,
instance_monitoring: {
enabled: true
}
})
puts "Updating AutoScaling Group : #{group_name}"
as.update_auto_scaling_group({
auto_scaling_group_name: group_name,
launch_configuration_name: image_name
})
puts "Completed Update of #{group_name} with new AMI #{image_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment