An SNS topic can be created by invoking the create_topic() function of the sns_client object. The result is the Python dictionary that represents the SNS topic. Once an SNS topic is created, AWS automatically assigns a unique ARN to the topic. This ARN can be fetched using the TopicARN key of the SNS topic.
The following Python program demonstrates the creation of an SNS topic:
import boto3
# Create an SNS client object
sns_client = boto3.client(
"sns",
# You may have to set the AWS credentials of you haven't already set using AWS CLI
# aws_access_key_id="YOUR ACCES KEY",
# aws_secret_access_key="YOUR SECRET KEY",
# region_name=us-east-1
)
# Create an SNS topic
sns_topic = sns_client.create_topic(Name="packt-pub")
topic_arn = sns_topic['TopicArn'] # fetch the resource name
Once a topic is created, subcribers can be added to the topic using their email ID or phone number.