This blog discusses how to send Cloudwatch alarm notifications to Slack.
The architecture diagram of a notification system
Here is the architecture. The flow of the system is the following:
CloudWatch gains the metrics of EC2
Only if there is a specific alarm, then Cloudwatch will send a notification to SNS
SNS sends out the alarm, which is obtained from the CloudWatch, to a Chatbot
(This architecture is easier to construct because there is no need to add AWS Lambda to this diagram)
The procedure of the configuration
0-1 Log in to the AWS console
After logging in to the AWS console, choose SNS service, select the Topic section on the sidebar, and click on to Create Topic.
Next, we will fill out these information below. This time, we chose the Type section. In the name section and the display section, you can write whatever name, you think, is appropriate. Afterwards, click on Create Topic.
As shown in the picture below, check out whether SNS-Topic is created successfully.
In order to realize whether it is successful, look at the green box in the picture.
Sucess: Topic is successfully created
Firstly, you need to move to AWS Chatbot. Afterwards, you need to find configured clients and then click on Configure new clients.
Next, when you see a client type setting in Confgure new client, choose Slack this time.
AWS chatbot seeks permission to the slack workspace just as this picture shows so you are required to click on the permission button. Otherwise, you are not allowed to connect AWS chatbot.
2-4 Click on the "Configure new channel"
If AWS is successfully allowed to permit your workspace, then the green display says that slack successfully authorized the AWS chatbot.
2-5 Write in the configuration name
In this case, we will make a test notification on Slack, and we named it as "my-alarm-test". However, you can choose whatever name you want.
2-6 The selection of the slack channel
In the slack channel section, you must choose the Channel type, which is public or private.
the Public setting: Once you join the channel, you can join and view the workspace.
the private setting: You can join and view the channel, but it's possible only when there is an invitation.
2-7 The role setting
2-8 The setting of the channel IAM role
2-9 The setting of the policy template
2-10 the setting of channel Guardrail policy
Channel Guardpolicy is a policy that is prior to channel IAM role and user role.
As shown in this picture, CloudwatchReadOnlyAccess is selected. the convenience of this policy is that it becomes possible to look at a graph.
2-11 Notification Setting
Here, you need to select SNS Topic. It is necessary to choose a topic which is specified in AWS SNS.
Be Careful. This procedure presupposes that EC2 has already been created before.
If you haven't launched an EC2, you should do it at first, and then proceed with the following setting in CloudWatch.
At first, in the AWS console, search CloudWatch, click on Alarm, and then click on
3-2 Choose matrices
In the metrics section, choose whatever metrics you need, then click on the Choose metrics.
3-3 The selection of alarm condition
In our case, we chose CPUUtilization as a metric, and nextly we are required to set up when an alarm should occur. In the first condition, choose the threshold type. There are two types of thresholds, static and dynamic.
3-4 Notification Setting
There are two alarm sections in our case because we want to send notifications when a value is out of the threshold we set up, and when a value is within the threshold. Nextly, we are required to choose an SNS topic because without setting up a receiver, we will not get notifications. In our case, we already set up the SNS topic in Amazon SNS, so that My-test-yopic" will be the location to send notifications.
3-5 The specification of the Alarm name and Description
This time, we chose CPUUtilization as an alarm name because we chose this metric in selecting the preferred metric section (3-2). In order to fill out this section, check out the metric you chose before, and if necessary, write a description in the alarm description (as an option).
3-6 Click on "Create the alarm"
To finish up the setting, click on "Create the alarm".
4 Notification test
After creating your alarm (In our case, testCPUUtilization), check out if the state is in alarm.
4-2 Alarm notification
After confirming that the state is in alarm, make sure that you get the alarm notification in Slack.
If you change a threshold in order not to ring the alarm, then you will receive a recovery notification like the one below.
In this article, we discussed how to send CloudWatch notifications onto SNS using AWS Chatbot. Furthermore, AWS Chatbot makes slack notifications easier to use because its function facilitates checking out the alarm graphically. Especially, the use of AWS Chatbot adds a URL of the graph and a picture of the graph.
Slack is easier to deploy, but if you want to use other tools such as Microsoft Teams, it is necessary to prepare Webhook or coding in python on Lambda for sending notifications on them.