Cloud optimize

Add an auto-scaling group and policy for Amazon EC2 machines

Nick Hardiman walks you through the steps of creating an auto-scaling group, as well as creating policies to scale-out and scale-in your Amazon EC2 machines.

I am setting up Amazon auto-scaling to make my service scalable. The ability to scale is one of my 12 principles of operational readiness. Here's what we've done so far:

  1. Make an AMI as the basis for new EC2 machines.
  2. Check the Amazon EC2 API Tools
  3. Install the Auto Scaling Command Line Tools
  4. Install Amazon CloudWatch Command Line Tools
  5. Add a Launch Configuration.

The five steps above are detailed in these previous posts:

Now, it's time to create the auto-scaling group, the scale-out policy, and the scale-in policy. It's all done on the command line using commands like as-create-auto-scaling-group, as-put-scaling-policy, and as-describe-policies.

These components are all linked together to make auto-scaling work. When CloudWatch monitoring notices the site is getting too busy for my fleet of EC2 machines to handle, it will trigger auto-scaling changes. The group will get changed, and the changes will be made according to the scale-out policy.

Sort out your labeling policy

All the things I create -- such as the launch configuration, policies and group -- get labeled. It is worth working out in advance a labeling approach that is understood by everyone working with the AWS tools. I like short names, based on their purpose, and made from initials. I'm going to label my new auto-scaling group cag01 (Customer-facing Auto-scaling Group no.1).

I know labels like clc01, cag01 and cap01-add are not pretty, but they do have some meaning embedded in them. I really don't like the kinds of creative labels some people pick based on collections of things, such as birds, elements from the periodic table, or the seven dwarves. I am easily confused by names like Orca, Humpback, and Sperm. What they are related to?

Add an auto-scaling group

Use the as-create-autoscaling-group command to create the cag01 group. This defines how much to scale, but not when to scale. This command immediately launches a new instance.

My-MacBook-Pro:~ nick$ as-create-auto-scaling-group  cag01 \
> --availability-zones  eu-west-1a,eu-west-1b \
> --default-cooldown  120 \
> --load-balancers  clb01 \
> --min-size  1 \
> --max-size  6 \
> --launch-configuration   clc01
OK-Created AutoScalingGroup
My-MacBook-Pro:~ nick$

What do those options mean?

  • availability-zones eu-west-1a,eu-west-1b -- The AWS behind-the-scenes robots create new machines and spread them evenly between these AZs.
  • default-cooldown 120 -- Wait 120 seconds (two minutes) between each scaling action.
  • load-balancers clb01 -- Add new machines to my load balancer configuration.
  • min-size 1 -- Keep one machine running at all times. The auto scaler does not count any machines I already have running.
  • max-size 6 -- Don't bust the bank by starting more than six machines.
  • launch-configuration clc01 -- Start the machine according to my launch rules.

Check your work. Always check your work.

My-MacBook-Pro:~ nick$ as-describe-auto-scaling-groups
AUTO-SCALING-GROUP  cag01  clc01  eu-west-1a,eu-west-1b  clb01  1  6  1
INSTANCE  i-9b0888d3  eu-west-1a  InService  Healthy  clc01
My-MacBook-Pro:~ nick$

A DBA will recognize this as a row from a table. If it is too hard to figure out what each value is, row headings can be added with the --headers option.

Add a scale-out policy

A policy defines a few more details about how to scale, but doesn't trigger any action. Use the as-put-scaling-policy command to create the policy cap01-add (Customer-facing Auto-scaling Policy no.1, for scaling out by adding EC2 machines). I also need a matching cap02-del deletion policy.

My-MacBook-Pro:~ nick$ as-put-scaling-policy cap01-add \
> --auto-scaling-group cag01  \
> --adjustment=1  \
> --type ChangeInCapacity  \
> --cooldown 300

arn:aws:autoscaling:eu-west-1:123494605340:scalingPolicy:45e9ed16-9462-4905-b9d4-cc99fcff0d43:autoScalingGroupName/cag01:policyName/cap01-add

My-MacBook-Pro:~ nick$

What do those options mean?

  • auto-scaling-group cag01 -- where to find the rules on how to scale
  • adjustment=1 -- start one server at a time.
  • type ChangeInCapacity -- check everything is OK when a value changes (this gets defined next)
  • cooldown 300 -- if something gets scaled, wait 300 seconds (five minutes) before trying again.

ARN and SNS

That response line starting with arn: is an Amazon SNS topic. SNS (Simple Notification Service) is a service for sending messages back and forth. An ARN (Amazon Resource Name) is a unique label called a topic name. This ARN is about 140 characters long, built by sticking lots of auto-scaling values together with hyphens.

Add a scale-in policy

Create the policy for scaling in (removing EC2 machines).

My-MacBook-Pro:~ nick$ as-put-scaling-policy cap01-del  \
> --auto-scaling-group cag01  \
> --adjustment=-1  \
> --type ChangeInCapacity
arn:aws:autoscaling:eu-west-1:123494605340:scalingPolicy:8bf0832a-45f3-446b-93bc-3b7a94609943:autoScalingGroupName/cag01:policyName/cap01-del
My-MacBook-Pro:~ nick$

Check your work.

My-MacBook-Pro:~ nick$ as-describe-policies  --headers
SCALING-POLICY  GROUP-NAME  POLICY-NAME  SCALING-ADJUSTMENT  ADJUSTMENT-TYPE   COOLDOWN  POLICY-ARN
SCALING-POLICY  cag01       cap01-add    1                   ChangeInCapacity  300       arn:aws:autoscaling:eu-west-1:123494605340:scalingPolicy:45e9ed16-9462-4905-b9d4-cc99fcff0d43:autoScalingGroupName/cag01:policyName/cap01-add
SCALING-POLICY  cag01       cap01-del    -1                  ChangeInCapacity            arn:aws:autoscaling:eu-west-1:123494605340:scalingPolicy:8bf0832a-45f3-446b-93bc-3b7a94609943:autoScalingGroupName/cag01:policyName/cap01-del
My-MacBook-Pro:~ nick$

It's looking good so far, but it does not work yet. There are still a few steps to go. Next, I add the CloudWatch monitoring.

About

Nick Hardiman builds and maintains the infrastructure required to run Internet services. Nick deals with the lower layers of the Internet - the machines, networks, operating systems, and applications. Nick's job stops there, and he hands over to the ...

0 comments