sjadhav
sjadhav

Reputation: 47

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.

Upvotes: 1

Views: 5307

Answers (1)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23572

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')
...

Upvotes: 8

Related Questions