Sub topic 7
Alarm actions and SNS notifications
This chapter assumes you can create a metric alarm with the AWS CLI and know what a period and a statistic are. Here you will connect an alarm to an Amazon SNS topic so that a state change reaches a person or a system.
How an alarm triggers an action
An alarm is always in one of three states: OK, ALARM, or INSUFFICIENT_DATA. An alarm action runs when the alarm moves into a state. You attach actions per state with three parameters:
--alarm-actionsruns when the alarm entersALARM.--ok-actionsruns when the alarm returns toOK.--insufficient-data-actionsruns when there is not enough data to decide.
Actions run on transitions only. An alarm that stays in ALARM for hours sends one notification, not one per period.
Creating the SNS topic and subscription
SNS (Simple Notification Service) is a publish and subscribe service. An alarm publishes a message to a topic, and SNS delivers it to every subscriber. Create a topic and subscribe an email address:
aws sns create-topic --name ops-alerts
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:111122223333:ops-alerts \
--protocol email \
--notification-endpoint [email protected]An email subscription stays in PendingConfirmation until the recipient clicks the link in the confirmation message. No alarm messages arrive before that.
Attaching the topic to an alarm
The alarm from the earlier chapter already used --alarm-actions with this topic. To also send a message on recovery, add --ok-actions:
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-alerts \
--ok-actions arn:aws:sns:us-east-1:111122223333:ops-alertsput-metric-alarm replaces the whole alarm definition. Repeat every setting you want to keep, not only the one you are changing.
Testing the action
You can force a state change without waiting for real load:
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. The test still triggers the actions, so warn the on-call person first.
Common errors
- No message arrives. Check that the subscription is confirmed and that the topic ARN is in the same region as the alarm.
- Access denied when publishing. The topic's access policy must allow
cloudwatch.amazonaws.comto callsns:Publish. Topics created in the console usually allow this. Check the policy on topics created by other means. - Encrypted topic fails silently. If the topic uses a KMS key, the key policy must allow CloudWatch to use it.
- Too many emails. Alarms that flap between states send a message each time. Use
--datapoints-to-alarmand longer periods, or a composite alarm, covered in a later chapter.
Alarm actions are not limited to SNS. An alarm can also stop, terminate, or reboot an EC2 instance, or trigger an Auto Scaling policy.
Exercise
An alarm stays in the ALARM state for three hours. You configured only --alarm-actions with an SNS topic. How many notifications do you receive for this period, and which option would you add to be told when the problem ends?
Show answer
One notification, sent when the alarm entered ALARM, because actions run on state transitions and not on every evaluation. Add --ok-actions with the same or another SNS topic to be notified when the alarm returns to OK.
Exercise
You created an email subscription and tested with set-alarm-state, but no email arrived. Name two likely causes.
Show answer
The subscription is still in PendingConfirmation because the link in the confirmation email was not clicked, or the topic is in a different region than the alarm. A missing sns:Publish permission for cloudwatch.amazonaws.com on the topic policy is another cause.