

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon SNS トピックのタグ付け
<a name="sns-tags"></a>

Amazon SNS では、Amazon SNS トピックのタグ付けがサポートされています。これにより、トピックに関連するコストを追跡および管理し、 AWS Identity and Access Management (IAM) ポリシーでセキュリティを強化し、数千のトピックを簡単に検索またはフィルタリングできます。タグ付けにより、 AWS Resource Groups を使用して Amazon SNS トピックを管理できます。Resource Groups の詳細については、[「AWS Resource Groups ユーザーガイド」](https://docs.aws.amazon.com/ARG/latest/userguide/resource-groups.html)を参照してください。

## コスト割り当てのタグ付け
<a name="tagging-for-cost-allocation"></a>

コスト割り当てのために Amazon SNS トピックを整理および識別するには、トピックの目的を識別するタグを追加することができます。これは特に、多くのトピックを持っている場合に便利です。コスト配分タグを使用して、独自のコスト構造を反映するように AWS 請求書を整理できます。これを行うには、サインアップして AWS アカウント請求書を取得し、タグキーと値を含めます。詳細については、[「AWS Billing and Cost Management User Guide」](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-what-is.html)の[「Setting Up a Monthly Cost Allocation Report」](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/configurecostallocreport.html#allocation-report)を参照してください。

たとえば、次のように Amazon SNS のトピックのコストセンターや目的を表すタグを追加することができます。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/sns/latest/dg/sns-tags.html)

このタグ付けスキームでは、関連するタスクを実行している 2 つのトピックを同じコストセンターにグループ化する一方で、関連性のないアクティビティには別のコスト割り当てタグでタグ付けすることができます。

## アクセス制御のタグ付け
<a name="sns-tagging-for-access-control"></a>

AWS Identity and Access Management は、タグに基づくリソースへのアクセスの制御をサポートしています。リソースにタグ付け後、IAM ポリシーの条件要素でリソース タグに関する情報を提供し、タグベースのアクセスを管理します。[Amazon SNS console](sns-tags-configuring.md#list-add-update-remove-tags-for-topic-aws-console) (Amazon SNS コンソール) または [AWS SDK ](sns-tags-configuring.md#tag-resource-aws-sdks)を使用してリソースにタグ付けする方法については、[「Configuring tags」](sns-tags-configuring.md) (タグを設定する) を参照してください。

IAM アイデンティティのアクセスを制限できます。たとえば、キー `environment` および値 `production` のタグを含むすべてのAmazon SNS トピックへの `Publish` および `PublishBatch` のアクセスを制限し、他のすべての Amazon SNS トピックへのアクセスを許可することができます。次の例では、ポリシーにより、`production` でタグ付けされたトピックにメッセージを公開する機能が制限され、`development` でタグ付けされた トピックにメッセージを公開できるようになります。詳細については、IAM ユーザーガイドの [[Controlling Access Using Tags]](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html) (タグを使用したアクセス制御) を参照してください。

**注記**  
`Publish` の IAM の許可を設定すると、`Publish` および `PublishBatch` の両方に権限が設定されます。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [{
    "Effect": "Deny",
    "Action": [
	"sns:Publish"
    ],
    "Resource": "arn:aws:sns:*:*:*",
    "Condition": {
      "StringEquals": {
        "aws:ResourceTag/environment": "production"
      }
    }
  },
  {
    "Effect": "Allow",
    "Action": [
      "sns:Publish"
    ],
    "Resource": "arn:aws:sns:*:*:*",
    "Condition": {
      "StringEquals": {
        "aws:ResourceTag/environment": "development"
      }
    }
  }]
}
```

------

## リソース検索およびフィルタリングのタグ付け
<a name="sns-tagging-for-searching-filtering"></a>

 AWS アカウントには、数万の Amazon SNS トピックを含めることができます (詳細については、[Amazon SNS クォータ](https://docs.aws.amazon.com/general/latest/gr/sns.html)」を参照してください）。トピックにタグ付けすることで、トピックの検索やフィルタリングのプロセスを簡素化することができます。

たとえば、本番環境に関連するトピックが数百個ある場合があります。これらのトピックを手動で検索するのではなく、指定されたタグを持つすべてのトピックを照会することができます。

```
import com.amazonaws.services.resourcegroups.AWSResourceGroups;
import com.amazonaws.services.resourcegroups.AWSResourceGroupsClientBuilder;
import com.amazonaws.services.resourcegroups.model.QueryType;
import com.amazonaws.services.resourcegroups.model.ResourceQuery;
import com.amazonaws.services.resourcegroups.model.SearchResourcesRequest;
import com.amazonaws.services.resourcegroups.model.SearchResourcesResult;

public class Example {
    public static void main(String[] args) {
        // Query Amazon SNS Topics with tag "keyA" as "valueA"
        final String QUERY = "{\"ResourceTypeFilters\":[\"AWS::SNS::Topic\"],\"TagFilters\":[{\"Key\":\"keyA\", \"Values\":[\"valueA\"]}]}";

        // Initialize ResourceGroup client
        AWSResourceGroups awsResourceGroups = AWSResourceGroupsClientBuilder
            .standard()
            .build();

        // Query all resources with certain tags from ResourceGroups 
        SearchResourcesResult result = awsResourceGroups.searchResources(
            new SearchResourcesRequest().withResourceQuery(
                new ResourceQuery()
                .withType(QueryType.TAG_FILTERS_1_0)
                .withQuery(QUERY)
            ));
        System.out.println("SNS Topics with certain tags are " + result.getResourceIdentifiers());
    }
}
```

# Amazon SNS トピックタグを設定
<a name="sns-tags-configuring"></a>

このトピックでは、、 AWS SDK AWS マネジメントコンソール、または を使用して [Amazon SNS トピック](sns-tags.md)のタグを設定する方法について説明します AWS CLI。

**重要**  
個人情報 (PII) などの機密情報や秘匿性の高い情報はタグに追加しないようにします。タグは、他の Amazon Web Services のサービス (請求など) からアクセスできます。タグは、プライベートデータまたは機密データに使用することを目的としたものではありません。

## を使用した Amazon SNS トピックのタグの一覧表示、追加、削除 AWS マネジメントコンソール
<a name="list-add-update-remove-tags-for-topic-aws-console"></a>

1. [Amazon SNS コンソール](https://console.aws.amazon.com/sns/home)にサインインします。

1. ナビゲーションパネルで、[**トピック**] を選択します。

1. [**トピック**] ページで、トピックを選択して [**編集**] を選択します。

1. [**タグ**] セクションを展開します。

   トピックに追加されているタグが一覧表示されます。

1. トピックのタグを変更します。
   + タグを追加するには、**[Add tag]** (タグの追加) を選択し、**[Key]** (キー) と **[Value]** (値) (オプション) を入力します。
   + タグを削除するには、キー値ペアの横にある [**タグの削除**] を選択します。

1. **[Save changes]** (変更の保存) をクリックします。

## AWS SDK を使用してトピックにタグを追加する
<a name="tag-resource-aws-sdks"></a>

 AWS SDK を使用するには、認証情報を使用して設定する必要があります。詳細については、「*AWS SDK とツールのリファレンスガイド*」の「[共有設定ファイルと認証情報ファイル](https://docs.aws.amazon.com/sdkref/latest/guide/creds-config-files.html)」を参照してください。

次のサンプルコードは、`TagResource` を使用する方法を説明しています。

------
#### [ CLI ]

**AWS CLI**  
**トピックにタグを追加するには**  
次の `tag-resource` の例では、指定した Amazon SNS トピックにメタデータタグを追加します。  

```
aws sns tag-resource \
    --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \
    --tags Key=Team,Value=Alpha
```
このコマンドでは何も出力されません。  
+  API の詳細については、AWS CLI コマンドリファレンス**の「[TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/tag-resource.html)」を参照してください。

------
#### [ Java ]

**SDK for Java 2.x**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns#code-examples)での設定と実行の方法を確認してください。

```
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);
        }
    }
}
```
+  API の詳細については、*AWS SDK for Java 2.x API リファレンス*の「[TagResource](https://docs.aws.amazon.com/goto/SdkForJavaV2/sns-2010-03-31/TagResource)」を参照してください。

------
#### [ Kotlin ]

**SDK for Kotlin**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/sns#code-examples)での設定と実行の方法を確認してください。

```
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.fromEnvironment { region = "us-east-1" }.use { snsClient ->
        snsClient.tagResource(request)
        println("Tags have been added to $topicArn")
    }
}
```
+  API の詳細については、*AWS SDK for Kotlin API リファレンス*の「[TagResource](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

------

## Amazon SNS API アクションによるタグの管理
<a name="manage-tags-with-sns-api-actions"></a>

Amazon SNS API を使ってタグを管理するには、以下の API アクションを使用します。
+ [https://docs.aws.amazon.com/sns/latest/api/API_ListTagsForResource.html](https://docs.aws.amazon.com/sns/latest/api/API_ListTagsForResource.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_TagResource.html](https://docs.aws.amazon.com/sns/latest/api/API_TagResource.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/sns/latest/api/API_UntagResource.html)

## ABAC をサポートする API アクション
<a name="api-actions-that-support-abac"></a>

以下に、属性ベースのアクセスコントロール (ABAC) をサポートする API アクションの一覧を示します。ABAC の詳細については、*IAM ユーザーガイド*の[「ABAC とは AWS](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction_attribute-based-access-control.html)」を参照してください。
+ [https://docs.aws.amazon.com/sns/latest/api/API_AddPermission.html](https://docs.aws.amazon.com/sns/latest/api/API_AddPermission.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_DeleteTopic.html](https://docs.aws.amazon.com/sns/latest/api/API_DeleteTopic.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_GetDataProtectionPolicy.html](https://docs.aws.amazon.com/sns/latest/api/API_GetDataProtectionPolicy.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_GetSubscriptionAttributes.html](https://docs.aws.amazon.com/sns/latest/api/API_GetSubscriptionAttributes.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_GetTopicAttributes.html](https://docs.aws.amazon.com/sns/latest/api/API_GetTopicAttributes.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_ListSubscriptionsByTopic.html](https://docs.aws.amazon.com/sns/latest/api/API_ListSubscriptionsByTopic.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_ListTagsForResource.html](https://docs.aws.amazon.com/sns/latest/api/API_ListTagsForResource.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_Publish.html](https://docs.aws.amazon.com/sns/latest/api/API_Publish.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_PublishBatch.html](https://docs.aws.amazon.com/sns/latest/api/API_PublishBatch.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_PutDataProtectionPolicy.html](https://docs.aws.amazon.com/sns/latest/api/API_PutDataProtectionPolicy.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_RemovePermission.html](https://docs.aws.amazon.com/sns/latest/api/API_RemovePermission.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html](https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_SetTopicAttributes.html](https://docs.aws.amazon.com/sns/latest/api/API_SetTopicAttributes.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html](https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_TagResource.html](https://docs.aws.amazon.com/sns/latest/api/API_TagResource.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_Unsubscribe.html](https://docs.aws.amazon.com/sns/latest/api/API_Unsubscribe.html)
+ [https://docs.aws.amazon.com/sns/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/sns/latest/api/API_UntagResource.html)