Brief Introduction to AWS SNS using Boto3

Continuing along with the course mentioned in the previous blog, the next part covered notification services using SNS. Simple Notification services or SNS are crucial when dealing with applications or data pipelines. They allow you to reliably send notifications directly to users for monitoring or alerting. In this blog, I will cover how SNS works and is used for SMS and email alerts.

In order to create an SNS topic, you must first initialize a Boto3 client for SNS. Initializing still requires a valid access key id and secret access key. Creating a topic is done by calling the create_topic method and stating the name for the new topic. This generates a unique identifier for the topic called a topicARN key. An important caveat to know is that creating topics with the same name as a topic that exists will return the ARN of the existing topic.

Creating a ‘city_alerts’ topic through SNS

In order to list all the topics we are subscribed to, we call the list_topics method. We get a response listing all the topic ARN keys that we have access to. We can also delete a topic by calling in the delete_topic method and passing in the ARN key for the topic we wish to remove.

Deleting topics that don’t have ‘critical’ within their ARN key

Each subscription to a topic has a unique ID (subscriptionARN), an endpoint, a protocol, and status. The delivery protocol refers to how the message is transmitted, such as email, http, or SMS (text messages). The endpoint is the specific address that the message should be sent to. The status of a subscription can either be ‘confirmed’ or ‘pending confirmation’. ‘Pending confirmation’ is used for emails and requires the user confirming the subscription through an separate email containing a confirmation link.

To create a subscription, we call the subscribe method and pass in the ARN, protocol, and endpoint we wish to use. Deleting subscriptions uses the unsubscribe method and pass in the subscriptionARN.

Creating SMS/phone subscriptions to the streets_critical topic and printing their respective subscription ARN

To publish a message to a topic, we use the publish method and pass in the topicARN, along with the message and subject as arguments. This message will then be broadcasted to the subscribers of the topic, who will receive notifications based on the delivery protocol. We also have the ability to send one-off messages to a phone number by simply specifying the phone number and message.

Creating a custom message that sends an alert if there are over 100 potholes

The final exercises for this part of the course were for a case study in building a notification system for critical issues for a City Council. We are asked to create a multi-level topic system(critical and extreme) for each city department based on the number of complaints. An extreme alert is considered more serious than a critical alert. A critical topic will alert the staff and managers of that department and an extreme topic will alert politicians and directors. Thus, we have different sets of subscribers depending on the threshold reached.

Create multi-level topics for each department
Setting up subscription lists. ‘Critical’ topics subscribers will receive emails and ‘extreme’ topic subscribers will receive text messages
Creating different messages for critical and extreme alerts

This was a very basic introduction to SNS as the course does not cover a number of features such as integration with other AWS services, message ordering/filtering, and topic encryption. However, Amazon does maintain documentation for the API to learn more about some of these features. In the next blog, I will look to cover more advanced services, specifically AWS Rekognition and Comprehend. Thanks for reading!

Data Science student and aspiring Data Analyst

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store