Amazon Simple Notification Service (Amazon SNS) is a serverless service that uses
We can create topics in Amazon SNS and then add subscribers to these topics. Any message published on a topic is sent to the topic’s subscribers immediately. A single topic can have multiple publishers and subscribers. Amazon SNS supports application-to-application (A2A) and application-to-person (A2P) subscribers. In A2A subscribers, we can send messages between different components of distributed systems, microservices, and event-driven applications.
In A2P messages Amazon SNS supports emails, mobile text messages, and push notifications as subscriber endpoints and uses a device token on our behalf to send notifications to these services.
Following are some of the subscriber endpoints Amazon SNS supports:
We can create two kinds of topics on SNS, standard and FIFO (first in, first out). Standard topics are used when the order of the messages being sent isn’t crucial. Also, message duplication is possible in standard topics. On the other hand, FIFO topics are used when we want to ensure that messages are sent in the correct order, and no duplicates are created. Currently, only
Let's look into some important features provided by Amazon SNS.
Amazon SNS supports the fanout architecture where, any message published on an SNS topic is replicated and sent to multiple subscriber endpoints, which allows parallel asynchronous processing. This means a single message can be sent to different endpoints, such as SQS queues and Lambda functions, where it will be used for further processing.
In case a topic as multiple subscribers, Amazon SNS allows us to filter the messages being forward to each subscriber. This is done by adding a message filtering policy to each subscriber to specify the type of messages the subscriber receives.
When a publisher sends a message to Amazon SNS, SNS creates multiple copies of this message and saves them across multiple availability zones and keeps them till it receives confirmation that a message has been sent to it subscribers.
Amazon SNS uses server-side encryption to protect the contents of our messages and uses the encryption keys provided by
We can create an SNS topic through the AWS console or SDK. Let's see how to create an SNS topic using Boto3, an AWS SDK for Python. We will use the create_topic
function to create a topic and name it sns-demo-topic
. We'll also add an email subscriber to this topic.
Click the "Run" button on the widget given below. Before executing the code, remember to replace the {{access_key_id}}
and {{secret_access_key}}
placeholders with your AWS access key and secret access key on lines 5–6 and replace the <add your email address here>
placeholder with a valid email address on line 14. Once the code executes, copy the topic ARN given as output, as we'll use it later to publish messages.
import boto3import json#create a boto3 clientsns = boto3.client('sns',region_name='us-east-1',aws_access_key_id="{{access_key_id}}",aws_secret_access_key="{{secret_access_key}}")#create an SNS topicresponse = sns.create_topic(Name='sns-demo-topic')Topic_ARN = response['TopicArn']print("SNS topic ARN:", Topic_ARN)email_address = '<add your email address here>'#add an email subscriber to the topicsubscriber_response = sns.subscribe(TopicArn=Topic_ARN,Protocol='email',Endpoint=email_address)
In the code given above:
Line 8: We create a topic in Amazon SNS using the create_topic
function in Boto3.
Lines 17–21: We add a subscriber to the SNS topic where the subscriber endpoint is email
.
If the code above is executed successfully, you’ll receive a confirmation email at the email address you provided in the code given above. Click the confirmation link in the email to receive notifications from the SNS topic.
Replace the {{access_key_id}}
and {{secret_access_key}}
placeholders with your AWS access key and secret access key on lines 5–6 and replace the <provide your topic arn here>
placeholder with your topic ARN on line 10 in the following code and click the "Run" button to receive a message from SNS on your email address.
import boto3import json#create a boto3 clientsns = boto3.client('sns',region_name='us-east-1',aws_access_key_id="{{access_key_id}}",aws_secret_access_key="{{secret_access_key}}")#Publish messages to the topicmessage_one = sns.publish (TargetArn = '<provide your topic arn here>',Message = json.dumps({'Message': "Hello from Educative" }),)print('Message sent successfully')
In the code given above:
Line 9–12: We publish a message on Amazon SNS using the publish
function in Boto3.
After this code is executed, you'll receive a message from SNS on your email address.
Amazon SNS has various use cases, some of which are given below:
Decoupled architecture: Amazon SNS can be used to decouple architecture in the AWS cloud, where components need to send the same message to other application components.
Application notifications: Amazon SNS can be used to notify an application's developers/users about application events.
Sending alerts: Amazon SNS can be used to send alerts to customers or clients. For example, we can notify users once their order is confirmed using Amazon SNS.
Amazon SNS is a service that uses the pub-sub method to securely send messages between publishers and subscribers. SNS supports multiple subscribers such as AWS Lambda, Amazon SQS, email, and text messages. Through this service, we can filter the type of messages forwarded to a topics' subscriber and create a fan-out architecture.
Free Resources