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.
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.
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 lambda_handler()
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.
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 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.