Sub topic 6
Creating metric alarms
This chapter assumes you can run AWS CLI commands and that you know what a metric, a namespace, a dimension, a statistic, and a period are. A topic for an SNS notification should already exist, or you should be able to create one.
What a metric alarm does
A metric alarm watches one metric, or the result of a metric math expression, and compares it to a threshold. CloudWatch evaluates the alarm on a schedule and sets one of three states:
| State | Meaning |
|---|---|
| OK | The metric is within the threshold. |
| ALARM | The metric breached the threshold according to your evaluation rules. |
| INSUFFICIENT_DATA | Not enough data has arrived to decide. A newly created alarm starts here. |
An alarm acts on state changes, not on individual data points. This gives you a place to put noise control and to decide what missing data means.
Creating an alarm
This command creates an alarm on the CPU of one EC2 instance. It notifies an SNS topic.
aws cloudwatch put-metric-alarm \
--alarm-name high-cpu-web \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--statistic Average \
--period 300 \
--evaluation-periods 3 \
--datapoints-to-alarm 2 \
--threshold 80 \
--comparison-operator GreaterThanOrEqualToThreshold \
--treat-missing-data notBreaching \
--alarm-actions arn:aws:sns:us-east-1:111122223333:ops-alertsEach option answers one question:
- Which metric?
--namespace,--metric-name, and--dimensionsmust match the published metric exactly. An alarm onInstanceIddoes not see data published with extra dimensions. - How is data summarized?
--statisticand--periodreduce raw data points to one value per period. Here, each value is a five-minute average. - What counts as a breach?
--thresholdand--comparison-operatordefine it. The operators areGreaterThanOrEqualToThreshold,GreaterThanThreshold,LessThanThreshold,LessThanOrEqualToThreshold, and the band operators used with anomaly detection. - How many breaches trigger it?
--evaluation-periodsand--datapoints-to-alarmset the rule, explained below. - What happens on missing data?
--treat-missing-datadecides. - What happens on a state change?
--alarm-actionsruns when the alarm enters ALARM. Actions are covered in the next chapter.
The alarm lives in one region and must be created in the same region as the metric. A call with an existing alarm name overwrites that alarm's configuration.
Evaluation periods and datapoints to alarm
CloudWatch looks at the last N periods, where N is --evaluation-periods. It goes to ALARM if at least M of them breach, where M is --datapoints-to-alarm. This is an "M out of N" rule. The breaching periods do not have to be consecutive.
If you omit --datapoints-to-alarm, it equals --evaluation-periods, so every period must breach.
For high-cpu-web, N is 3 and M is 2:
| Period (5 min) | Average CPU | Breaching? |
|---|---|---|
| 18:40 | 85 | Yes |
| 18:45 | 70 | No |
| 18:50 | 90 | Yes |
Two of three periods breach, so the alarm goes to ALARM. A single spike never triggers it, and neither does a brief dip that interrupts a real problem.
Choose the rule by the cost of a false alarm.
- 1 out of 1 reacts fastest and is the noisiest.
- 3 out of 3 is slow and calm.
- 2 out of 3, or 3 out of 5, tolerates one bad reading in either direction.
The total window is period times evaluation periods. CloudWatch limits it, so very long windows are rejected. Standard-resolution metrics need a period that is a multiple of 60 seconds. Metrics published at high resolution can use 10 or 30 seconds.
Treating missing data on purpose
A metric can have no data point in a period. A stopped instance, an idle application, and a failed agent all cause this. The --treat-missing-data option has four values:
| Value | Effect |
|---|---|
| `missing` | The default. Missing periods are ignored. If every period in the window is missing, the alarm goes to INSUFFICIENT_DATA. |
| `notBreaching` | A missing period counts as within the threshold. |
| `breaching` | A missing period counts as a breach. |
| `ignore` | The alarm keeps its current state. |
Pick the value that matches how the metric behaves:
- Sparse event counters. A metric like
ErrorCountpublishes only when something happens. UsenotBreaching, or the alarm sits in INSUFFICIENT_DATA whenever things are healthy. - Heartbeat metrics. Silence is the failure you want to detect. Use
breaching, so a stopped publisher raises the alarm. - Flapping metrics. If the metric arrives irregularly and you only want to react to real values, use
ignore.
With the default, a dead service can leave you with an alarm in INSUFFICIENT_DATA. Nothing notifies you, because the default alarm action covers ALARM only. This is the most common silent failure.
You can also attach actions to the INSUFFICIENT_DATA state with --insufficient-data-actions.
Percentiles and other statistics
--statistic accepts Average, Sum, Minimum, Maximum, and SampleCount. For percentiles such as p99 latency, use --extended-statistic p99 instead. Do not give both.
With percentiles, a period may contain very few samples, and the result is then unreliable. The --evaluate-low-sample-count-percentile option can be set to ignore. The alarm then skips periods that have too few samples.
Testing and inspecting an alarm
To check an alarm without waiting for a real incident, force a state:
aws cloudwatch set-alarm-state \
--alarm-name high-cpu-web \
--state-value ALARM \
--state-reason "Testing the notification path"This runs the alarm actions, so you can confirm the SNS email or subscription works. The state is temporary. CloudWatch re-evaluates the alarm at the next evaluation and sets the real state.
To read the current state and see why it changed:
aws cloudwatch describe-alarms \
--alarm-names high-cpu-web \
--query "MetricAlarms[0].[StateValue,StateReason]"
aws cloudwatch describe-alarm-history \
--alarm-name high-cpu-web \
--history-item-type StateUpdateThe StateReason text lists the data points that CloudWatch evaluated. Read it first when an alarm behaves unexpectedly.
Common errors
- The alarm stays in INSUFFICIENT_DATA. The namespace, metric name, or dimensions do not match the published data, or the metric is published less often than your period. Check the dimensions with
aws cloudwatch list-metrics. - The alarm exists but notifies nobody. The alarm action points to a topic that has no confirmed subscription, or the topic policy does not allow CloudWatch to publish.
- The alarm flaps between OK and ALARM. The window is too short. Raise
--evaluation-periods, and consider M out of N. - A low-traffic alarm never fires. A
SumorAverageover mostly empty periods hides the problem. Check how missing data is treated. - The alarm is in the wrong region. It evaluates only metrics in its own region.
- The wrong statistic hides spikes. An
Averageover five minutes can stay under 80 whileMaximumhits 100. Match the statistic to what hurts users.
Exercise
A custom metric ErrorCount is published only when an error occurs. You create an alarm with --statistic Sum --period 60 --evaluation-periods 5 --datapoints-to-alarm 3 --threshold 10 --comparison-operator GreaterThanOrEqualToThreshold and the default missing-data setting. For hours the alarm shows INSUFFICIENT_DATA even though the application is healthy. Why, and what do you change? Also, how many periods must breach for ALARM?
Show answer
A healthy application publishes no data points, so every period is missing. With the default missing setting, the alarm moves to INSUFFICIENT_DATA when all periods in the window are missing. Set --treat-missing-data notBreaching so empty periods count as within the threshold, and the alarm returns to OK. Three of the last five one-minute periods must have a Sum of at least 10 for the alarm to enter ALARM.