Skip to content

Instantly share code, notes, and snippets.

@zitooon
Created April 29, 2014 05:57
Show Gist options
  • Save zitooon/11391626 to your computer and use it in GitHub Desktop.
Save zitooon/11391626 to your computer and use it in GitHub Desktop.
AWS autoscale management rake tasks
namespace :aws do
namespace :autoscale do
require 'shellwords'
def title_puts s
puts "\n############################## #{s} ##############################"
end
def define_default_variables!
@autoscale_group_name = "your-autoscale-group-name-here"
@security_group_name = "your-security-group-name-here"
@instance_type = "m1.large"
@max_price ||= "0.07" # when using spot instances
@vpc_zone_identifier = "your-vpc-subnet-id"
end
desc "Create autoscale strategy"
task :create do
@ami_id = ENV['ami_id']
unless @ami_id
puts "ERROR : please provide ami_id=ami_id to create autoscale strategy"
exit
end
@max_price = ENV['max_price']
define_default_variables!
@max_instances = 25 # max number of instances to launch
@launch_config_name = "your-launch-config-desired-name"
title_puts "Creating launch config with ami_id #{@ami_id}"
puts `as-create-launch-config #{@launch_config_name} --group #{@security_group_name} --image-id #{@ami_id} --instance-type #{@instance_type} --spot-price "#{@max_price}"`
title_puts "Creating auto scaling group..."
puts `as-create-auto-scaling-group #{@autoscale_group_name} --availability-zones us-east-1b --default-cooldown 600 --min-size 0 --max-size #{@max_instances} --launch-configuration #{@launch_config_name} --vpc-zone-identifier #{@vpc_zone_identifier}`
title_puts "Creating scale up policy..."
puts (@scale_up_policy = `as-put-scaling-policy asp-workers-add --auto-scaling-group #{@autoscale_group_name} --adjustment=#{@max_instances} --type ChangeInCapacity --cooldown 0`)
title_puts "Creating scale down policy..."
puts (@scale_down_policy = `as-put-scaling-policy asp-workers-del --auto-scaling-group #{@autoscale_group_name} --adjustment=-#{@max_instances} --type ChangeInCapacity --cooldown 0`)
title_puts "Creating scale up alarm..."
puts `mon-put-metric-alarm ma-workers-add --alarm-actions #{@scale_up_policy.shellescape} --comparison-operator GreaterThanOrEqualToThreshold --evaluation-periods 1 --metric-name TriggerWorkers --namespace \"Sidekiq: Metrics\" --period 60 --statistic Average --threshold 2`
title_puts "Creating scale down alarm..."
puts `mon-put-metric-alarm ma-workers-del --alarm-actions #{@scale_down_policy.shellescape} --comparison-operator LessThanOrEqualToThreshold --evaluation-periods 1 --metric-name TriggerWorkers --namespace \"Sidekiq: Metrics\" --period 60 --statistic Average --threshold 0`
puts `mon-describe-alarms-for-metric --metric-name TriggerWorkers --namespace "Sidekiq: Metrics"`
title_puts "Autoscale stategy created !"
Rake::Task["aws:ec2:autoscale:describe"].invoke
end
desc "Update ami_id in autoscale strategy"
task :update do
@ami_id = ENV['ami_id']
unless @ami_id
puts "ERROR : please provide ami_id=ami_id to update autoscale strategy"
exit
end
@max_price = ENV['max_price']
define_default_variables!
@launch_config_name = "your-launch-config-desired-name-#{rand(36**4).to_s(36)}" # same launch config name with rand
title_puts "Creating a new launch config with ami_id #{@ami_id} and launch config name #{@launch_config_name}..."
puts `as-create-launch-config #{@launch_config_name} --group #{@security_group_name} --image-id #{@ami_id} --instance-type #{@instance_type} --spot-price "#{@max_price}"`
title_puts 'Updating auto scaling group...'
puts `as-update-auto-scaling-group #{@autoscale_group_name} --launch-configuration #{@launch_config_name}`
title_puts "Autoscale stategy updated !"
puts "Use as-delete-launch-config #launch_config_name# to delete unused launch configurations"
puts `as-describe-launch-configs`
end
desc "Describe autoscale strategy"
task :describe do
title_puts "Describing launch configs..."
puts `as-describe-launch-configs`
title_puts 'Describing auto scaling groups...'
puts `as-describe-auto-scaling-groups`
title_puts 'Describing scaling policies...'
puts `as-describe-policies`
title_puts 'Describing metrics alarms'
puts `mon-describe-alarms-for-metric --metric-name TriggerWorkers --namespace "Sidekiq: Metrics"`
end
desc "Delete autoscale startegy"
task :delete do
title_puts "Deleting alarms..."
res = `mon-describe-alarms-for-metric --metric-name TriggerWorkers --namespace "Sidekiq: Metrics"` # You need to change the namespace here to fit yours
unless res.start_with?('No alarms found')
res.split("\n").each do |alarm|
puts "--> Deleting #{alarm.split.first}"
puts `mon-delete-alarms #{alarm.split.first} -f`
end
else
puts "--> No alarms found"
end
title_puts "Deleting scaling policies..."
res = `as-describe-policies`
unless res.start_with?('No policies found')
res.split("\n")[1..-1].each do |policy|
puts "--> Deleting #{policy.split[2]}"
puts `as-delete-policy --auto-scaling-group #{policy.split[1]} --name #{policy.split[2]} -f`
end
else
puts "--> No policies found"
end
title_puts "Deleting scaling groups..."
res = `as-describe-auto-scaling-groups`
unless res.start_with?('No AutoScaleGroups found')
res.split("\n").each do |group|
if group.split[0] == "AUTO-SCALING-GROUP"
puts "--> Deleting #{group.split[1]}"
puts `as-delete-auto-scaling-group #{group.split[1]} -f --force-delete`
end
end
else
puts "--> No AutoScaleGroups found"
end
title_puts "Deleting launch configurations..."
res = `as-describe-launch-configs`
unless res.start_with?('No launch configurations found')
res.split("\n").each do |launch_config|
puts "--> Deleting #{launch_config.split[1]}"
puts `as-delete-launch-config #{launch_config.split[1]} -f`
end
else
puts "--> No launch configurations found"
end
title_puts "Autoscale stategy deleted !"
end
end
end
@zitooon
Copy link
Author

zitooon commented Apr 29, 2014

This will work with autoScaling-1.0.61.2 installed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment