Sub topic 13
CloudWatch Metrics and Alarms That Drive Scaling
This chapter assumes you can run AWS CLI commands and know what an Auto Scaling group (ASG) and a target group are. It explains how CloudWatch turns raw measurements into scaling actions.
Metrics, statistics and periods
A metric is a time-ordered series of data points, such as CPUUtilization for one instance. CloudWatch groups metrics by namespace and dimension. The namespace AWS/EC2 holds instance metrics. The namespace AWS/ApplicationELB holds load balancer metrics. A dimension, such as AutoScalingGroupName, narrows a metric to one resource.
To use a metric you pick a statistic (Average, Sum, Maximum) and a period, which is the number of seconds each data point covers. Scaling usually relies on these metrics:
| Metric | Namespace | Typical statistic | Use |
|---|---|---|---|
| `CPUUtilization` | `AWS/EC2` | Average across the ASG | Compute-bound workloads |
| `RequestCountPerTarget` | `AWS/ApplicationELB` | Sum per target | Request-driven web tiers |
| `TargetResponseTime` | `AWS/ApplicationELB` | Average or p90 | Latency-driven scaling |
| `GroupInServiceInstances` | `AWS/AutoScaling` | Average | Checking capacity |
Basic EC2 monitoring publishes data every 5 minutes. Detailed monitoring publishes every minute. ASGs created from launch templates use detailed monitoring unless you turn it off. Detailed monitoring has an extra cost, but it shortens the time before an alarm can react.
How an alarm drives a scaling action
An alarm watches one metric and compares it with a threshold. It has three states: OK, ALARM and INSUFFICIENT_DATA. Two settings control sensitivity:
- Evaluation periods: how many recent periods the alarm examines.
- Datapoints to alarm: how many of those periods must breach the threshold. A setting of 3 out of 5 tolerates short spikes.
When an alarm enters ALARM, it can run a scaling policy. The policy changes the ASG's desired capacity within the minimum and maximum limits.
Creating a scaling alarm yourself
With step and simple scaling policies you create the alarm. This command raises an alarm when average CPU stays above 70 percent for three one-minute periods. Replace the policy ARN with the one returned by put-scaling-policy.
aws cloudwatch put-metric-alarm \
--alarm-name web-asg-high-cpu \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=AutoScalingGroupName,Value=web-asg \
--statistic Average \
--period 60 \
--evaluation-periods 3 \
--threshold 70 \
--comparison-operator GreaterThanThreshold \
--alarm-actions "$SCALE_OUT_POLICY_ARN"Target tracking policies differ. Auto Scaling creates and manages their alarms, and you should not edit or delete them. The target tracking policy for request count in the Topic's earlier example creates one alarm for scale-out and one for scale-in.
Common errors
INSUFFICIENT_DATA: the alarm has no data points. Check the dimension name and value, and confirm detailed monitoring is on.- Alarm never fires: the threshold, statistic or period does not match the metric.
SumofRequestCountfor the whole load balancer is not comparable with a per-target threshold. - Capacity does not change: the ASG is already at its maximum, or the instances are still in warmup.
- Flapping: the scale-in threshold sits too close to the scale-out threshold, so the group grows and shrinks repeatedly.
Exercise
An alarm uses 5 evaluation periods with 3 datapoints to alarm, each period 60 seconds. CPU exceeds the threshold for two non-consecutive minutes within a five-minute window. Does the alarm enter ALARM?
Show answer
No. Only 2 of the 5 datapoints breach the threshold, and 3 are required. The alarm stays in OK until a third datapoint in the 5-period window breaches.