-
-
Save yorickpeterse/9119747666ca7f8c0535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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