

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 2：允許使用 aws：RequestTag 標記串流
<a name="streams-abac-example-2"></a>

使用 `aws:RequestTag/tag-key`條件金鑰，您可以將在請求中傳遞的標籤鍵/值對與 IAM 政策中指定的標籤對進行比較。例如，只有在請求包含特定標籤時，您才能允許標記串流。

## 使用 AWS CLI
<a name="streams-abac-example-2-cli"></a>

1. 建立[內嵌政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#inline-policies)並將其新增至角色，如下列範例所示。

   ```
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "dynamodb:TagResource"
         ],
         "Resource": "arn:aws:dynamodb:*:*:table/*/stream/*",
         "Condition": {
           "StringEquals": {
             "aws:RequestTag/CostCenter": "12345"
           }
         }
       }
     ]
   }
   ```

1. 使用所需的標籤鍵/值對來標記串流。

   ```
   aws dynamodb tag-resource \
     --resource-arn arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable/stream/2024-01-01T00:00:00.000 \
     --tags Key=CostCenter,Value=12345
   ```

## 使用和不使用 Streams ABAC 的行為
<a name="streams-abac-example-2-behavior"></a>

沒有 Streams ABAC  
如果您的 未啟用 Streams ABAC AWS 帳戶，則內嵌政策中的標籤條件和請求不相符。`TagResource` 動作會傳回 `AccessDeniedException`。

使用 Streams ABAC  
如果您的 已啟用 Streams ABAC AWS 帳戶，則標籤請求會成功完成。由於`TagResource`請求中存在 `"CostCenter": "12345"` 的標籤鍵/值對，因此內嵌政策允許 動作。