Setting notifications with AWS CloudWatch and AWS SNS

In this article we’ll discover the steps to setup the AWS Simple Notification Service (SNS) to send notification whenever a AWS CloudWatch Event is triggered. We’ll go through a complete working example, setting up the alarm and SNS resources, and demonstrating the notifications coming through in real time. However, before going into the details of the subject, let us have a brief overview of what is AWS CloudWatch and AWS SNS first.

What is AWS CloudWatch?

AWS CloudWatch is an AWS monitoring service that allows you to ingest logs and metrics from your applications or servers. You can correlate your metrics and logs to better understand the health and performance of your resources.

Benefits of CloudWatch:

  1. Observability on a single platform across applications and infrastructure.
  2. Easiest way to collect metrics in AWS and on-premises.
  3. Improve operational performance and resource optimization.
  4. Get operational visibility and insight.
  5. Derive actionable insights from logs.

Taking advantage of these benefits we can create events based on the metric value thresholds you specify, or that can watch for anomalous metric behavior based on machine learning algorithms. To take action quickly, we can set up automated actions to notify if an event is triggered and automatically start auto scaling. Hence, CloudWatch Events can be configured to be triggered when metrics meet certain criteria. For example, you could set up events to be notified about Instance state change, low disk space or high CPU usage. And CloudWatch can send messages directly to an SNS topic and SNS allows us to setup for the message to be sent to so that we can respond to the situation.

What is AWS SNS?

AWS SNS (Simple Notification Service) is a highly available publish/subscribe messaging service. The publish-subscribe pattern allows publishers to send messages, without knowledge of who they need to be send to. Instead, it’s up to the subscriber to register itself to receive messages from specific topics it’s interested in. An SNS topic is a communication channel to link together publishers and subscribers.

Benefits of SNS :

  1. Publishers don’t need to know about their subscribers, allowing for decoupling of services.
  2. One message can be sent to many subscribers, allowing for parallel processing
  3. It’s compatible with many AWS services to use for the message source and destination.
  4. It’s highly available.

In the diagram above, we can see that a publisher is sending messages to a specific topic, and these messages are then being delivered to two subscribers. This is how AWS SNS works.

How can we use SNS with CloudWatch ?

You can see from the above diagram that CloudWatch will now act as the publisher, and the email address as the subscribers. So, whenever the CloudWatch Event is triggered, the CloudWatch which is the publisher, will publish message to the SNS. The SNS will then according to the topic configured, will deliver the messages to the subscribers.

Now, let us customize an email notification when an AWS EC2 instance changes its states.

The AWS EC2 instance has the following states: Running, Stop, Terminate, Reboot and Hibernate. In real time an instance in the “Running” state might enter into “Stopped” state due to some reason, something which is not desirable. Hence, with CloudWatch Events set, a change state event will be triggered by the CloudWatch in the form of notification. And, in this case Email notification will be sent to the concern person to respond accordingly.

To do that, we will follow the following steps:

  1. Create an Amazon Simple Notification Service (Amazon SNS) topic.
  2. Create an Amazon CloudWatch event using the EC2 Instance State-change Notification event type.

1. Creating an SNS topic:

  1. Open the Amazon SNS Console

And then select Topics from the navigation pane.

Click on Create topic.

For Type, Select the Standard.
For Name, enter a name for your topic.
For Display name, enter a display name for your topic.

On the Subscriptions tab, click Create subscription.

You select the ARN of the topic that you have just created in Type ARN

For Protocol, Select Email.

For Endpoint, enter the email address where you want to receive the notifications.

Click on the Create subscription tab and it is created.

  1. A subscription confirmation email is sent to the address you entered. 

Click on the confirm link to confirm the subscription.

Below is the subscription confirmation page.

2. Creating a CloudWatch Event:

  1. Open the CloudWatch Console.

And then go to Events > Rules > Create rule

Click on Create Rule.
For Event Source, Click on Event Pattern.
For Service Name, Select EC2.
For Event Type, Select  EC2 Instance State-change Notification.
Tick on  Any state.
Tick on Any instance.

2. In the Targets section.
For Targets, select SNS topic.
For Topic, select the topic name that you created earlier.

For Configure input, select Input Transformer.
For Input Path, enter the following:
{“instance-id”:”$.detail.instance-id”, “state”:”$.detail.state”, “time”:”$.time”, “region”:”$.region”, “account”:”$.account”}
For Input Template, enter the following:
“At <time>, the status of your EC2 instance <instance-id> on account <account> in the AWS Region <region> has changed to <state>.”

Note: The Input Template also allows custom inputs.

3. Click Configure details.
For Name, enter a rule name.
For Description, enter a rule description.

Then click on Create rule to complete rule creation.

We can verify by changing the states of the instance in the EC2 console.

Open the EC2 Console

Select any instance and change the instance state manually. In this case we are “Stopping” an instance.

When the state of the instance is changed into “Stopping” from “Running”.

An email alert is sent to our email address given in the SNS topic.

And when the instance state changes to “Stopped” from “Stopping”.

Then again we get an Email alert confirming the same. 

Conclusion

So, this is it. This is how we create notifications using AWS CloudWatch and AWS SNS. I hope the article is of great help. Feel free to comment and contact me for anything regarding the article and other cloud technologies as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top

Subscribe

* indicates required