What is SNS?

SNS (Simple Notification Service) is a fully managed pub / sub messaging service for microservices and serverless applications which coordinates and manages the delivery or sending of messages to the endpoints or subscribers or the client.

What is SNS Topic?

Topic is a medium which acts as a communication channel which consists of collection of subscribers subscribed to it.

Subscribers can be a HTTP Endpoints , Email , SMS , SQS , Lambda.

This article walks you through the process of creating a SNS Topics and to add a subscriber to it.

Creating SNS Topic:

To create a SNS topic , Login to SNS Console

You should see the following page.

SNS Topic

Click Start with an overview,

You will see the Dashboard, In the left pane click Topics ,

Click Create topic

SNS Topic

Give a name for the topic ,

SNS Topic

You can find multiple options which are optional, Such as

ENCRYPTION:

By default , SNS delivers the messages in the encrypted way, If you want to enable server side encryption which means encrypting the topic at rest.

SNS Topic

ACCESS POLICY

Access policy restricts the access to the policy , It defines who can access the topic.

By default , The owner (account_id) of the topic can subscribe and publish to the topic.

SNS Topic
SNS Topic

You can explicitly define who can publish or subscribe to the topic, Also you can write a custom policy by Clicking the Advanced tab.

DELIVERY RETRY POLICY

This policy defines how many times the SNS should retry publishing to HTTP & HTTPS endpoints.

You can use default retry delivery policy which is 3 retries Or else you can customize it as per your requirement.

SNS Topic

DELIVERY STATUS LOGGING

If you want to debug SNS in case of delivery failure or any delivery status. You can enable logging to Cloudwatch logs.

Also You can enable logging for Lambda , SQS , HTTP/S endpoints and application endpoints.

This requires an IAM role , Choose Create new service role.

SNS Topic

TAGS

Finally , You can add a tag to the SNS topic to segregate the topics based on the environments.

SNS Topic

Click Create topic.

Now that we have successfully created SNS topic.You can find the topic ARN using which you can communicate with other services.

SNS Topic

Lets go ahead and create a subscriber for this topic.

Creating Subscription:

To create a subscription , Click create subscription,

SNS Topic

You will be asked to choose the protocol such as lambda , SMS , email , SQS , http/s endpoints.

For this tutorial , I will choose Email.

Provide the email id and click create subscription.

But whenever we add a subscriber to the SNS topic we should confirm it.

Else It will be pending for confirmation and subscriber wont get the messages published by the SNS topic.

Go to your email , You should have got a mail from Amazon SNS like this,

SNS Topic

Click Confirm subscription , And your subscriber will be confirmed.

SNS Topic

Now go back to SNS console and refresh the page , You should see the subscription is confirmed.

SNS Topic

Now that we have a SNS topic and a confirmed subscriber.

Now Lets test the pub/sub messaging system , By publishing a mesage to the SNS topic and the subscriber (email) should receive the message.

Publishing Message to Subscriber:

To do this , Go to the Topic and click Publish Message ,

SNS Topic

In the publish message page , You can provide a Subject for your Message ,

SNS Topic

Under Message body , Enter the message you want to send to the subscribers.

SNS Topic

Then click Publish Message.

As I have checked my Mail box , I have received a message from SNS topic.

SNS Topic

Hope you like it , Thanks for reading this article.

Please do check out my other articles.