Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the SNS topic ARN using the topic name to publish a SNS message in Python via Boto3?

Python

method to run query

def function(run_query)
   //topic = arn return from topic name
   topic.publish(message)
   pass

I am using the boto3 resource method. There are lot of examples of using boto3.client and limited example of sns method implementation using boto3 resource methods.

like image 793
sjadhav Avatar asked Nov 23 '25 20:11

sjadhav


1 Answers

There seems to be no easy, expected get_topic_arn(topic_name) method to get an AWS topic ARN using the topic name, via the Boto3 SNS client or resource.

However, a clever workaround would be to use the create_topic method:

This action is idempotent, so if the requester already owns a topic with the specified name, that topic's ARN is returned without creating a new topic.

Call create_topic with an existing topic name, which will retrieve the SNS.Topic sub-resource, and then call the publish method on the topic sub-resource.

import boto3

sns = boto3.resource('sns')

topic = sns.create_topic(Name='MyTopicName')
topic_arn = topic.arn

response = topic.publish(Message='MyMessage')
...
like image 88
Ermiya Eskandary Avatar answered Nov 26 '25 11:11

Ermiya Eskandary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!