Wednesday 8 February 2023

Combine AWS CloudWatch Alert with AWS Chatbot

AWS Chatbot is an interesting service that can integrate with both Slack and Chime. 

Usually, when something happens in our AWS account, we invoke some alert mechanisms. The most common is the CloudWatch alarm that can invoke the SNS topic. The SNS topic is bound to the email or Lambda, and using both of them allows you to invoke any possible way you can think of to notify the end-user.

While sending an email is simple, invoking the Lambda to activate other notification options can be more difficult.

We can get the notification directly into Slack and Chime in a declarative manner by using AWS Chatbot. 

Our architecture will be as following:



We will stress the EC2 instance to have high CPU usage. We will create an alert to monitor the CPU, and if alert happens, it will invoke the SNS topic that will be integrated with AWS Chatbot. We will set the relevant settings in Slack, Chime, and the chatbot to send the alert message.

To create the stress, we created an Amazon Linux EC2 instance and installed on this instance the "stress" utility.

sudo yum install -y epel-release

sudo amazon-linux-extras install epel

sudo yum install stress

Next we will create the SNS topic




Note!! The subscription part will appear later after you choose this topic in the chatbot console.

Now we will define the CloudWatch alarm.


I simplified the use-case and the alarm will be called after CPU consumes more then 10% of the CPU.

Next step is to configure Slack, Chime and the chatbot.


In the Slack, make sure that you have "AWS" apps and if you don't, use "Add apps" option 


Configure the Slack client by using the instruction from official AWS documentation.
It is very important to run "/invite @AWS" command in the Slack channel.

After completing the setup you should see the following information:


If we enter the "AwsTest" configuration we see that it is bound to SNS topic we created earlier

Set Chime integration according to this AWS documentation.

You should see the following settings after completing the integration:



And also this this case we see that Chime channel is bound to the same SNS topic


Now, let's stress the EC2 instance.

sudo stress --cpu  8 --timeout 120


And after about a minute, I got the notification in both Slack and Chime.







Zero coding, simple implementation. Another cool service from AWS.