

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

# 搭配 IAM 政策使用標籤
<a name="tagging-iot-iam"></a>

您可以在用於受管整合 API 動作的 IAM 政策中套用標籤型資源層級許可。這可讓您更有效地控制使用者可以建立、修改或使用哪些資源。您可以使用 `Condition` 元素 (也稱為 `Condition` 區塊)，以及 IAM 政策中的以下條件內容金鑰和值，來根據資源標籤控制使用者存取 (許可)：
+ 使用 `aws:ResourceTag/{{tag-key}}: {{tag-value}}` 以允許或拒絕資源上具有特定標籤的使用者動作。
+ 使用 `aws:RequestTag/{{tag-key}}: {{tag-value}}` 以在提出 API 請求時，要求使用 (或不使用) 特定標籤，以建立或修改允許標籤的資源。
+ 使用 `aws:TagKeys: [{{tag-key}}, ...]` 以在提出 API 請求時，要求使用 (或不使用) 特定標籤金鑰集，以建立或修改允許標籤的資源。

**注意**  
IAM 政策中的條件內容索引鍵和值僅適用於那些受管整合動作，其中能夠標記資源的識別符是必要的參數。例如，根據條件內容索引鍵和值，不允許或拒絕使用 [GetCustomEndpoint](https://docs.aws.amazon.com/iot-mi/latest/APIReference/API_GetCustomEndpoint)，因為在此請求中不會參考任何可標記的資源 （受管物件、佈建設定檔、登入資料儲存庫、over-the-air任務）。如需可標記的受管整合資源及其支援的條件索引鍵的詳細資訊，請參閱 [的 AWS IoT 受管整合功能的動作、資源和條件索引鍵 AWS IoT Device Management](https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsiotmanagedintegrationsfeatureofiotdevicemanagement.html)。

如需使用標籤的詳細資訊，請參閱《AWS Identity and Access Management 使用者指南》**中的[使用標籤控制](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html)。該指南的 [IAM JSON 政策參考](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies.html)章節有詳細的語法、說明，還有元素、變數範例，以及在 IAM 中的 JSON 政策評估邏輯。

下列範例政策會套用兩個以標籤為基礎的 `CreateManagedThing` 動作限制。受到此政策限制的 IAM 使用者：
+ 無法建立標籤為 "env=prod" 的受管物件 （在範例中，請參閱行 `"aws:RequestTag/env" : "prod"`)。
+ 無法修改或存取具有現有標籤 "env=prod" 的受管物件 （在範例中，請參閱行 `"aws:ResourceTag/env" : "prod"`)。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "iotmanagedintegrations:CreateManagedThing",
      "Resource": "arn:aws:iotmanagedintegrations:{{us-east-1}}:{{123456789012}}:managed-thing/*",
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/env": "prod"
        }
      }
    },
    {
      "Effect": "Deny",
      "Action": [
        "iotmanagedintegrations:CreateManagedThing",
        "iotmanagedintegrations:DeleteManagedThing",
        "iotmanagedintegrations:GetManagedThing",
        "iotmanagedintegrations:UpdateManagedThing"
      ],
      "Resource": "arn:aws:iotmanagedintegrations:{{us-east-1}}:{{123456789012}}:managed-thing/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/env": "prod"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": [
        "iotmanagedintegrations:CreateManagedThing",
        "iotmanagedintegrations:DeleteManagedThing",
        "iotmanagedintegrations:GetManagedThing",
        "iotmanagedintegrations:UpdateManagedThing"
      ],
      "Resource": "*"
    }
  ]
}
```

------

您也可以透過將其包含在清單中，為特定標籤金鑰指定多個標籤值，如下所示：

```
            "StringEquals" : {
              "aws:ResourceTag/env" : ["dev", "test"]
            }
```

**注意**  
如果您允許或拒絕使用者根據標籤存取資源，請務必考慮明確拒絕使用者將這些標籤新增至相同資源或從中移除的能力。否則，使用者可能透過修改標籤來避開您的限制，並取得資源的存取。