How to create Cloudwatch alarms for an SNS Topic on AWS
We can use Cloudwatch to view and analyze metrics for SNS Topics we have in our account. Cloudwatch can give us better insight into the performance of our SNS topics. Cloudwatch provides NumberOfMessagesPublished, NumberOfNotificationsDelivered, NumberOfNotificationsFailed and many more metrics for SNS Topics. In this article, we will see steps to create an alarm for the "NumberOfNotificationsDelivered" metric for the existing SNS Topic. The "NumberOfNotificationsDelivered" gives insight into a number of messages delivered successfully from the SNS topics to subscribing endpoints.
Before proceeding with this article, it is assumed that you already have an SNS Topic in the account. To create an alarm you must have a topic in the account. Also, if you want to get notified when the alarm is triggered, you must have an SNS Topic with a confirmed subscription to it.
To know more about metrics provided by Cloudwatch for SNS visit the official documentation of AWS here.
- AWS Account(Create if you don’t have one).
- SNS Topic (Click here to learn to create an SNS Topic using Cloudformation)
What will we do?
- Login to AWS.
- Create an alarm for the SNS Topic.
- Delete the alarm.
Login to AWS
Click here to go to AWS login Page.
Enter your credentials to login into your account on the page you get when you click on the above link.
Once you successfully login into your AWS account you will see the main AWS management console as follows.
Create an Alarm for SNS Topic
To create an alarm for the existing SNS Topic, search for "Cloudwatch" in the search box at the top of the screen.
You will see the main dashboard for Cloudwatch as follows. Click on the "Alarms" button in the left panel.
Since I do not have any alarm created in my account under the selected region, we do not see anything on this screen. To create an alarm click on the "Create alarm" button.
Click on the "Select metric" button to select the required metric for the SNS topic.
Search for "SNS" to filter metrics related to the SNS topics. Click on "Topic metrics" to proceed.
On this page, you will see available metrics for the SNS topics you have in your account under the selected region. Here, I have selected a "NumberOfNotificationsDelivered" metric for the "test" Topic. This metric checks for the number of notifications that have been delivered by the topic. Click on the "Select metric" button to proceed.
Here, you can specify a threshold type and a value for it. I have selected the threshold type as "static', specified its value as "1" and the condition as "Greater/Equal". This means if the number of messages that have been delivered by the topic is greater than or equal to 1 then the alarm will be triggered.
Click on the "Next" button to proceed.
If you want to send a notification then you can specify an SNS topic on which the notification will be sent if the condition becomes true. So in this case, if the number of messages that have been delivered by the SNS topic is greater or equal to 1 then a notification will be sent to the "test" SNS topic.
On this screen, give a name to the alarm you are creating and optionally you can add a description to the alarm. Click on the "Next" button to proceed further.
This is the last step where you can review your configurations before you create an alarm. Review all your configuration and then click on the "Create alarm" button.
You can see your alarm if it gets successfully created. The alarm will take a few minutes to update its state, till then the state of the alarm will be "Insufficient data".
Depending upon the condition, the state of the alarm will get updated. In this case, the condition matches and hence the state of the alarm goes to "In alarm".
As the alarm moves to the "In alarm" state it sends a notification to the Endpoint subscribed to the SNS specified in the configuration.
You can also edit the alarm to update the threshold value. To edit the alarm, select the alarm and click on the "Actions -- > Edit" button.
Here, I have changed the threshold value to "10" from "1" and updated the alarm.
This time the condition does not match and the alarm transits to the "OK" state. It means the number of notifications delivered by the SNS topic is not greater than or equal to the value "10" we specified in the alarm.
Delete the Alarm
If you were just learning to create alarms and you no longer need the alarm you created in your account you can easily delete it.
To delete the alarm select it and click on the "Actions -- > Delete" button.
Click on the "Delete" button on the pop-up screen you get to confirm the deletion. Once you delete the alarm you cannot restore it so be careful with the delete operation.
In this article, we saw the steps to create an alarm for the existing SNS topic in your account. We also saw how easily an alarm can be deleted in just a few clicks. One needs to be very careful while performing the delete operation since the delete operation deletes alarms permanently.