Use TagResource with an Amazon SDK or CLI - Amazon Simple Notification Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use TagResource with an Amazon SDK or CLI

The following code examples show how to use TagResource.

CLI
Amazon CLI

To add a tag to a topic

The following tag-resource example adds a metadata tag to the specified Amazon SNS topic.

aws sns tag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tags Key=Team,Value=Alpha

This command produces no output.

  • For API details, see TagResource in Amazon CLI Command Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import software.amazon.awssdk.services.sns.model.Tag; import software.amazon.awssdk.services.sns.model.TagResourceRequest; import java.util.ArrayList; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AddTags { public static void main(String[] args) { final String usage = """ Usage: <topicArn> Where: topicArn - The ARN of the topic to which tags are added. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String topicArn = args[0]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); addTopicTags(snsClient, topicArn); snsClient.close(); } public static void addTopicTags(SnsClient snsClient, String topicArn) { try { Tag tag = Tag.builder() .key("Team") .value("Development") .build(); Tag tag2 = Tag.builder() .key("Environment") .value("Gamma") .build(); List<Tag> tagList = new ArrayList<>(); tagList.add(tag); tagList.add(tag2); TagResourceRequest tagResourceRequest = TagResourceRequest.builder() .resourceArn(topicArn) .tags(tagList) .build(); snsClient.tagResource(tagResourceRequest); System.out.println("Tags have been added to " + topicArn); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see TagResource in Amazon SDK for Java 2.x API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun addTopicTags(topicArn: String) { val tag = Tag { key = "Team" value = "Development" } val tag2 = Tag { key = "Environment" value = "Gamma" } val tagList = mutableListOf<Tag>() tagList.add(tag) tagList.add(tag2) val request = TagResourceRequest { resourceArn = topicArn tags = tagList } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.tagResource(request) println("Tags have been added to $topicArn") } }
  • For API details, see TagResource in Amazon SDK for Kotlin API reference.

For a complete list of Amazon SDK developer guides and code examples, see Using Amazon SNS with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.