How to use AWS SNS Tutorial

AWS SNS Tutorial (Simple Notification System)

Share This Post

Share on linkedin
Share on facebook
Share on twitter
Share on email

Modern apps tend to use microservices. This means that a single app has multiple standalone components, normally in containers. However, all the parts need to talk with each other. Amazon AWS has a solution for that: SNS. In this AWS SNS Tutorial, we will see what SNS is and how to use it.

AWS SNS Tutorial

What is SNS?

SNS stands for Simple Notification System and, as the name says, it is a system to send notifications. However, we are not talking about the kind of notifications you get on your phone. Instead, we are talking about notification a system sends to another system.

Even if this looks a little bit confusing at first, it is actually quite simple. A modern application that has multiple standalone components needs to have them communicate with one another. SNS is a channel to do so: one module puts a notification there, and the other reads it.

But we are not simply talking about shared storage. Instead, SNS is transient and one-way. Imagine the push notifications you get on your phone: you get it, read it, and it disappears. SNS works like that: one module creates the notification, another reads it and the notification is canceled from SNS.

Furthermore, SNS is not real-time. You can write the notification, but you have no idea when the other module is going to consume it (even if it is in general quite fast). This also means you don’t get to have any feedback at all.

At the heart of SNS: the topic

The first thing we need to cover in this AWS SNS Tutorial is the concept of “topic”. A topic is something like a channel, where you have one (or more) modules writing on it and one (or more) modules reading from it.

Applications reading from the module are subscribed to the topic. Instead, applications writing on it are publishing on it.

To create a topic, log in into your AWS Console. This assumes you are already registered to AWS, if not you may have to take a look here.

In the list of services, search for SNS and get to the AWS SNS page. In there, you cannot do anything unless you have a topic. Thus, if you don’t have one, it will ask you to create the first one.

When creating a topic, it will ask you for some settings. For a basic AWS SNS Tutorial, we can leave everything to default and only provide name and display name. All other settings may need to be modified only in a more specific implementation.

AWS SNS Tutorial and an explanation on how to create a SNS topic from the web interface
The creation form.

Using the topic

The next part of our AWS SNS Tutorial is to use a topic. Once you create a topic, you will end up on its dashboard page. From there, you can see what are its subscribers and even publish a notification in the topic.

AWS SNS Dashboard with the list of subscriptions and details about this topic
The dashboard of a topic.

We can publish a message, but nobody will listen to it because there is no subscription yet. The kind of subscription you may want to do depends on your application requirements, and we cannot cover them all.

Some simple alternatives are HTTP or HTTPS calls, as well as AWS Lambda. In case of an AWS Lambda function, assuming you use python, you can fetch the data inside your lambda_handler() function with the following string.

text = event['Records'][0]['Sns']['Message']

Once you have a subscription in place, you can start to publish messages. A message is a JSON text with several entries, each for a specific type of subscription. If you are just using one type of subscription you can just send raw text.

To publish a notification, click on Publish message in the dashboard and configure the message as you want.

AWS SNS Tutorial on how to publish a message to a SNS topic using diversified messages
Message body during publication.

The only required parameter to fill is the message body. Once your message is ready, you can publish it by using the button at the end of the page. Soon, all subscribers will consume the message. In fact, SNS is not a queue where only a worker processes a message. It is a notification system where everyone who is subscribed gets it.

Publishing messages to SNS with Python

Publishing messages to SNS in the web interface is a quick way to test, but it is not the best way to use SNS. In fact, SNS can handle millions of messages, and that’s not something you publish manually.

Instead, as we covered previously, you want one part of your application to publish and another part of your application to subscribe. We already saw how to consume SNS with lambda, now we are going to see how to publish.

In short, you can publish to SNS by calling the public API of AWS. However, there are several wrapping of this API in many languages, and the most popular of those is Python.

Publishing a message with boto3 is dead simple, and just as follow.

import boto3

sns= boto3.client('sns')

response = sns.publish(
            TopicArn='arn of the topic',
            Message='some text to publish
        )

First, you create an SNS client, and then you use the publish() method on it. This wants as a parameter at least the ARN (the unique identifier of the topic) and the message to publish.

You can learn more about the SNS boto3 API in the official documentation.

Wrapping it up

In this AWS SNS Tutorial, we have seen how to create a new topic in SNS, publish messages to it, and consume its content. With SNS in your toolbelt, you are ready to deploy modern, scalable, and fast applications with a microservices infrastructure.

Just drop a comment below in case you want to learn something more about SNS or if you have some questions.

Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.
Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.

Join the Newsletter to Get Ahead

Revolutionary tips to get ahead with technology directly in your Inbox.

Alessandro Maggio

2020-02-20T16:30:04+00:00

Unspecified

Cloud, AWS

Unspecified

Want Visibility from Tech Professionals?

If you feel like sharing your knowledge, we are open to guest posting - and it's free. Find out more now.