AWS Startups Blog

How to Use AWS Auto Scaling for Everything

Use Auto Scaling for Everything Possible

Guest post by Shane Meyers, SmugMug

 New users of AWS commonly create a handful of instances and manually configure them while experimenting and learning how everything in AWS works. This is a great advantage of AWS: Getting started is relatively painless. The big gotcha is when users continue using the test instances for production use, which can create a challenging situation if scaling is needed or an instance fails.

My recommendation is to use a configuration management tool for provisioning instances, such as Puppet or Chef, and have all instances be managed by Auto Scaling groups. This brings a few advantages: It helps you manage instance lifecycle, scales easily, and provides resiliency against failures. Scaling out by increasing the number of instances is as simple as increasing the desired capacity value for the Auto Scaling group. If an instance fails for any reason, Auto Scaling will replace it without any intervention.

Here’s how to build an example Auto Scaling group using the new CLI tools. This group will turn off three instances when the CPU average across the whole pool drops to 20% and add three instances when CPU reaches 70%. In the following example, replace ami-xxxxxx with your Amazon Machine Image (AMI) ID.

#!/bin/bash
set -e

AS=’foo’
LC=’foo’

# create launch config
aws autoscaling create-launch-configuration
–launch-configuration-name $LC
–image-id ami-xxxxxx
–instance-type m1.small
–instance-monitoring ‘{“Enabled”: true}’
–security-groups foo

# create AS group
aws autoscaling create-auto-scaling-group
–auto-scaling-group-name $AS
–launch-configuration-name $LC
–availability-zones us-east-1a us-east-1b us-east-1c
–min-size 3
–max-size 30
–load-balancer-names foo
–health-check-type ELB
–health-check-grace-period 1500

UP=$(aws autoscaling put-scaling-policy –auto-scaling-group-name $AS –policy-name scale-up –scaling-adjustment 3 –adjustment-type ChangeInCapacity —cooldown 300 | head -1)
DOWN=$(aws autoscaling put-scaling-policy –auto-scaling-group-name $AS –policy-name scale-down –scaling-adjustment -3 –adjustment-type ChangeInCapacity –cooldown 600 | head -1)

aws cloudwatch put-metric-alarm
–alarm-name $AS-CPUHigh
–metric-name CPUUtilization
–namespace “AWS/EC2”
–period 300
–evaluation-periods 1
–threshold 70
–statistic Average
–comparison-operator GreaterThanThreshold
–alarm-actions $UP
–dimensions Name=AutoScalingGroupName,Value=$AS

aws cloudwatch put-metric-alarm
–alarm-name $AS-CPULow
–metric-name CPUUtilization
–namespace “AWS/EC2”
–period 300
–evaluation-periods 1
–threshold 20
–statistic Average
–comparison-operator LessThanThreshold
–alarm-actions $DOWN
–dimensions Name=AutoScalingGroupName,Value=$AS

# Enable AS group (not instance) metrics in CloudWatch
aws autoscaling enable-metrics-collection
–auto-scaling-group-name $AS
–granularity 1Minute
–metrics GroupInServiceInstances GroupTotalInstances

I highly recommend placing the commands to create the Auto Scaling group in a shell script that can be checked into revision control. That way you can easily see how the group is configured and who made what changes. As changes are applied to the AS group, be sure to update the script to reflect reality.