

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

# Direct Connect 身分型政策範例使用標籤型條件
<a name="security_iam_resource-based-policy-examples"></a>

透過使用標籤金鑰條件，您就可以控制對於資源和請求的存取。您也可以在 IAM 政策中使用條件，控制可在資源或請求中使用特定的標籤金鑰。

如需有關如何使用具有 IAM 政策之標籤的資訊，請參閱《*IAM 使用者指南*》中的[使用標籤控制存取權](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_iam-tags.html)。

## 根據標籤與 Direct Connect 虛擬介面產生關聯
<a name="security_iam_resource-based-policy-examples-associate-interface"></a>

以下範例將示範如何建立政策，以便僅在標籤包含環境金鑰、preprod 或 production 等值的條件下，才能與虛擬介面建立關聯。

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

****  

```
       {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "directconnect:AssociateVirtualInterface"
          ],
          "Resource": "arn:aws:directconnect:*:*:dxvif/*",
          "Condition": {
            "StringEquals": {
              "aws:ResourceTag/environment": [
                "preprod",
                "production"
              ]
            }
          }
        },
        {
          "Effect": "Allow",
          "Action": "directconnect:DescribeVirtualInterfaces",
          "Resource": "*"
        }
      ]
    }
```

------

## 根據標籤控制對請求的存取
<a name="security_iam_resource-based-policy-examples-associate-interface-requests"></a>

您可以使用 IAM 政策中的條件來控制哪些標籤鍵/值對可以在標記 AWS 資源的請求中傳遞。下列範例示範如何建立政策，允許使用 Direct Connect TagResource 動作，僅在標籤包含環境金鑰和 Preprod 或生產值時，才將標籤連接至虛擬介面。最佳實務是，搭配 `aws:TagKeys` 條件金鑰使用 `ForAllValues` 修飾詞，以表示僅允許在請求中使用金鑰環境。

 

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

****  

```
    {
        "Version":"2012-10-17",		 	 	 
        "Statement": {
            "Effect": "Allow",
            "Action": "directconnect:TagResource",
            "Resource": "arn:aws:directconnect:*:*:dxvif/*",
            "Condition": {
                "StringEquals": {
                    "aws:RequestTag/environment": [
                        "preprod",
                        "production"
                    ]
                },
                "ForAllValues:StringEquals": {"aws:TagKeys": "environment"}
            }
        }
    }
```

------

## 控制標籤鍵
<a name="security_iam_resource-based-policy-examples-associate-interface-keys"></a>

您可以在 IAM 政策中使用條件，以控制是否可對資源或在請求中使用特定標籤索引鍵。

以下範例將示範如何建立政策，以便僅在搭配標籤金鑰環境的條件下，才能為資源加上標籤。

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

****  

```
     {
      "Version":"2012-10-17",		 	 	 
      "Statement": {
        "Effect": "Allow",
        "Action": "directconnect:TagResource",
        "Resource": "*",
        "Condition": {
          "ForAllValues:StringEquals": {
            "aws:TagKeys": [
              "environment"
            ]
          }
        }
      }
    }
```

------