Getting Started With AWS SNS

Introduction

Amazon SNS (Simple Notification Service) is a fully managed pub/sub messaging service which enables you to write distributed applications. Using SNS, you can manage and send notifications to all the subscribed system using endpoints like SQS and webhooks. It can also send messages to Lambda function further processing. SNS can also be used to maintain large number of human subscribers too. People can get notifications using SMS and emails. In this part we will see how can we publish message using SNS.

Flow of SNS

Before we begin let us first understand what is Publisher/Subscriber model.

Publish/Subscriber Model

There are two components in a system:

  • Publisher: A service that can broadcast out messages to its subscribers.
  • Subscriber: Any service that wish to receive the messages broadcasted by the publisher.

If a service wish to subscribe to a publisher, it needs to notify the publisher that it wants to receive its broadcasts along with where it wishes to receive i.e. the endpoint. It can be Http endpoint, SQS or lambda function. In the above diagram the publisher is sending message to a SNS topic via the service and all the subscribers will receive the message but the mode or endpoint using which they have subscribed if different.

Pre-requisites

You will need a valid AWS account and credentials to access the SNS. You will also need to have access to the AWS console to create a SNS topic and some subscribers to it.

Setting up a SNS Topic

To setup a SNS topic you first login to AWS and navigate to SNS. Follow the instruction to create a SNS and a topic. Once created, you will need the ARN property of the SNS to use in the code. Make sure, the credentials you are using have access to publish message from the SNS. Add some subscribers and confirm them to see the full action.

Publishing a message

Let's assume following is the message.

{
  "foo": "bar"
}

Now that we have a message structure, we need to publish it out to the desired SNS. We have to import the AWS SDK for node.js and use it to publish a message . The SDK is capable of using the credentials stored in your env. It looks for the following environment variable:-

export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
3export AWS_REGION=the_region_you_are_using

Following is the code to publish the message:-

// Getting Started with AWS SNS using node js. This part shows how to publish content to SNS
// Load the AWS SDK for Node.js
const AWS = require("aws-sdk");
const sns = new AWS.SNS({apiVersion: "2010-03-31"});
const params = {
  "Message": JSON.stringify({"foo": "bar"}),
  "TopicArn": "ARN FOR TOPIC YOU WANT TO PUBLISH TO"
};
// By using Callback
sns.publish(params, (err, data) => {
  if (err) {
    console.log("There was an Error: ", err);
  } else {
    console.log("Successfully published.", data);
  }
});

The above is implemented using the callback. if you wish to achieve the implementation using promise, following is the implementation.

// Promise implementation
sns.publish(params).promise()
.then(data => console.log("Successfully published.", data))
.catch(err => console.log("There was an Error: ", err));

You can also find the code sample in my github repo

Conclusion

AWS Simple Notification Service (SNS) is a super scalable service that allows us to implement the publish/subscribe model with ease. We can use this to send texts, emails, push notifications, or other automated messages to multiple channels at the same time. There are many other uses cases and some advanced filtering logic, message templating and mixed message available in SNS. Give it a try and Happy Coding.

I would love to hear what you think about this article and if there anything you would like me to cover in future. Drop me a line on @awsmag.