Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SNS - How to get topic arn by topic name

In my application i need to send messages to the given topic name. The topic is already created by other person and in a config file they give only the topic Name. My work is to push the messages in the given topic Name. Is there any way to get topic ARN by topic NAME in java?

like image 641
Gowsikan Avatar asked Apr 19 '16 14:04

Gowsikan


2 Answers

I've done this one of two ways. The ARN is always the same pattern. So, you could just subscribe to "arn:aws:sns:<region>:<account>:<name>" where:

region is from Regions.getCurrentRegion(). Be careful with this as it is a bit expensive of a call and you'll need to handle not being on an EC2/Elastic Beanstalk instance.

account is from AmazonIdentityManagementClient.getUser().getUser().getArn(). You'll have to parse the account number from that. Same warning about not being in an EC2 environment.

name is what you have.

A simpler way is loop through the the topics and look for the name you want in the ARN. You will use the AmazonSNSClient listTopics method to do this. Remember that that method only returns the first 100 topics - you'll want to properly loop over the entire topic list.

like image 142
stdunbar Avatar answered Sep 19 '22 16:09

stdunbar


As stated in this answer using createTopic(topicName) is more direct approach. In case topic have been created before, it will just return you topic ARN.

like image 23
Bryn Avatar answered Sep 20 '22 16:09

Bryn