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).
Add tags to an Amazon SNS topic using an Amazon SDK
The following code examples show how to add tags to an Amazon SNS topic.
- Java
-
- SDK for Java 2.x
-
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);
}
}
- Kotlin
-
- SDK for Kotlin
-
This is prerelease documentation for a feature in preview release. It is subject to change.
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 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.