AWS SNS get topic by title

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

I think this is a very inefficient way to get the ARN topic, and I would like to know if there is the most efficient way to get this information, like getTopicByName function or something else.

If not, I would like to know if the ARN is immutable, and can I save the ARN theme in my database?

Thanks.

+5
source share
3 answers

I do not know how to search for SNS topic by name.

ARN is unchanged. This will not change for the life of the SNS theme. In particular, according to the ARN documentation for the SNS topic, it is in the following format:

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

Thus, the only way to change the SNS ARN topic is to change the name (in this case, your search by name will also be violated) or delete the topic and recreate it in a new region or in a completely different account, in which case it will not be same.

+4
source

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

+7
source

If the theme is already available, you can use the "createTopic" method as follows. With this, if the requester already owns a topic with the specified name, this ARN topic is returned without creating a new topic. In the AWS Java SDK, the code will look like this.

  AWSCredentialsProvider provider = new ProfileCredentialsProvider(); AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(provider).build(); CreateTopicResult createRes = sns.createTopic("HelloTopic"); 

Then, using CreateTopicResult, you can get the ARN of the topic and post a message

 sns.publish(new PublishRequest(createRes.getTopicArn(), "Hello World")); 
+3
source

Source: https://habr.com/ru/post/1246655/


All Articles