Shows how to use the AWS SDK for Python (Boto3) with Amazon Simple Notification Service (Amazon SNS).
Amazon SNS enables applications, end-users, and devices to instantly send and receive notifications from the cloud.
- Creating a topic
(
create_topic) - Deleting a topic
(
delete_topic) - Listing subscriptions
(
list_subscriptions) - Listing topics
(
list_topics) - Publishing a message with custom attributes
(
publish_message_attributes) - Publishing a message that differs based on the protocol of the subscriber
(
publish_message_structure) - Publishing a text message to a phone number
(
publish_text_message) - Setting subscription attributes
(
set_subscription_attributes) - Subscribing an endpoint to a topic
(
subscribe) - Unsubscribing an endpoint from a topic
(
unsubscribe)
- As an AWS best practice, grant this code least privilege, or only the permissions required to perform a task. For more information, see Grant Least Privilege in the AWS Identity and Access Management User Guide.
- This code has not been tested in all AWS Regions. Some AWS services are available only in specific Regions. For more information, see the AWS Region Table on the AWS website.
- Running this code might result in charges to your AWS account.
- You must have an AWS account, and have your default credentials and AWS Region configured as described in the AWS Tools and SDKs Shared Configuration and Credentials Reference Guide.
- Python 3.8.5 or later
- Boto3 1.15.4 or later
- PyTest 5.3.5 or later (to run unit tests)
Run this example at a command prompt with the following command.
python sns_basics.py
The example contains the following files.
sns_basics.py
Shows how to use Amazon SNS to subscribe to a topic and receive messages.
- Creates a notification topic.
- Sends a message directly to a phone number.
- Subscribes an email and a phone number to the topic.
- Publishes a multi-format message to the topic. Subscribers receive different messages depending on their notification protocol.
- Adds an attribute filter to the phone number subscription and publishes messages to the topic. Only messages with matching filters are sent to the phone number.
The unit tests in this module use the botocore Stubber. This captures requests before they are sent to AWS, and returns a mocked response. To run all of the tests, run the following command in your [GitHub root]/python/example_code/sns folder.
python -m pytest
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0