

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Direct Connect 基于身份的策略示例（使用基于标签的条件）
<a name="security_iam_resource-based-policy-examples"></a>

您可以使用标签键条件控制对资源和请求的访问。您还可以在 IAM policy 中使用条件来控制是否可以在资源或请求中使用特定标签键。

有关如何在 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>

以下示例显示您可以如何创建此类策略：仅当标签包含环境键和预生产或生产值时，才允许关联虚拟接口。

------
#### [ 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 操作将标签附加到虚拟接口。作为最佳实践，请将 `ForAllValues` 修饰符与 `aws:TagKeys` 条件键配合使用，以指示只允许在请求中使用键环境。

 

------
#### [ 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 policy 中使用条件来控制是否可以在资源或请求中使用特定标签键。

以下示例显示如何可以创建一个策略，让您能够标记资源但仅限于标签键环境

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

****  

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

------