AWS Auto Scaling Groups

JNS consultants have had multiple occasions to work with clients that host their infrastructure in the Amazon cloud.  One key feature of AWS is the ability to horizontally scale your environment using auto-scaling groups.  Some clients are intimidated by this, but auto-scaling groups are fairly simple to setup and maintain.  Below is a simple script that creates an auto-scaling group using command line tools that are pre-installed on the Amazon linux distribution.  The script below is stripped down for simplicity.  JNS uses similar but more advanced scripts for all manner of AWS system management.  If you would like the assistance of AWS experts, please contact JNS.

#!/bin/bash

ASG_NAME='ASG name'        # An arbitrary name for the ASG
CONSOLE_NAME='name'        # The name launched instances are given
AMIID='ami-xxxxxxxxxx'     # The AMI that will be launched
INSTANCE_TYPE=m1.large     # The instance type
SSHKEY='key'               # The name of the EC2 key to use
SECURITY_GROUPS='g1,g2'    # List of security groups for instances
AZ='us-east-1a,us-east-1b' # availability zones to be used
MIN_SIZE=4                 # minimum number of instances running
MAX_SIZE=100               # maximum number of instances running
NUM_SCALE_UP=2             # Number of instances to be launched
NUM_SCALE_DOWN=-1          # Number of instances to shutdown
ELB='Some ELB'             # Name of the ELB used by the instances

# First create a launch config that defines the AMI to use
# instance type, ssh key and security groups
as-create-launch-config $ASG_NAME-launch \
       --image-id $AMIID \
       --instance-type $INSTANCE_TYPE \
       --key $SSHKEY \
       --group $SECURITY_GROUPS

# Next define an auto-scale group
as-create-auto-scaling-group $ASG_NAME-group $CREATE_PARAMS \
       --availability-zones $AZ \
       --launch-configuration $ASG_NAME-launch \
       --max-size $MAX_SIZE --min-size $MIN_SIZE \
       --tag "k=Name, v=$CONSOLE_NAME, p=true"

# Create a policy to be used for scaling up and scaling down
SCALE_UP=`as-put-scaling-policy $ASG_NAME-pol-up \
       --auto-scaling-group $ASG_NAME-group \
       --adjustment=$NUM_SCALE_UP \
       --type ChangeInCapacity`
SCALE_DOWN=`as-put-scaling-policy $ASG_NAME-pol-down \
       --auto-scaling-group $ASG_NAME-group \
       --adjustment=$NUM_SCALE_DOWN" \
       --type ChangeInCapacity`

# create a cloudwatch alarm which has the above policies
# set as the action.  This one triggers based on average
# CPU aggregated across members of the auto-scaling group
mon-put-metric-alarm --alarm-name $ASG_NAME-scale-up \
       --alarm-description "Scale up at 40% load" \
       --metric-name CPUUtilization \
       --namespace AWS/EC2 \
       --dimensions AutoScalingGroupName=$ASG_NAME-group \
       --statistic Average \
       --period 60 \
       --threshold 40 \
       --comparison-operator GreaterThanThreshold \
       --evaluation-periods 3 \
       --unit Percent --alarm-actions $SCALE_UP
mon-put-metric-alarm --alarm-name $ASG_NAME-scale-down \
       --alarm-description "Scale down below 10% load" \
       --metric-name CPUUtilization \
       --namespace AWS/EC2 \
       --dimensions AutoScalingGroupName=$ASG_NAME-group \
       --statistic Average \
       --period 60 \
       --threshold 10 \
       --comparison-operator LessThanThreshold \
       --evaluation-periods 3 \
       --unit Percent --alarm-actions $SCALE_DOWN
This entry was posted in AWS/Amazon and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s