AWS Simple Notification Service

AWS SNS is a very big topic. So In this blog we’re going to talk about the basics, it’s usage, workflow and little bit about the set up and the configuration of the SNS.

What is AWS SNS?

Simple Notification Service is a Message publishing and processing service (PubSub) and you often hear the term PubSub. So, PubSub is just a concept where you have a publisher and a consumer; those are the two main actor in a PubSub model.

A publisher can be a variety of different themes, the theme that you pick obliviously depends on how your application work. 

 Here’s AWS definition of SNS:
“Amazon Simple Notification Service (Amazon SNS) is a managed service that provides message delivery from publishers to subscribers (also known as producers and consumers). Publishers communicate asynchronously with subscribers by sending messages to a topic, which is a logical access point and communication channel. Clients can subscribe to the SNS topic and receive published messages using a supported endpoint type, such as Amazon Kinesis Data Firehose, Amazon SQS, AWS Lambda, HTTP, email, mobile push notifications, and mobile text messages (SMS).”

So, In a nutshell simple notification service is a notification service that sends email and text messages based on events that happen within your aws account.

Let’s talk about SNS Basics components:

There are three things that we want to concentrate on here first we have Topics in which how we label and group different endpoints that you send messages to. The next our Subscriptions, these are the actual end points that a topic sends the messages to (i.e the email address or phone number of our system admin) and then Publishers, The human/alarm/event that gives SNS the message that needs to be sent.

PubSub model

Using the SNS

For this blog, we will start with creating a topic then we will create a subscription which will be simple email protocol based and finally we will look into the how publish message to subscriptions.

Let’s start with creating a topic for publishing messages

Creating a topic

To get started, you’ll definitely need an aws account and you need to setup the iam user. Then from the AWS Management Console, please do the following:

  • Navigate: Console Home –> SNS –> Create topic -> Enter topic name -> click next step.
  • Select the standard type for this exercise. (because standard topics are better suited for use cases that require higher message publish and delivery throughput rates and fifo topics are a better fit for use cases that require message ordering and deduplication)
  • Then you can enter display name for the SMS subscription that can be displayed on the subscribers end (i.e Mail-trap Team)
  • Take the defaults for everything else. (feel free to play around and explore)
  • Click create topic.
  • Then you will see confirmation that the topic is created.
topic created successfully

Adding subscribers to a topic

When a topic is created you will be redirected to its details (see above image) along with the dashboard displaying the list of subscribers. Now we need to add subscribers here, specifying their protocols and endpoints.

  • Now we down on the same page and click on the create subscriptions button on the right. It will take us to the next page where we will fill out some details.
  • Topic name can be selected here then we will select the protocol (The type of endpoint to subscribe) -> select email option for this excercise.
  • Moving on to the main part: Endpoint (where a subscriber will receive the messages or notifications) -> enter the email that you want to subscribe to the topic selected above.
  • Take the defaults for everything else.
  • Click create subscriptions.
  • When a subscription is created you will be redirected to its details. (see the below image)
  • Now we can see the subscription status is pending because that email subscriber need to confirm by clicking on the link that we sent them to subscribe to the topic.
subscription created; status pending
  • Then we have to check the email to confirm the status of this subscription and confirm subscription -> you will be navigated to the aws subscription confirm tab where you can see the subscription’s id and you can unsubscribe, if you want to.
  • After doing that, we can see status in subscription details it’s Confirmed!
  • Check your spam folder, if you can’t able to find confirmation email.

Sending a message to a topic

  • Go back to the topic page (you can find it, on the left side menu) -> select the topic -> publish message.
  • Now we need to put message details here:
  • Subject -> leave out the TTL (because it’s applies only to mobile application endpoints) -> enter the message body -> leave the attributes blank -> publish message.
  • Now you will see the confirmation message published to specific topic with message id and request id.
  • Check your inbox, you will the email.
publishing a message to a subscriber

Of course, You do not need to use the console to send each message. If you have many messages to send, you can use either AWS SDK for Java or for .NET.

Publishing a message with AWS SDK for Java

Specify your AWS credentials and type in your code. If you need help using Amazon SDK for Java 2.x, consult their guide. Here’s a sample of code that publishes a message and prints its ID.

Note that TopicArn refers to Amazon Resource Name, the topic that the endpoint is subscribed to.

// Publish a message to an Amazon SNS topic.
final String msg = "If you receive this message, publishing a message to an Amazon SNS topic works.";
final PublishRequest publishRequest = new PublishRequest(topicArn, msg);
final PublishResult publishResponse = snsClient.publish(publishRequest);

// Print the MessageId of the message.
System.out.println("MessageId: " + publishResponse.getMessageId());

Compile and run the code, and you’re done.

Publishing a message with AWS SDK for .NET

Begin by entering your credentials and the code for .NET. For details on using Amazon SDK with .NET, look here

Here’s another sample:

// Publish a message to an Amazon SNS topic.
String msg = "If you receive this message, publishing a message to an Amazon SNS topic works.";
PublishRequest publishRequest = new PublishRequest(topicArn, msg);
PublishResponse publishResponse = snsClient.Publish(publishRequest);

// Print the MessageId of the published message.
Console.WriteLine("MessageId: " + publishResponse.MessageId);

Common Amazon SNS scenarios

  • Application Integration – when a message is published to a SNS topic is replicated and pushed multiple endpoints.
  • Application alerts – where we can set up system & application alerts to trigger specific predefined thresholds and Amazon SNS can send these notifications to specific user via email, sms.
  • Mobile push notifications – mobile push notifications enables you directly send the notifications to the user. So we can use Amazon SNS to send update notifications to the app.
  • User notifications – Amazon SNS can send push email messages or sms to groups or individuals.

Pricing for Amazon SNS

Like every other service, Amazon SNS is fairly inexpensive and comes with generous free tier. No subscription is necessary. You simply pay for what you use at the type of endpoint you choose. You can check more about it here.

That’s it for now. If you enjoyed this article, share it with your friends and colleagues!

More About SNS

We will learn more about the SNS later. If you “just gotta know more” now, here is an excellent link.
And here’s good YouTube playlist to start.

Thank you!