How to publish a message to SNS topic using AWS CloudWatch alarm?

Publish a Message to SNS Topic

AWS SNS provides lots of option to notify the subscribers through various protocols with the message received from Publisher. Different AWS services could be used to send notification to the SNS topic. In this article we would talk over below topic

Topic

  1. Launch an Ec2 Instance
  2. Configure CloudWatch Metrics
  3. Send a Message to SNS topic using CloudWatch

Launch an EC2 Instance

We would need a RHEL EC2 instance where we would enable the CloudWatch to it. The test case is to send a notification to SNS topic when the CPU utilization would exceeds its threshold value.

We are not going to follow step by step to launch an EC2 here. You could refer Launch an Amazon EC2 in AWS to launch an Instance in AWS.

We already have an instance running in 2/2 checks. So we would use the same instance to enable the CloudWatch metrics in next topic.

Configure CloudWatch Metrics

By default, the CloudWatch alarms are not configured for your instance. You could enable this service during your EC2 Launch in Configure Instance details or you could configure it manually by below process

1. Sign into AWS Console (link) and open EC2 Service

2. Select your EC2 Instance and go to Monitoring

No Alarms configured as of yet.

3. Click on Create Alarm

In Create Alarm, provide all the details

Send a notification to – You need to provide your topic name here. The metrics would send notification to your topic. If you don’t have any topic created then refer How to Create a SNS Topic

Take Action – You could use this to create alarm to automatically stop, terminate, reboot or recover the instance. You may need to create separate role for each actions.

The Section Whenever, Is, For at least, Name of alarm is part of your alarm threshold configuration and tag name.

Here, the CloudWatch would send a notification to the SNS topic when the EC2 instance Average CPU utilization exceeds its threshold value (85% in our case)

Once you clicked on Create Alarm, the alarm would be created successfully

4. Verify the Alarm

Go to your EC2 console and you would be able to see the Alarm Status OK with alarm details

Send a Message to SNS topic using CloudWatch

As per alarm configuration, we need to increase the CPU Utilization above 85% to receive a notification to your SNS Topic using following steps

1. Connect to your EC2 using SSH

2. Verify the CPU using top command

The CPU looks very low due to no application is currently running on the server at this moment.

3. Increase the CPU Utilization

Create a bash script with below commands in your root terminal & execute in multiple windows. This is to create CPU spike in the Instance

[root@twwip ~]# vi spikecpu.sh
Insert command dd if=/dev/zero of=/dev/null & Save it
[root@twwip ~]# chmod 755  spikecpu.sh
[root@twwip ~]# sh spikecpu.sh 

4. verify the CloudWatch CPU Alarm

Open the CLoudWatch CPU alarm of your EC2 in a new tab and verify it

Once the CPU Utilization exceeds 85% threshold value, the alarm status would change from OK to ALARM.

You could also click on View in Metrics to refresh your alarm manually.

As you could see the CPU alarm status has been changed from OK to ALARM because of above threshold value. Now we should receive a notification to your SNS topic

5.Verify the SNS Notification

We received the notification as per the configuration successfully.

Please comment below for any questions related to this post.

Leave a Reply

Discover more from TWWIP

Subscribe now to keep reading and get access to the full archive.

Continue reading