Sub topic 6
Creating metric alarms
This chapter assumes you can run AWS CLI commands, know what a CloudWatch metric, namespace, dimension, statistic and period are, and have an SNS topic available for notifications.
How an alarm evaluates a metric
A metric alarm watches one metric and compares a statistic against a threshold. It does this once per period, and it decides its state from the most recent evaluation periods. Three states exist:
OK: the threshold is not breached.ALARM: the threshold is breached.INSUFFICIENT_DATA: not enough data has arrived to decide.
Three settings control the decision. --period is the length of each data point in seconds. --evaluation-periods is how many recent periods the alarm looks at. --datapoints-to-alarm is how many of those periods must breach. Together they form an "M out of N" rule.
Create an alarm
The command below raises an alarm when average CPU is at least 80 percent in 2 of the last 3 five-minute periods. It also sends a message to 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-alertsThe namespace, metric name and dimensions must match the metric exactly. If one differs, the alarm watches a metric that has no data, and it stays in INSUFFICIENT_DATA.
Using --datapoints-to-alarm 2 with --evaluation-periods 3 means one brief spike does not trigger the alarm. Two breaching periods out of three do.
Handle missing data on purpose
Metrics can have gaps. An instance may be stopped, or an application may publish only when work happens. The --treat-missing-data option decides how the alarm counts a gap.
| Value | Missing periods are treated as |
|---|---|
| `missing` | Ignored. The alarm keeps its state, or moves to `INSUFFICIENT_DATA` if all periods are missing. This is the default. |
| `notBreaching` | Within the threshold. |
| `breaching` | Over the threshold. |
| `ignore` | Not evaluated. The alarm keeps its current state. |
Pick the value that matches what a gap means for your workload. A stopped instance is usually not a problem, so notBreaching fits the example above. For a heartbeat metric, silence is the failure, so breaching is the right choice.
Check the alarm
Confirm the alarm exists and read its current state:
aws cloudwatch describe-alarms --alarm-names high-cpu-web \
--query "MetricAlarms[0].[AlarmName,StateValue,StateReason]"To test the notification path without waiting for real load, force a state change:
aws cloudwatch set-alarm-state \
--alarm-name high-cpu-web \
--state-value ALARM \
--state-reason "Testing notification path"The alarm returns to its real state at the next evaluation.
Common errors
- Alarm stuck in
INSUFFICIENT_DATA: the dimensions, namespace or region do not match the metric. Compare them with the output ofaws cloudwatch list-metrics. - No notification arrives: the SNS topic has no confirmed subscription, or its access policy does not allow CloudWatch to publish.
- Alarm flaps between states: the period is too short or
datapoints-to-alarmequals 1. Widen the window or require more breaching points.
Exercise
An alarm uses --period 60, --evaluation-periods 5 and --datapoints-to-alarm 3. A web service publishes a request-count metric only while it is serving traffic, and silence for several minutes means the service has crashed. Which --treat-missing-data value should you use, and when does the alarm go to ALARM if the service stops publishing?
Show answer
Use breaching, because missing data signals the failure. Once at least 3 of the last 5 one-minute periods are missing, they count as breaching, so the alarm goes to ALARM after about 3 minutes of silence.