| Did this page help you? Yes No Tell us about it... |
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 |
|---|---|
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 |
|---|---|
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. |
To set up an auto-scaled, load-balanced Amazon EC2 application
Call CreateLaunchConfiguration with the following parameters:
ImageId = ami-xxxxxxxx
LaunchConfigurationName = MyLC
InstanceType = m1.small
Call CreateAutoScalingGroup with the following parameters:
AutoScalingGroupName = MyAutoScalingGroup
AvailabilityZones = us-east-1a, us-east-1b
LaunchConfigurationName = MyLC
LoadBalancerNames = MyLB
MaxSize = 20
MinSize = 2
Call PutScalingPolicy with the following parameters:
AutoScalingGroupName = MyAutoScalingGroup
PolicyName = MyScaleUpPolicy
ScalingAdjustment = 1
AdjustmentType = ChangeInCapacity
Cooldown = 300
![]() | Note |
|---|---|
The |
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)
Call PutScalingPolicy again with the following parameters:
AutoScalingGroupName = MyAutoScalingGroup
PolicyName = MyScaleDownPolicy
ScalingAdjustment = -1
AdjustmentType = ChangeInCapacity
Cooldown = 300
![]() | Note |
|---|---|
The |
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.
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
Use the Auto Scaling as-create-launch-config command.
PROMPT>as-create-launch-config MyLC --image-id ami-xxxxxxxx --instance-type m1.smallAuto Scaling returns the following:
OK-Created launch config
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 MyLBAuto Scaling returns the following:
OK-Created AutoScalingGroup
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 300Auto Scaling returns the following (example output):
POLICY-ARN arn:aws:autoscaling:us-east-1:0123456789:scalingPolicy/abc-1234-def-567
![]() | Note |
|---|---|
The |
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
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 300Auto Scaling returns the following (example output):
POLICY-ARN arn:aws:autoscaling:us-east-1:0123456789:scalingPolicy/abc-1234-def-567
![]() | Note |
|---|---|
The |
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
Verify that your Auto Scaling group exists by using the as-describe-auto-scaling-groups command.
PROMPT>as-describe-auto-scaling-groups MyAutoScalingGroup --headersAuto 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.