Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SNS get topic by name

I started to work with AWS SNS to send push notifications to my app. Today, to get a topic that I want to send push notifications to, I need to load all topics – using SNS client function listTopics() - and check each topic to see if the ARN has the name of the topic that I'm looking for.

I think that it's a very inefficient way to get a topic ARN, and I'd like to know if there is a most efficient way to get this information, like a function getTopicByName or something else.

If not, I'd like to know if ARN is immutable, and if can I store the topic ARN on my database?

Thanks.

like image 553
GodFather Avatar asked Apr 07 '16 21:04

GodFather


2 Answers

I don't know of any way to look up an SNS topic by name.

The ARN is immutable. It will not change for the life of the SNS topic. In particular, according to the documentation an ARN for an SNS topic is in the following format:

arn:aws:sns:region:account-id:topicname

So the only way for the SNS topic's ARN to change would be to change the name (in which case your lookup by name would also break), or delete the topic and recreate it in a new region or in an entirely different account, in which case it would not really the same topic anymore.

like image 193
Mark B Avatar answered Sep 17 '22 18:09

Mark B


If you know the topic already exists or you don't care if it gets created, then a much more direct approach is simply to call CreateTopic. Given a name, CreateTopic will return the existing topic, including the ARN.

like image 45
sigpwned Avatar answered Sep 17 '22 18:09

sigpwned