Working with Amazon SNS in Python using Boto3 SDK

This blog will show you how to use Python and the Boto3 module to connect with Amazon SNS in order to create, describe, list, and delete SNS topics as well as send messages to them.

Amazon Simple Notification Service (Amazon SNS) is a highly available, secure, and fully managed Publisher-Subscriber (pub/sub) messaging service that supports both A2A and A2P communication. Amazon SNS allows us to publish messages and distribute them to a large number of subscriber systems, including HTTPS endpoints, webhooks, and AWS services like AWS Lambda functions, Amazon SQS queues, and Amazon Kinesis Data Firehose. Furthermore, services can use SNS to distribute notifications to end users via mobile push, SMS, and email.

Benefits of using SNS:

Here’s a list of benefits of using Amazon SNS:

  • Scalability – Amazon Simple Notification Service topics may accommodate an unlimited number of publishers, subscribers, and messages.
  • Ease of work – Simple Notification Service is an AWS completely managed service, therefore setting it up requires no infrastructure work.
  • Multiple notification options – Amazon Simple Notification Service supports AWS Lambda, AWS SQS notifications, mobile push notifications, HTTP(S) endpoints, webhooks, email addresses, and SMS message.
  • Integration with AWS Lambda – The native integration with AWS Lambda allows you to run a Lambda function every time a message is published to an SNS topic

What exactly is the Boto3 library?
The Boto3 library is the AWS Software Development Kit (SDK) for Python, which lets you to use APIs to create, configure, and administer AWS services. The Boto3 SDK offers an object-oriented API as well as low-level access to AWS services. Boto3 primarily relies on another botocore package, which handles Python code to handle low-level tasks such as sending secure HTTPS API queries to AWS services and converting XML replies to Python dictionary data type.

Every Cloud Automation Engineer must have a minimal understanding of the AWS SDK for his favorite programming language. This blog article covers not only the installation and configuration of the Boto3 library but also advanced topics such boto3.client()boto3.resource() and boto3.Session() objects. The content of this blog article will allow you to start managing the AWS cloud using Python SDK in just 5 minutes.

How do I use Boto3 to connect to Amazon SNS?

You may use the Boto3 library to access APIs for managing AWS services in two ways:

  • The Boto3 client provides access to low-level API data. You can, for example, obtain API response data in JSON format.
  • The Boto3 resource enables you to use AWS services in a more object-oriented fashion. 

To begin working with Amazon SNS APIs, we can instantiate the Boto3 SNS client as follows:

Instantiating the Boto3 SNS client

import boto3
AWS_REGION = "us-east-2"
sns_client = boto3.client("sns", region_name=AWS_REGION)

Similarly, you can instantiate the Boto3 SNS resource:

Instantiating the Boto3 SNS resource

import boto3
AWS_REGION = "us-east-2"
sns_resource = boto3.resource("sns", region_name=AWS_REGION)

We will explore the following two topics in this blog:

We use paginators in our code from time to time. As a result, we must learn more about it.

Paginators

Some AWS operations return results that are incomplete and require subsequent requests in order to attain the entire result set. The process of sending subsequent requests to continue where a previous request left off is called pagination. For example, the list_objects operation of Amazon S3 returns up to 1000 objects at a time, and you must send subsequent requests with the appropriate Marker in order to retrieve the next page of results.

Paginators are a feature of boto3 that act as an abstraction over the process of iterating over an entire result set of a truncated API operation.

Creating paginators

Paginators are created via the get_paginator() method of a boto3 client. The get_paginator() method accepts an operation name and returns a reusable Paginator object. You then call the paginate method of the Paginator, passing in any relevant operation parameters to apply to the underlying API operation. The paginate method then returns an iterable PageIterator:

import boto3

# Create a client
client = boto3.client('s3', region_name='us-west-2')

# Create a reusable Paginator
paginator = client.get_paginator('list_objects')

# Create a PageIterator from the Paginator
page_iterator = paginator.paginate(Bucket='my-bucket')

for page in page_iterator:
    print(page['Contents'])