Menu
Auto Scaling
User Guide

Scaling Based on Metrics

You can create a scaling policy that uses CloudWatch alarms to determine when your Auto Scaling group should scale out or scale in. Each CloudWatch alarm watches a single metric and sends messages to Auto Scaling when the metric breaches a threshold that you specify in your policy. You can use alarms to monitor any of the metrics that the services in AWS that you're using send to CloudWatch, or you can create and monitor your own custom metrics.

When you create a CloudWatch alarm, you can specify an Amazon SNS topic to send an email notification to when the alarm changes state. For more information, see Create Amazon CloudWatch Alarms.

Create an Auto Scaling Group with Scaling Policies

Use the console to create an Auto Scaling group with two scaling policies: a scale out policy that increases the capacity of the group by 30 percent, and a scale in policy that decreases the capacity of the group to two instances.

To create an Auto Scaling group with scaling based on metrics

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under Auto Scaling, choose Auto Scaling Groups.

  3. Choose Create Auto Scaling group.

  4. On the Create Auto Scaling Group page, do one of the following:

    • Select Create an Auto Scaling group from an existing launch configuration, select an existing launch configuration, and then choose Next Step.

    • If you don't have a launch configuration that you'd like to use, choose Create a new launch configuration and follow the directions. For more information, see Creating a Launch Configuration.

  5. On the Configure Auto Scaling group details page, do the following:

    1. For Group name, type a name for your Auto Scaling group.

    2. For Group size, type the desired capacity for your Auto Scaling group.

    3. If the launch configuration specifies instances that require a VPC, such as T2 instances, you must select a VPC from Network. Otherwise, if your AWS account supports EC2-Classic and the instances don't require a VPC, you can select either Launch info EC2-Classic or a VPC.

    4. If you selected a VPC in the previous step, select one or more subnets from Subnet. If you selected EC2-Classic in the previous step, select one or more Availability Zones from Availability Zone(s).

    5. Choose Next: Configure scaling policies.

  6. On the Configure scaling policies page, do the following:

    1. Select Use scaling policies to adjust the capacity of this group.

    2. Specify the minimum and maximum size for your Auto Scaling group using the row that begins with Scale between. For example, if your group is already at its maximum size, you need to specify a new maximum in order to scale out.

      
                                    Specify the minimum and maximum size for your group.
    3. Specify your scale out policy under Increase Group Size. You can optionally specify a name for the policy, then choose Add new alarm.

    4. On the Create Alarm page, choose create topic. For Send a notification to, type a name for the SNS topic. For With these recipients, type one or more email addresses to receive notification. If you want, you can replace the default name for your alarm with a custom name. Next, specify the metric and the criteria for the policy. For example, you can leave the default settings for Whenever (Average of CPU Utilization). For Is, choose >= and type 80 percent. For For at least, type 1 consecutive period of 5 Minutes. Choose Create Alarm.

      
                                    Create Alarm
    5. For Take the action, choose Add, type 30 in the next field, and then choose percent of group. By default, the lower bound for this step adjustment is the alarm threshold and the upper bound is null (positive infinity). To add another step adjustment, choose Add step.

      (Optional) We recommend that you use the default to create both scaling policies with steps. If you need to create simple scaling policies, choose Create a simple scaling policy. For more information, see Scaling Policy Types.

      
                                    Create scale out policy
    6. Specify your scale in policy under Decrease Group Size. You can optionally specify a name for the policy, then choose Add new alarm.

    7. On the Create Alarm page, you can select the same notification that you created for the scale out policy or create a new one for the scale in policy. If you want, you can replace the default name for your alarm with a custom name. Keep the default settings for Whenever (Average of CPU Utilization). For Is, choose <= and type 40 percent. For For at least, type 1 consecutive period of 5 Minutes. Choose Create Alarm.

    8. For Take the action, choose Remove, type 2 in the next field, and then choose instances. By default, the upper bound for this step adjustment is the alarm threshold and the lower bound is null (negative infinity). To add another step adjustment, choose Add step.

      (Optional) We recommend that you use the default to create both scaling policies with steps. If you need to create simple scaling policies, choose Create a simple scaling policy. For more information, see Scaling Policy Types.

      
                                    Create scale out policy
    9. Choose Review.

    10. On the Review page, choose Create Auto Scaling group.

  7. Use the following steps to verify the scaling policies for your Auto Scaling group.

    1. The Auto Scaling Group creation status page confirms that your Auto Scaling group was successfully created. Choose View your Auto Scaling Groups.

    2. On the Auto Scaling Groups page, select the Auto Scaling group that you just created.

    3. On the Activity History tab, the Status column shows whether your Auto Scaling group has successfully launched instances.

    4. On the Instances tab, the Lifecycle column contains the state of your instances. It takes a short time for an instance to launch. After the instance starts, its lifecycle state changes to InService.

      The Health Status column shows the result of the EC2 instance health check on your instance.

    5. On the Scaling Policies tab, you can see the policies that you created for the Auto Scaling group.

Add a Scaling Policy to an Auto Scaling Group

Use the console to add a scaling policy to an existing Auto Scaling group.

To update an Auto Scaling group with scaling based on metrics

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under Auto Scaling, choose Auto Scaling Groups.

  3. Select the Auto Scaling group.

  4. On the Scaling Policies tab, choose Add policy.

  5. For Name, type a name for the policy, and then choose Create new alarm.

  6. On the Create Alarm page, choose create topic. For Send a notification to, type a name for the SNS topic. For With these recipients, type one or more email addresses to receive notification. If you want, you can replace the default name for your alarm with a custom name. Next, specify the metric and the criteria for the alarm, using Whenever, Is, and For at least. Choose Create Alarm.

  7. Specify the scaling activity for the policy using Take the action. By default, the lower bound for this step adjustment is the alarm threshold and the upper bound is null (positive infinity). To add another step adjustment, choose Add step.

    (Optional) We recommend that you use the default to create both scaling policies with steps. If you need to create simple scaling policies, choose Create a simple scaling policy. For more information, see Scaling Policy Types.

  8. Choose Create.

Configure Scaling Policies Using the AWS CLI

Use the AWS CLI as follows to configure scaling policies for your Auto Scaling group.

Step 1: Create an Auto Scaling Group

Use the following create-auto-scaling-group command to create an Auto Scaling group named my-asg using the launch configuration my-lc. If you don't have a launch configuration that you'd like to use, you can create one. For more information, see create-launch-configuration.

Copy
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --launch-configuration-name my-lc --max-size 5 --min-size 1 --availability-zones "us-west-2c"

Step 2: Create Scaling Policies

You can create scaling policies that tell the Auto Scaling group what to do when the specified conditions change.

Example: my-scaleout-policy

Use the following put-scaling-policy command to create a scaling policy named my-scaleout-policy with an adjustment type of PercentChangeInCapacity that increases the capacity of the group by 30 percent:

Copy
aws autoscaling put-scaling-policy --policy-name my-scaleout-policy --auto-scaling-group-name my-asg --scaling-adjustment 30 --adjustment-type PercentChangeInCapacity

Auto Scaling returns the ARN that serves as a unique name for the policy. Subsequently, you can use either the ARN or a combination of the policy name and group name to specify the policy. Store this ARN in a safe place. You'll need it to create CloudWatch alarms.

Copy
{ "PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:ac542982-cbeb-4294-891c-a5a941dfa787:autoScalingGroupName/my-asg:policyName/my-scaleout-policy }

Example: my-scalein-policy

Use the following put-scaling-policy command to create a scaling policy named my-scalein-policy with an adjustment type of ChangeInCapacity that decreases the capacity of the group by two instances:

Copy
aws autoscaling put-scaling-policy --policy-name my-scalein-policy --auto-scaling-group-name my-asg --scaling-adjustment -2 --adjustment-type ChangeInCapacity

Auto Scaling returns the ARN for the policy. Store this ARN in a safe place. You'll need it to create CloudWatch alarms.

Copy
{ "PolicyARN": "arn:aws:autoscaling:us-west-2:123456789012:scalingPolicy:4ee9e543-86b5-4121-b53b-aa4c23b5bbcc:autoScalingGroupName/my-asg:policyName/my-scalein-policy }

Step 3: Create CloudWatch Alarms

In step 2, you created scaling policies that provided instructions to the Auto Scaling group about how to scale out and scale in when the conditions that you specify change. In this step, you create alarms by identifying the metrics to watch, defining the conditions for scaling, and then associating the alarms with the scaling policies.

Example: AddCapacity

Use the following CloudWatch put-metric-alarm command to create an alarm that increases the size of the Auto Scaling group when the value of the specified metric breaches 80. For example, you can add capacity when the average CPU usage of all the instances (CPUUtilization) increases to 80 percent. To use your own custom metric, specify its name in --metric-name and its namespace in --namespace.

Copy
aws cloudwatch put-metric-alarm --alarm-name AddCapacity --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 120 --threshold 80 --comparison-operator GreaterThanOrEqualToThreshold --dimensions "Name=AutoScalingGroupName,Value=my-asg" --evaluation-periods 2 --alarm-actions PolicyARN

Example: RemoveCapacity

Use the following CloudWatch put-metric-alarm command to create an alarm that decreases the size of the Auto Scaling group when the value of the specified metric breaches 40. For example, you can remove capacity when the average CPU usage of all the instances (CPUUtilization) decreases to 40 percent. To use your own custom metric, specify its name in --metric-name and its namespace in --namespace.

Copy
aws cloudwatch put-metric-alarm --alarm-name RemoveCapacity --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 120 --threshold 40 --comparison-operator LessThanOrEqualToThreshold --dimensions "Name=AutoScalingGroupName,Value=my-asg" --evaluation-periods 2 --alarm-actions PolicyARN