Thursday, May 30, 2013

AWS Auto Scaling Part 3 - Auto Scaling Based on Demand

In this example, we will go through an example that will auto scaling your group based on CPU Utilization. Alternatively, you can also make your group scale in or out based on other metrics like memory usage, IO throughput, etc.

We will set up a auto scaling group with elastic load balancing. Scale out by adding one instance when the CPU utilization is above 80% for 10 mins. Scale in by subtracting one instance when the CPU utilization is below 40% for 10 mins.

There will be 6 things we need to do:
  1. create a launch configuration
  2. create an auto scaling group
  3. create a scale out policy
  4. create a scale in policy
  5. create a alarm attached to the scale out policy
  6. create an alarm attached to the scale in policy

Create a Launch Configuration

We will create a launch configuration called NodeJS.
as-create-launch-config NodeJS --image-id ami-87acc4ee --instance-type m1.large --block-device-mapping="/dev/sda1=snap-1f356ee2, /dev/sdf=snap-18356ee5, /dev/sdg=snap-15356ee8, /dev/sdb=ephemeral0" --group your_security_group --key your_key_pair 
This is same as what we have at the end of Part 2 - Auto Scaling Based on Fixed Number of Instances.


Create an Auto Scaling Group 

We will create an auto scaling group called NodeJSGroup.
as-create-auto-scaling-group NodeJSGroup --launch-configuration NodeJS --availability-zones us-east-1d --min-size 1 --max-size 5 --tag "k=name, v=AsNodeJSProd, p=true" --load-balancers TestNodeBalancer
We have added a elastic load balancer called TestNodeBalancer. You can create a load balancer in the AWS EC2 console.

Check all your available load balancers:
elb-describe-lbs --headers
This is same as what we have at the end of Part 2 - Auto Scaling Based on Fixed Number of Instances.


Create a Scale out Policy

We will create a scale out policy such that a new instance will be added to the auto scaling group when the CPU utilization reaches 80% or above for over 10 mins. We will label this policy NodeJSScaleOutPolicy.

as-put-scaling-policy NodeJSScaleOutPolicy --auto-scaling-group NodeJSGroup --adjustment=1 --type ChangeInCapacity --cooldown 300
Take note of the Amazon Resource  Name (ARN) when it returns as a response. You will need this to set up the alarm.
arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:f0037fff-0949-4123-8887-f6c7064b8253:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleOutPolicy
The --cooldown = 300 parameter means that there needs to be 300 seconds gap before the policy can be applied again.

You can check the policy by
as-describe-policies

Create a Scale in Policy

We will create a scale in policy such that an instance will be removed from the auto scaling group when the CPU utilization reaches 40% or below for over 10 mins.
as-put-scaling-policy NodeJSScaleInPolicy --auto-scaling-group NodeJSGroup --adjustment=-1 --type ChangeInCapacity --cooldown 300

Install CloudWatch API

We will need to associate the alarms with the scale in and scale out policies. To do that, we need to install the CloudWatch Command Line Tools.


Associate Alarms with Policies

Make an alarm called NodeJSHighAlarm and associate it with NodeJSScaleOutPolicy.
mon-put-metric-alarm NodeJSHighAlarm --comparison-operator GreaterThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 80 --alarm-actions arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:f0037fff-0949-4123-8887-f6c7064b8253:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleOutPolicy --dimensions "AutoScalingGroupName=NodeJSGroup"

Make another alarm called NodeJSLowAlarm and associate it with NodeJSScaleInPolicy.
mon-put-metric-alarm NodeJSLowAlarm --comparison-operator LessThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 40 --alarm-actions arn:aws:autoscaling:us-east-1:240591131275:scalingPolicy:631fd578-9517-42e2-a424-8b1ed8dd0874:autoScalingGroupName/NodeJSGroup:policyName/NodeJSScaleInPolicy  --dimensions "AutoScalingGroupName=NodeJSGroup"
Check the status of the alarms
mon-describe-alarms --headers 

1 comment: