

# 适用于 VPC 对等连接的 Identity and Access Management
<a name="security-iam"></a>

默认情况下， 用户无法创建或修改 VPC 对等连接。要授予对 VPC 对等连接资源的访问权限，请将 IAM policy 附加到角等 IAM 身份。

**Topics**
+ [创建 VPC 对等连接](#vpc-peering-iam-create)
+ [接受 VPC 对等连接](#vpc-peering-iam-accept)
+ [删除 VPC 对等连接](#vpc-peering-iam-delete)
+ [在特定账户中工作](#vpc-peering-iam-account)
+ [在控制台中管理 VPC 对等连接](#peering-connection-console-iam)

有关 Amazon VPC 操作以及每个操作支持的资源和条件键的列表，请参阅《服务授权参考》**中的 [Actions, resources, and condition keys for Amazon EC2](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonec2.html)。

## 示例：创建 VPC 对等连接
<a name="vpc-peering-iam-create"></a>

以下策略授予用户权限以使用标记有 `Purpose=Peering` 的 VPC 来创建 VPC 对等连接请求。第一条语句对 VPC 资源应用条件键 (`ec2:ResourceTag`)。请注意，`CreateVpcPeeringConnection` 操作的 VPC 资源始终为请求者 VPC。

第二条语句授予用户权限以创建 VPC 对等连接资源，因此使用 \$1 通配符代替特定资源 ID。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Effect":"Allow",
      "Action": "ec2:CreateVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc/*",
      "Condition": {
        "StringEquals": {
          "ec2:ResourceTag/Purpose": "Peering"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": "ec2:CreateVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc-peering-connection/*"
    }
  ]
}
```

------

以下策略授予特定 AWS 账户中的用户权限，以使用指定区域中的任何 VPC 创建 VPC 对等连接，但是仅当接受对等连接的 VPC 是特定账户中的特定 VPC 时。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:CreateVpcPeeringConnection",
            "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc/*"
        },
        {
            "Effect": "Allow",
            "Action": "ec2:CreateVpcPeeringConnection",
            "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc-peering-connection/*",
            "Condition": {
                "ArnEquals": {
                    "ec2:AccepterVpc": "arn:aws:ec2:us-east-1:111122223333:vpc/vpc-1234567890abcdef0"
                }
            }
        }
    ]
}
```

------

## 示例：接受 VPC 对等连接
<a name="vpc-peering-iam-accept"></a>

以下策略授予用户权限，以接受来自特定 AWS 账户的 VPC 对等连接请求。这样有助于防止用户接受来自未知账户的 VPC 对等连接请求。语句使用 `ec2:RequesterVpc` 条件键强制实施此策略。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Effect":"Allow",
      "Action": "ec2:AcceptVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc-peering-connection/*",
      "Condition": {
        "ArnEquals": {
          "ec2:RequesterVpc": "arn:aws:ec2:us-east-1:111122223333:vpc/*"
        }
      }
    },
    {
      "Effect":"Allow",
      "Action": "ec2:AcceptVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc/*"
    }
  ]
}
```

------

以下策略授予用户权限，以便在仅当 VPC 具有标签 `Purpose=Peering` 时才接受 VPC 对等请求。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Effect":"Allow",
      "Action": "ec2:AcceptVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc-peering-connection/*"
    },
    {
      "Effect": "Allow",
      "Action": "ec2:AcceptVpcPeeringConnection",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc/*",
      "Condition": {
        "StringEquals": {
          "ec2:ResourceTag/Purpose": "Peering"
        }
      }
    }
  ]
}
```

------

## 示例：删除 VPC 对等连接
<a name="vpc-peering-iam-delete"></a>

以下策略授予特定账户中的用户权限以删除任何 VPC 对等连接，使用指定 VPC （处于同一账户中）的连接除外。该策略同时指定 `ec2:AccepterVpc` 和 `ec2:RequesterVpc` 条件密钥，因为 VPC 可能是原始 VPC 对等连接请求中的请求者 VPC 或对等方 VPC。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:DeleteVpcPeeringConnection",
            "Resource": "arn:aws:ec2:us-east-1:123456789012:vpc-peering-connection/*",
            "Condition": {
                "ArnNotEquals": {
                    "ec2:AccepterVpc": "arn:aws:ec2:us-east-1:123456789012:vpc/vpc-1234567890abcdef0",
                    "ec2:RequesterVpc": "arn:aws:ec2:us-east-1:123456789012:vpc/vpc-0abcdef1234567890"
                }
            }
        }
    ]
}
```

------

## 示例：在特定账户中工作
<a name="vpc-peering-iam-account"></a>

以下策略授予用户权限以在特定账户中处理 VPC 对等连接。用户可以查看、创建、接受、拒绝和删除 VPC 对等连接，前提是他们都处于相同的 AWS 账户中。

第一条语句授予用户权限以查看所有 VPC 对等连接。在这种情况下，`Resource` 元素需要 \$1 通配符，因为此 API 操作 (`DescribeVpcPeeringConnections`) 当前不支持资源级权限。

第二条语句授予用户权限以创建 VPC 对等连接，并访问特定账户中的所有 VPC 以便执行此操作。

第三条语句使用 \$1 通配符作为 `Action` 元素的一部分，以便为所有 VPC 对等连接操作授予权限。条件密钥确保只能对与属于账户的 VPC 建立的 VPC 对等连接执行操作。例如，如果接受者或请求者 VPC 属于不同账户，则用户无法删除 VPC 对等连接。用户无法与属于不同账户的 VPC 建立 VPC 对等连接。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:DescribeVpcPeeringConnections",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:CreateVpcPeeringConnection",
                "ec2:AcceptVpcPeeringConnection"
            ],
            "Resource": "arn:aws:ec2:*:111122223333:vpc/*"
        },
        {
            "Effect": "Allow",
            "Action": "ec2:*VpcPeeringConnection",
            "Resource": "arn:aws:ec2:*:111122223333:vpc-peering-connection/*",
            "Condition": {
                "ArnEquals": {
                    "ec2:AccepterVpc": "arn:aws:ec2:*:111122223333:vpc/*",
                    "ec2:RequesterVpc": "arn:aws:ec2:*:111122223333:vpc/*"
                }
            }
        }
    ]
}
```

------

## 示例：使用控制台管理 VPC 对等连接
<a name="peering-connection-console-iam"></a>

要在 Amazon VPC 控制台中查看 VPC 对等连接，用户必须拥有使用 `ec2:DescribeVpcPeeringConnections` 操作的权限。要使用**创建对等连接**页面，用户必须拥有使用 `ec2:DescribeVpcs` 操作的权限。这可授予其权限以查看和选择 VPC。您可以将资源级权限应用于所有 `ec2:*PeeringConnection` 权限 (`ec2:DescribeVpcPeeringConnections` 除外)。

以下策略授予用户权限以查看 VPC 对等连接，并使用 **Create VPC Peering Connection**（创建 VPC 对等连接）对话框创建仅使用特定请求者 VPC 的 VPC 对等连接。如果用户尝试创建使用不同请求者 VPC 的 VPC 对等连接，则请求会失败。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect":"Allow",
      "Action": [
        "ec2:DescribeVpcPeeringConnections", "ec2:DescribeVpcs"
      ],
      "Resource": "*"
    },
    {
      "Effect":"Allow",
      "Action": "ec2:CreateVpcPeeringConnection",
      "Resource": [
        "arn:aws:ec2:*:*:vpc/vpc-1234567890abcdef0",
        "arn:aws:ec2:*:*:vpc-peering-connection/*"
      ]
    }
  ]
}
```

------