Get startedGet started for free

SNS Topics

1. SNS Topics

In the last two chapters, we learned about cloud storage with S3 and how to share (and not share) files with the world. But what if we send a text with a link to the latest report? Or send an email if certain criteria are met in the data. Alerting humans and machines to take action is a key component in data engineering.

2. SNS

That's where Amazon SNS comes in! Amazon SNS stands for simple notification service. With it, we can send emails, text messages and push notifications. Publishers post messages to topics and subscribers receive them.

3. Understanding SNS

To fully understand SNS, let's talk about... the Superbowl.

4. Understanding SNS

I know, sports aren't my thing either. Just bear with me for the analogy.

5. Understanding SNS

Say, the Superbowl is happening right now, and CBS is broadcasting. That makes CBS a publisher.

6. Understanding SNS

CBS is publishing channel 2, and others can subscribe to it by selecting that channel. Channel 2 is like an SNS topic.

7. Understanding SNS

The channel can have phones, TVs, tablets and other devices watching. They all get the same Superbowl. In SNS, these are subscribers.

8. Understanding SNS

In SNS, we publish messages to an SNS topic, and subscribers receive those messages via email or text.

9. Accessing SNS

To access SNS, we open the AWS Services tab, type in "SNS", and click on the "Simple Notification Service" option that pops up.

10. SNS Dashboard

Once we get to the dashboard, we can see Topics and Subscriptions on the left.

11. SNS Topics

Every topic has an ARN or an Amazon resource name. This is a unique id for this topic.

12. SNS Topics

On the bottom, we can see Subscriptions. Each one of those has a unique ID as well. We'll explore those in the next lesson.

13. Creating an SNS Topic

To create an SNS topic, we initialize the SNS client. This is the first time we are using a boto3 client for something other than S3. We do this by passing SNS instead of S3 as the first argument.

14. Creating an SNS Topic

Now that we have a client, we can create a topic by calling the create_topic method.

15. Creating an SNS Topic

In return, we get an API response from AWS.

16. Creating an SNS Topic

We grab the TopicArn key from the response to store in a variable. Or we can do the whole thing with a shortcut - calling create_topic and grabbing TopicARN at the same time.

17. Creating an SNS Topic

We can see our new topic and its ARN on the dashboard. Creating topics is idempotent - if we create a topic with the same name, it will return the ARN of the topic that already exists, not an error.

18. Permissions

In the first lesson, we used IAM to configure our AWS Access Key and ID to have full access to SNS. This will allow us to create topics, make subscriptions, and publish messages.

19. Listing topics

We can list topics by calling the list_topics method.

20. Listing topics

We get a response that contains a topic's key with a list of topics our user has access to. We can do this to perform an operation like delete on multiple topics.

21. Deleting topics

Sometimes, we need to delete a topic that's outlived its usefulness. To delete a topic, pass its TopicArn to the delete_topic method. The topic is gone.

22. Review

Phew! This has been a full lesson. First, we learned how SNS works. We publish messages to an SNS topic. Subscribers to this topic will receive that message via email or text.

23. Review

To work with SNS, we have to create an SNS client. And now we can create a shiny new topic.

24. Review

To see all the created topics, we can use list_topics(). And to delete a topic, we use delete_topic.

25. Let's practice!

Now that we have the power of topics, let's practice!