2011-01-26

Autoscaling revisited

Shortly after writing my previous post about AWS autoscaling, Amazon updated the autoscaling methodology. Instead of triggers they now use autoscaling policies and alarms from CloudWatch to initiate the policy actions. So here's how I create and remove policies and alarms from an autoscaling group. Note: you can't have both triggers and policies on a group, you have to remove the triggers first before adding the policies.

#
# create policies that will scale the group up and down
# note: cooldown is how many seconds to wait before
# applying the policy again
#
export COOLDOWN=300
export SCALEUP=`as-put-scaling-policy $ASGROUP-scaleUp \
--auto-scaling-group $ASGROUP \
--cooldown $COOLDOWN \
--adjustment=1 \
--type ChangeInCapacity`
if [ $? -eq 0 ]; then echo OK - $SCALEUP; else echo ERROR; fi

export SCALEDOWN=`as-put-scaling-policy $ASGROUP-scaleDown \
--auto-scaling-group $ASGROUP \
--cooldown $COOLDOWN \
--adjustment=-1 \
--type ChangeInCapacity`
if [ $? -eq 0 ]; then echo OK - $SCALEDOWN; else echo ERROR; fi

#
# create alarms to implement policies
#

#
# example: Latency on the ELB
#
mon-put-metric-alarm \
--alarm-name $ASGROUP-HighLatency \
--namespace "AWS/ELB" \
--metric-name Latency \
--statistic Average \
--period 60 \
--comparison-operator GreaterThanThreshold \
--threshold 5.0 \
--unit Seconds \
--evaluation-periods 5 \
--dimensions "LoadBalancerName=$LBNAME" \
--alarm-actions $SCALEUP
if [ $? -eq 0 ]; then echo OK; else echo ERROR; fi

mon-put-metric-alarm \
--alarm-name $ASGROUP-LowLatency \
--namespace "AWS/ELB" \
--metric-name Latency \
--statistic Average \
--period 60 \
--comparison-operator LessThanThreshold \
--threshold 0.5 \
--unit Seconds \
--evaluation-periods 5 \
--dimensions "LoadBalancerName=$LBNAME" \
--alarm-actions $SCALEDOWN
if [ $? -eq 0 ]; then echo OK; else echo ERROR; fi


And to clean up:

mon-delete-alarms --alarm-name $ASGROUP-HighLatency --force
mon-delete-alarms --alarm-name $ASGROUP-LowLatency --force
as-delete-policy $ASGROUP-scaleUp --auto-scaling-group $ASGROUP --force
as-delete-policy $ASGROUP-scaleDown --auto-scaling-group $ASGROUP --force

No comments:

Ratings and Recommendations by outbrain