Create an alarm that triggers an Auto Scaling action
With a metric alarm, you can trigger an Auto Scaling scaling policy to add or remove capacity when a metric breaches a threshold. This example scales an Auto Scaling group when the average CPU of the group exceeds a threshold.
You first create the scaling policy, and then attach its ARN as the alarm action. This applies to simple and step scaling policies. For target tracking scaling policies, the Auto Scaling service creates and manages the alarms for you.
Composite alarms can't trigger Auto Scaling actions. For more information, see Auto Scaling actions.
Setting up an Auto Scaling action alarm using the Amazon Web Services Management Console
Use the Amazon Web Services Management Console to create an alarm that triggers an Auto Scaling action. Because the full workflow has more than seven steps, it is divided into two parts.
Part 1: Select the metric and set the condition
Open the CloudWatch console at https://console.amazonaws.cn/cloudwatch/
. -
In the navigation pane, choose Alarms, All alarms.
-
Choose Create alarm.
-
Choose Select metric.
-
Select the AWS/EC2 namespace, and then select By Auto Scaling Group. Find the row with CPUUtilization for the AutoScalingGroupName dimension set to your group. Select the check box next to this row, and choose Select metric.
-
For Statistic, choose Average, and choose a period (for example,
5 minutes). -
Under Conditions, for Threshold type choose Static. For Whenever CPUUtilization is, choose Greater, and under than... enter
70. Then choose Next.
Part 2: Configure the action and create the alarm
-
Under Notification, configure an Amazon SNS notification, or choose Remove if you don't want one.
-
Choose Add EC2 Auto Scaling action. Select the alarm state that triggers the action, select the Auto Scaling group, and then select the scaling policy to run. Then choose Next.
-
Under Name and description, enter a name and description for the alarm, and choose Next.
-
Under Preview and create, choose Create alarm.
It might take a few minutes for the alarm to become active.
Setting up an Auto Scaling action alarm using the Amazon CLI
Use the Amazon CLI to create an alarm that triggers an Auto Scaling action.
To create an alarm that triggers an Auto Scaling action
-
Create a simple scaling policy using the put-scaling-policy command. Replace
my-asgandscale-outwith your Auto Scaling group name and policy name.aws autoscaling put-scaling-policy --auto-scaling-group-namemy-asg--policy-namescale-out--policy-type SimpleScaling --adjustment-type ChangeInCapacity --scaling-adjustment 1 --cooldown 300After the command runs, copy the
PolicyARNvalue from the output to use in the next step. The output looks similar to the following:{ "PolicyARN": "arn:aws:autoscaling:us-east-1:111122223333:scalingPolicy:12345678-1234-1234-1234-123456789012:autoScalingGroupName/my-asg:policyName/scale-out" } -
Create the alarm using the put-metric-alarm command. Replace
my-asgwith your Auto Scaling group name, and replace the ARN in--alarm-actionswith thePolicyARNvalue from the previous step.aws cloudwatch put-metric-alarm --alarm-nameasg-cpu-high--alarm-description "Scale out when CPU exceeds 70%" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=AutoScalingGroupName,Value=my-asg--evaluation-periods 2 --alarm-actionsarn:aws:autoscaling:us-east-1:111122223333:scalingPolicy:...:policyName/scale-outIf the command succeeds, it returns no output.