Sub topic 14
Anomaly detection alarms
This chapter assumes you can create a standard metric alarm, know what a period and a statistic are, and have an SNS topic for notifications. Anomaly detection alarms replace the fixed threshold you chose by hand with a band that CloudWatch learns from the metric's history.
Why a fixed threshold sometimes fails
A static threshold such as "CPU at least 80 percent" works when normal behavior is flat. Many metrics are not flat. Request counts rise on weekday mornings and fall at night. A threshold high enough to avoid false alarms at noon misses a real problem at 3 a.m. A threshold low enough to catch the 3 a.m. problem fires every noon.
Anomaly detection fixes this by modeling the metric's expected range over time.
How the model works
When you enable anomaly detection on a metric, CloudWatch trains a machine learning model on up to two weeks of past data. The model accounts for hourly, daily, and weekly patterns. It produces an expected band: an upper and a lower bound for each point in time.
You control how wide the band is with a number called the anomaly detection threshold. It is the number of standard deviations from the expected value. A higher number gives a wider band and fewer alarms. The default is 2.
The model keeps retraining as new data arrives. It needs enough history to be useful, so a brand-new metric produces a poor band at first.
Creating the alarm
An anomaly detection alarm uses metric math. The ANOMALY_DETECTION_BAND function builds the band from a metric, and the alarm compares the metric to it. The comparison operator is one of three choices:
| Operator | Alarms when the metric is |
|---|---|
| `GreaterThanUpperThreshold` | above the band |
| `LessThanLowerThreshold` | below the band |
| `LessThanLowerOrGreaterThanUpperThreshold` | outside the band on either side |
The following command alarms when the request count of an Application Load Balancer leaves the band. Replace the load balancer dimension value with your own.
aws cloudwatch put-metric-alarm \
--alarm-name alb-requests-anomaly \
--evaluation-periods 3 \
--datapoints-to-alarm 2 \
--comparison-operator LessThanLowerOrGreaterThanUpperThreshold \
--threshold-metric-id ad1 \
--treat-missing-data notBreaching \
--metrics '[
{
"Id": "m1",
"ReturnData": true,
"MetricStat": {
"Metric": {
"Namespace": "AWS/ApplicationELB",
"MetricName": "RequestCount",
"Dimensions": [
{ "Name": "LoadBalancer", "Value": "app/my-alb/0123456789abcdef" }
]
},
"Period": 300,
"Stat": "Sum"
}
},
{
"Id": "ad1",
"Expression": "ANOMALY_DETECTION_BAND(m1, 2)",
"Label": "RequestCount expected range",
"ReturnData": true
}
]' \
--alarm-actions arn:aws:sns:us-east-1:111122223333:ops-alertsNote that the command has no --threshold. The --threshold-metric-id option points at the band expression instead. The 2 in ANOMALY_DETECTION_BAND(m1, 2) is the band width.
Common errors and gotchas
- Too little history. A new or rarely reported metric gives an unreliable band. Expect false alarms until the model has seen several days of data.
- A band that is too narrow. If the alarm fires on ordinary variation, raise the width from 2 to 3 or more.
- Excluding known events. A one-time load test or outage can distort the model. You can exclude time ranges from training so the model does not learn them as normal.
- Missing data. As with standard alarms, set
--treat-missing-datadeliberately. HerenotBreachingkeeps quiet nights from raising alarms. - Metrics with no pattern. For a metric with a hard limit, such as disk used percent, a static threshold is clearer and easier to explain.
Exercise
Why does the anomaly detection alarm command above use --threshold-metric-id instead of --threshold, and what would you change to make the alarm fire only when traffic drops unusually low?
Show answer
The threshold is not a fixed number. It is the band produced by the ANOMALY_DETECTION_BAND expression, so --threshold-metric-id names the expression's ID (ad1). To alarm only on unusually low traffic, change the comparison operator to LessThanLowerThreshold.