Skip to content

Instantly share code, notes, and snippets.

@jarpy
Created May 25, 2012 02:28
Show Gist options
  • Save jarpy/2785420 to your computer and use it in GitHub Desktop.
Save jarpy/2785420 to your computer and use it in GitHub Desktop.
AWS Auto Scaling with boto
import boto
from boto.ec2.autoscale import ScalingPolicy
from boto.ec2.cloudwatch import MetricAlarm
autoscale = boto.connect_autoscale()
cloudwatch = boto.connect_cloudwatch()
# Let's assume you already have an Auto Scaling group.
# Setting one up is well documented elsewhere.
autoscaling_group = 'as_group_0'
# Define some Scaling Policies. These tell Auto Scaling _how_ to scale
# group, but not when to do it. (We'll define that later).
# We need one policy for scaling up...
scale_up_policy = ScalingPolicy(
name='scale_up', adjustment_type='ChangeInCapacity',
as_name=autoscaling_group, scaling_adjustment=1, cooldown=180)
#...and one for scaling down again.
scale_down_policy = ScalingPolicy(
name='scale_down', adjustment_type='ChangeInCapacity',
as_name=autoscaling_group, scaling_adjustment=-1, cooldown=180)
# The policy objects are now defined locally.
# Let's submit them to AWS.
autoscale.create_scaling_policy(scale_up_policy)
autoscale.create_scaling_policy(scale_down_policy)
# Now that the polices have been digested by AWS, they have extra properties
# that we aren't aware of locally. We need to refresh them by requesting them
# back again.
# Specifically we'll need the Amazon Resource Name (ARN) of each policy.
scale_up_policy = autoscale.get_all_policies(
as_group=autoscaling_group, policy_names=['scale_up'])[0]
scale_down_policy = autoscale.get_all_policies(
as_group=autoscaling_group, policy_names=['scale_down'])[0]
# Now we'll create CloudWatch alarms that will define _when_ to run the
# Auto Scaling policies.
# We wan't to measure the average CPU usage across the whole Auto Scaling
# group, rather than individual instances. We can define that as a CloudWatch
# "Dimension".
alarm_dimensions = {"AutoScalingGroupName": autoscaling_group}
# One alarm for when to scale up...
scale_up_alarm = MetricAlarm(
name='scale_up_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='>', threshold='70',
period='60', evaluation_periods=2,
alarm_actions=[scale_up_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_up_alarm)
# ...and one for when to scale down.
scale_down_alarm = MetricAlarm(
name='scale_down_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='<', threshold='40',
period='60', evaluation_periods=2,
alarm_actions=[scale_down_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_down_alarm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment