Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created August 20, 2015 08:29
Show Gist options
  • Save yorickpeterse/9119747666ca7f8c0535 to your computer and use it in GitHub Desktop.
Save yorickpeterse/9119747666ca7f8c0535 to your computer and use it in GitHub Desktop.
# gem install aws-sdk-v1
require 'aws-sdk-v1'
require 'tempfile'
require 'time'
require 'json'
AWS.config(:ec2_region => 'eu-west-1', :auto_scaling_region => 'eu-west-1')
as_group_name = ENV['AS_GROUP']
as_client = AWS::AutoScaling.new
if !as_group_name or as_group_name.empty?
abort 'Specify an autoscaling group in AS_GROUP'
end
group = as_client.groups[as_group_name]
old_conf = group.launch_configuration
tempfile = Tempfile.new(['userdata', '.json'])
tempfile.write(JSON.pretty_generate(
JSON.load(old_conf.user_data),
:indent => ' '
))
tempfile.rewind
system("#{ENV['EDITOR']} #{tempfile.path}")
user_data = tempfile.read
puts 'Creating launch configuration...'
ami_id = ENV['AMI_ID'] || old_conf.image_id
iam_id = ENV['IAM_ID'] || old_conf.iam_instance_profile
options = {
:detailed_instance_monitoring => old_conf.detailed_instance_monitoring,
:key_pair => old_conf.key_pair,
:security_groups => old_conf.security_groups.to_a,
:user_data => user_data,
:iam_instance_profile => iam_id,
:block_device_mappings => old_conf.block_device_mappings.to_a
}
if old_conf.spot_price
options[:spot_price] = old_conf.spot_price
elsif ENV['SPOT_PRICE']
options[:spot_price] = ENV['SPOT_PRICE']
end
new_conf = as_client.launch_configurations.create(
Time.now.strftime("#{as_group_name}-%Y%m%d-%H%M%S"),
ami_id,
ENV['INSTANCE_TYPE'] || old_conf.instance_type,
options
)
puts 'Updating autoscaling group...'
group.update(:launch_configuration => new_conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment