Auto Scaling
Developer Guide (API Version 2011-01-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Auto Scaling with Alarms and Load Balancing

In this example, you set up an Amazon EC2 application to be load-balanced and auto-scaled with a minimum number of two instances and maximum number of twenty instances. Auto Scaling in this example is configured to scale out by one when the application’s average CPU usage exceeds a threshold of 80 percent for 10 minutes, and scale in by one when the application's average CPU usage drops below 40 percent for 10 minutes. You use Amazon CloudWatch alarms to alert the application when a threshold is breached.

This example assumes that you have the following:

  • An Amazon Machine Image (AMI) for your Amazon EC2 application

  • A defined LoadBalancer named MyLB configured for Availability Zones us-east-1a and us-east-1b

  • Installed Amazon CloudWatch command line tools if you're planning on using the command line interface.

[Note]Note

For information on how to create an AMI, go to Creating Your Own AMIs in the Amazon Elastic Compute Cloud User Guide. For more information about Elastic Load Balancing, go to the Elastic Load Balancing Developer Guide. For information on installing the Amazon CloudWatch command line tool, see Command Line Tools.

[Note]Note

This scenario is for a load-balanced, auto-scaled application. In the following examples, if you aren't using Elastic Load Balancing, omit the list of LoadBalancers from step 2.

API Example

To set up an auto-scaled, load-balanced Amazon EC2 application

  1. Call CreateLaunchConfiguration with the following parameters:

    • ImageId = ami-xxxxxxxx

    • LaunchConfigurationName = MyLC

    • InstanceType = m1.small

  2. Call CreateAutoScalingGroup with the following parameters:

    • AutoScalingGroupName = MyAutoScalingGroup

    • AvailabilityZones = us-east-1a, us-east-1b

    • LaunchConfigurationName = MyLC

    • LoadBalancerNames = MyLB

    • MaxSize = 20

    • MinSize = 2

  3. Call PutScalingPolicy with the following parameters:

    • AutoScalingGroupName = MyAutoScalingGroup

    • PolicyName = MyScaleUpPolicy

    • ScalingAdjustment = 1

    • AdjustmentType = ChangeInCapacity

    • Cooldown = 300

    [Note]Note

    The PutScalingPolicy action will return an ARN that represents your policy. Use your policy ARN, contained in the PolicyARN response element, to associate your policy with the CloudWatch alarm that you will create in the following step.

  4. Call PutMetricAlarm from the CloudWatch API with the following parameters:

    • AlarmName = MyHighCPUAlarm

    • AlarmActions = POLICY-ARN_from_previous_step

    • MetricName = CPUUtilization

    • Namespace = AWS/EC2

    • Statistic = Average

    • Period = 600

    • EvaluationPeriods = 1

    • Threshold = 80

    • ComparisonOperator = GreaterThanThreshold

    • Dimensions = (Name=AutoScalingGroupName, Value=MyAutoScalingGroup)

  5. Call PutScalingPolicy again with the following parameters:

    • AutoScalingGroupName = MyAutoScalingGroup

    • PolicyName = MyScaleDownPolicy

    • ScalingAdjustment = -1

    • AdjustmentType = ChangeInCapacity

    • Cooldown = 300

    [Note]Note

    The PutScalingPolicy action will return an ARN that represents your scale-down policy. Use your policy ARN, contained in the PolicyARN response element, to associate your policy with the CloudWatch alarm that you will create in the following step.

  6. Call PutMetricAlarm from the CloudWatch API with the following parameters:

    • AlarmName = MyLowCPUAlarm

    • AlarmActions = POLICY-ARN_from_previous_step

    • MetricName = CPUUtilization

    • Namespace = AWS/EC2

    • Statistic = Average

    • Period = 600

    • EvaluationPeriods = 1

    • Threshold = 40

    • ComparisonOperator = LessThanThreshold

    • Dimensions = (Name=AutoScalingGroupName, Value=MyAutoScalingGroup)

Your Amazon EC2 application has been launched as an auto-scaled and load-balanced application.

Command Line Tools Example

In this example, you will need to enter commands for both Auto Scaling and Amazon CloudWatch.

To set up an auto-scaled, load-balanced Amazon EC2 application

  1. Use the Auto Scaling as-create-launch-config command.

    PROMPT>as-create-launch-config MyLC --image-id ami-xxxxxxxx --instance-type m1.small

    Auto Scaling returns the following:

    OK-Created launch config
  2. Use the Auto Scaling as-create-auto-scaling-group command.

    PROMPT>as-create-auto-scaling-group MyAutoScalingGroup --launch-configuration MyLC --availability-zones us-east-1a us-east-1b  --min-size 2 --max-size 20 --load-balancers MyLB

    Auto Scaling returns the following:

    OK-Created AutoScalingGroup
  3. Use the Auto Scaling as-put-scaling-policy command to create a policy to enlarge your fleet.

    PROMPT>as-put-scaling-policy MyScaleUpPolicy --auto-scaling-group MyAutoScalingGroup  --adjustment=1 --type ChangeInCapacity  --cooldown 300

    Auto Scaling returns the following (example output):

    POLICY-ARN arn:aws:autoscaling:us-east-1:0123456789:scalingPolicy/abc-1234-def-567		
    [Note]Note

    The as-put-scaling-policy command returns an ARN that represents your policy. Use your policy ARN to associate your policy with the CloudWatch alarm that you will create in the following step.

  4. Use the CloudWatch mon-put-metric-alarm command, substituting your POLICY-ARN for the --alarm-actions parameter.

    PROMPT>mon-put-metric-alarm MyHighCPUAlarm  --comparison-operator  GreaterThanThreshold  --evaluation-periods  1 --metric-name  CPUUtilization  --namespace  "AWS/EC2"  --period  600  --statistic Average --threshold  80 --alarm-actions POLICY-ARN_from_previous_step --dimensions "AutoScalingGroupName=MyAutoScalingGroup"

    Amazon CloudWatch returns the following:

    OK-Created Alarm
  5. Use the Auto Scaling as-put-scaling-policy command to create a policy to reduce your fleet size. If you are using Windows, wrap the --adjustment parameter in quotation marks: "--adjustment=-1".

    PROMPT>as-put-scaling-policy MyScaleDownPolicy --auto-scaling-group MyAutoScalingGroup  --adjustment=-1 --type ChangeInCapacity  --cooldown 300

    Auto Scaling returns the following (example output):

    POLICY-ARN arn:aws:autoscaling:us-east-1:0123456789:scalingPolicy/abc-1234-def-567		
    [Note]Note

    The as-put-scaling-policy command returns an ARN that represents your policy. Use your policy ARN to associate your policy with the CloudWatch alarm that you will create in the following step.

  6. Use the CloudWatch mon-put-metric-alarm command, substituting the MyScaleDownPolicy POLICY-ARN for the ARN shown in the --alarm-actions parameter.

    PROMPT>mon-put-metric-alarm MyLowCPUAlarm  --comparison-operator  LessThanThreshold --evaluation-periods  1 --metric-name  CPUUtilization --namespace  "AWS/EC2"  --period  600  --statistic Average --threshold  40  --alarm-actions POLICY-ARN_from_previous_step --dimensions "AutoScalingGroupName=MyAutoScalingGroup"

    Amazon CloudWatch returns the following:

    OK-Created Alarm
  7. Verify that your Auto Scaling group exists by using the as-describe-auto-scaling-groups command.

    PROMPT>as-describe-auto-scaling-groups MyAutoScalingGroup --headers

    Auto Scaling returns the following:

    AUTO-SCALING-GROUP  GROUP-NAME          LAUNCH-CONFIG  AVAILABILITY-ZONES     LOAD-BALANCERS  MIN-SIZE  MAX-SIZE  DESIRED-CAPACITY
    AUTO-SCALING-GROUP  MyAutoScalingGroup  MyLC           us-east-1b,us-east-1a  MyLB            2         20        2
    INSTANCE  INSTANCE-ID  AVAILABILITY-ZONE  STATE      STATUS   LAUNCH-CONFIG
    INSTANCE  i-xxxxxxxx   us-east-1b         InService  Healthy  MyLC
    INSTANCE  i-xxxxxxxx   us-east-1a         InService  Healthy  MyLC

Your Amazon EC2 application has been launched as an auto-scaled and load-balanced application.