Skill 100 · Setting Up CloudWatch Alarm Notifications
Subchapter 100.1
references/setup-cloudwatch-alarm-notifications.mdMarkdown10 KBView on GitHub
This SOP guides you through setting up notification channels for CloudWatch alarms using Amazon SNS (Simple Notification Service). It will create SNS topics, configure subscriptions for various notification methods (email, SMS, webhooks), and link them to existing or new CloudWatch alarms.
alarm_name (required): The name of the CloudWatch alarm to configure notifications for notification_type (required): Type of notification (email, sms, webhook, lambda, sqs) notification_endpoint (required): The endpoint for notifications (email address, phone number, webhook URL, etc.) sns_topic_name (optional): Custom name for the SNS topic (default: generated from alarm name) aws_region (optional, default: “us-east-1”): AWS region where resources will be created
Check for required tools and warn the user if any are missing.
Constraints:
Verify that the specified CloudWatch alarm exists and gather its current configuration.
Constraints:
aws cloudwatch describe-alarms --alarm-names {alarm_name} --region {aws_region}Create an SNS topic that will be used to send notifications when the alarm is triggered.
Constraints:
aws sns create-topic --name {sns_topic_name} --region {aws_region}Configure encryption at rest for the SNS topic to protect sensitive notification data.
Constraints:
aws sns set-topic-attributes --topic-arn {topic_arn} --attribute-name KmsMasterKeyId --attribute-value alias/aws/sns --region {aws_region}Set up appropriate permissions for the SNS topic to allow CloudWatch to publish messages.
Constraints:
aws sns set-topic-attributes --topic-arn {topic_arn} --attribute-name Policy --attribute-value {policy_json} --region {aws_region}Create a subscription to the SNS topic based on the specified notification type and endpoint.
Constraints:
aws sns subscribe --topic-arn {topic_arn} --protocol {protocol} --notification-endpoint {notification_endpoint} --region {aws_region}Configure the CloudWatch alarm to send notifications to the SNS topic when triggered.
Constraints:
aws cloudwatch put-metric-alarm with the existing alarm configuration plus the new AlarmActionsVerify that the notification system is working by testing the alarm state change.
Constraints:
aws cloudwatch set-alarm-state --alarm-name {alarm_name} --state-value ALARM --state-reason "Testing notification setup" --region {aws_region}aws cloudwatch set-alarm-state --alarm-name {alarm_name} --state-value OK --state-reason "Test complete" --region {aws_region}At the end of the setup, provide examples of testing and integrating the notification system.
Constraints:
# List all alarms
aws cloudwatch describe-alarms --region us-east-1
# Create SNS topic
aws sns create-topic --name MyAlarmNotifications --region us-east-1
# Subscribe email to topic
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:MyAlarmNotifications --protocol email --notification-endpoint user@example.com --region us-east-1
# Test alarm state
aws cloudwatch set-alarm-state --alarm-name MyAlarm --state-value ALARM --state-reason "Manual test" --region us-east-1
# List SNS subscriptions
aws sns list-subscriptions-by-topic --topic-arn arn:aws:sns:us-east-1:123456789012:MyAlarmNotifications --region us-east-1import boto3
def create_alarm_with_notifications(alarm_name, metric_name, threshold, sns_topic_arn):
cloudwatch = boto3.client('cloudwatch')
cloudwatch.put_metric_alarm(
AlarmName=alarm_name,
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName=metric_name,
Namespace='AWS/EC2',
Period=300,
Statistic='Average',
Threshold=threshold,
ActionsEnabled=True,
AlarmActions=[sns_topic_arn],
AlarmDescription='Alarm with SNS notification',
Unit='Percent'
)const AWS = require('aws-sdk');
const cloudwatch = new AWS.CloudWatch({region: 'us-east-1'});
async function triggerTestNotification(alarmName) {
const params = {
AlarmName: alarmName,
StateValue: 'ALARM',
StateReason: 'Testing notification from JavaScript'
};
try {
await cloudwatch.setAlarmState(params).promise();
console.log('Test notification triggered');
} catch (error) {
console.error('Error triggering notification:', error);
}
}import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.SetAlarmStateRequest;
public class AlarmNotificationTest {
public static void testNotification(String alarmName) {
CloudWatchClient cloudWatch = CloudWatchClient.create();
SetAlarmStateRequest request = SetAlarmStateRequest.builder()
.alarmName(alarmName)
.stateValue("ALARM")
.stateReason("Testing notification from Java")
.build();
cloudWatch.setAlarmState(request);
}
}Email notifications not received
If email notifications are not working, check that the email subscription was confirmed. Use aws sns list-subscriptions-by-topic to verify the subscription status is “Confirmed” rather than “PendingConfirmation”.
SMS notifications failing Ensure the phone number is in E.164 format (e.g., +12345678901) and that SMS is supported in your AWS region. Some regions have restrictions on SMS delivery.
Alarm not triggering notifications
Verify that the alarm has the correct SNS topic ARN in its AlarmActions. Use aws cloudwatch describe-alarms to check the alarm configuration and ensure ActionsEnabled is set to true.