Lesson 4
Creating and Configuring AWS SNS Topics with Python and Boto3
Introduction

Hello, learners! In today's lesson, we'll delve deeper into AWS Simple Notification Service (SNS). As you might remember, we touched upon SNS in our overview of AWS messaging services. The core concept we need to master in AWS SNS is 'Topics'. SNS Topics are simply communication channels, where messages can be published, and subscribers to these topics receive these messages. Our focus today will primarily be on these 'Topics'.

Understanding SNS and Its Concepts

AWS SNS Topics are a pivotal part of the Amazon Simple Notification Service. They enable you to group multiple recipients under a common area of interest, assigning a unique endpoint to each entity subscribed. Here are some important details to take note of:

  • You can create up to 100,000 SNS Topics per AWS account.
  • Each topic can support deliveries of up to 100,000,000 messages per day!
  • Messages, or 'payloads', should not be more than 256KB in size.
  • You can have nearly 12.5 million immediate subscriptions per topic!

Subscribers to an SNS topic can be various types of endpoints, such as:

  • HTTP/S endpoints: A web-based or internet-accessible endpoint.
  • Email (Text or JSON): Notifications via email in a raw, free-form text format, or a structured, JSON-formatted message.
  • Amazon SQS Queues: Messages can be queued for one or more recipients to pull and process as needed.
  • AWS Lambda functions: Subscriptions can trigger a Lambda function when new messages are published to the topic.
  • SMS: Notifications sent as text messages directly to a mobile device.
  • Mobile app platforms(like Google’s FCM, Apple's APNS, Amazon’s ADM, etc.): Primarily used for push messaging to mobile apps.
  • AWS Managed service endpoints: Services like Amazon Simple Storage Service (S3), Amazon EC2 Systems Manager, AWS CloudFormation, AWS CloudWatch, can also receive notifications from SNS.

Remember, each type of endpoint has its own message delivery retry policies, filtering capabilities, and other features that differentiate it from others. They are also subject to different pricing structures.

As an illustrative use case, consider an eCommerce giant like Amazon. SNS topics may be used to disseminate order update notifications to various subscribers. The customer could receive the updates via the Amazon app (a mobile endpoint), email, or even a text message.

Creating SNS Topics with Boto3

Now comes the engaging part — the creation of an SNS Topic. We're going to break it down:

Python
1import boto3 2 3# Initialize the SNS resource 4sns_resource = boto3.resource('sns') 5 6# Create a new SNS topic 7topic = sns_resource.create_topic(Name='MyTopic') 8topic_arn = topic.arn 9 10# Confirm the SNS topic creation 11print("Created SNS topic:", topic_arn)

Here's what happens:

  • The boto3.resource('sns') command is used to instantiate an SNS resource.
  • The create_topic(Name='MyTopic') on the SNS resource constructs a new topic named MyTopic.
  • This operation yields a topic object, from which we extract the TopicArn—unique to your topic across all AWS accounts.
  • Lastly, we validate our creation by printing the TopicArn.
Configuring SNS Topics

Once an SNS topic is created, the next step is to configure its attributes for optimal performance and security. Here's a concise overview of key attributes you might adjust:

  • DisplayName: The name used in the "From" field for email notifications.
  • Policy: A JSON document detailing access permissions for the topic.
  • DeliveryPolicy: Customizes the message retry strategy for server-side errors.
  • KmsMasterKeyId: Assigns an AWS Key Management Service (KMS) key for message encryption.
  • FifoTopic: Enables FIFO (First-In-First-Out) delivery for strict order processing.
  • ContentBasedDeduplication: Automatically reduces message duplication in FIFO topics.

To configure an attribute, use the set_attributes method on the topic object with the appropriate parameters. Here’s how to set the DisplayName, with similar steps applicable for other attributes:

Python
1# Configure the DisplayName attribute for the SNS topic 2topic.set_attributes( 3 AttributeName='DisplayName', 4 AttributeValue='My Topic Display Name' 5)

Adjusting these settings allows you to tailor SNS topic behavior to meet your specific requirements.

Listing Your SNS Topics

After creating one or more SNS topics, you might want to view or manage all your existing topics. AWS SDK for Python (Boto3) makes it straightforward to list all SNS topics within your account. Here’s how you can achieve this using the resource interface:

Python
1# Initialize the SNS resource 2sns_resource = boto3.resource('sns') 3 4# Listing all SNS topics 5topics = sns_resource.topics.all() 6 7for topic in topics: 8 print(topic.arn)
Combining SNS and SQS for Improved Message Handling

The integration of AWS SNS with SQS (Simple Queue Service) opens up new possibilities for your messaging architecture. This combination leverages the Fan-Out pattern, allowing a single published message to be delivered to multiple SQS queues, effectively persisting these messages. The stored messages in SQS are protected against loss, and can be processed as needed, adding an extra layer of robustness and flexibility to your system.

Imagine a scenario where you have an application sending order confirmations to customers and simultaneously storing a duplicate for internal processing. With SNS you could publish this order confirmation once and using the Fan-Out pattern, deliver it to multiple subscribers including an SQS queue for internal use. The combination of SNS and SQS ensures reliable delivery and processing of vital information, making your applications resilient and reliable.

Review, Practice, and Next Steps

So, what have we learned? You now know what AWS SNS is, its key concepts, and how to create, configure, and list your very own SNS topics using the resource interface. This forms the base for our future explorations of SNS — like SNS operations and a further look into Fan-out patterns!

In the upcoming exercises, you'll put this freshly acquired knowledge to the test where you'll create SNS topics and set their attributes on your own. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.