Bootstrap FreeKB - Amazon Web Services (AWS) - Publish a message to a Simple Notification Service (SNS) Topic using Python boto3
Amazon Web Services (AWS) - Publish a message to a Simple Notification Service (SNS) Topic using Python boto3


This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.

Here is the minimal boilerplate code without any error handling to publish a message to one of your Simple Notification Service (SNS) Topcis.

#!/usr/bin/python3
import boto3

client = boto3.client('sns')
client.publish(
    TopicArn='arn:aws:sns:us-east-1:123456789012:mytopic',
    Subject='Hello',
    Message='World'
)

 

Here is a more practical example, with try/except/else error handling.

#!/usr/bin/python3
import boto3
import sys

try:
  client = boto3.client('sns')
except Exception as exception:
  print(exception)
  sys.exit(1)

try:
  client.publish(
    TopicArn='arn:aws:sns:us-east-1:123456789012:mytopic',
    Subject='Hello',
    Message='World'
  )
except Exception as exception:
  print(exception)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8c11fc in the box below so that we can be sure you are a human.