

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

# 创建和使用 CloudShell VPC 环境所需的 IAM 权限
<a name="aws-cloudshell-vpc-permissions-1"></a>

要创建和使用 CloudShell VPC 环境，IAM 管理员必须允许访问特定于 VPC 的 Amazon EC2 权限。本节列出了创建和使用 VPC 环境所需的 Amazon EC2 权限。

 要创建 VPC 环境，分配给您的角色的 IAM 策略必须包含以下 Amazon EC2 权限：
+ `ec2:DescribeVpcs`
+ `ec2:DescribeSubnets`
+ `ec2:DescribeSecurityGroups`
+ `ec2:DescribeDhcpOptions`
+ `ec2:DescribeNetworkInterfaces`
+ `ec2:CreateTags`
+ `ec2:CreateNetworkInterface`
+ `ec2:CreateNetworkInterfacePermission`

建议包括：
+ **ec2:DeleteNetworkInterface**

**注意**  
此权限不是强制性的，但这是清理由 CloudShell 其创建的 ENI 资源（为 CloudShell VPC 环境ENIs 创建的，标有**ManagedByCloudShell**密钥）所必需的。如果未启用此权限，则必须在每次 CloudShell VPC 环境使用后手动清理 ENI 资源。

## 授予完全 CloudShell 访问权限（包括 VPC 访问权限）的 IAM 策略
<a name="cshell-vpc-full-access-example-1"></a>

以下示例显示了如何启用完全权限，包括对 VPC 的访问权限 CloudShell：

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

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Sid": "AllowCloudShellOperations",
			"Effect": "Allow",
			"Action": [
				"cloudshell:*"
			],
			"Resource": "*"
		},
		{
			"Sid": "AllowDescribeVPC",
			"Effect": "Allow",
			"Action": [
				"ec2:DescribeSubnets",
				"ec2:DescribeSecurityGroups",
				"ec2:DescribeVpcs"
			],
			"Resource": "*"
		},
		{
			"Sid": "AllowInspectVPCConfigurationViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:DescribeDhcpOptions",
				"ec2:DescribeNetworkInterfaces"
			],
			"Resource": "*",
			"Condition": {
				"ForAnyValue:StringEquals": {
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		},
		{
			"Sid": "AllowCreateTagWithCloudShellKeyViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:CreateTags"
			],
			"Resource": "arn:aws:ec2:*:*:network-interface/*",
			"Condition": {
				"StringEquals": {
					"ec2:CreateAction": "CreateNetworkInterface"
				},
				"ForAnyValue:StringEquals": {
					"aws:TagKeys": "ManagedByCloudShell",
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		},
		{
			"Sid": "AllowCreateNetworkInterfaceWithSubnetsAndSGViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:CreateNetworkInterface"
			],
			"Resource": [
				"arn:aws:ec2:*:*:subnet/*",
				"arn:aws:ec2:*:*:security-group/*"
			],
			"Condition": {
				"ForAnyValue:StringEquals": {
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		},
		{
			"Sid": "AllowCreateNetworkInterfaceWithCloudShellTagViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:CreateNetworkInterface"
			],
			"Resource": "arn:aws:ec2:*:*:network-interface/*",
			"Condition": {
				"ForAnyValue:StringEquals": {
					"aws:TagKeys": "ManagedByCloudShell",
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		},
		{
			"Sid": "AllowCreateNetworkInterfacePermissionWithCloudShellTagViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:CreateNetworkInterfacePermission"
			],
			"Resource": "arn:aws:ec2:*:*:network-interface/*",
			"Condition": {
				"StringEquals": {
					"aws:ResourceTag/ManagedByCloudShell": ""
				},
				"ForAnyValue:StringEquals": {
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		},
		{
			"Sid": "AllowDeleteNetworkInterfaceWithCloudShellTagViaCloudShell",
			"Effect": "Allow",
			"Action": [
				"ec2:DeleteNetworkInterface"
			],
			"Resource": "arn:aws:ec2:*:*:network-interface/*",
			"Condition": {
				"StringEquals": {
					"aws:ResourceTag/ManagedByCloudShell": ""
				},
				"ForAnyValue:StringEquals": {
					"aws:CalledVia": "cloudshell.amazonaws.com"
				}
			}
		}
	]
}
```

------

## 将 IAM 条件键用于 VPC 环境
<a name="cloudshell-vpc-condition-keys-1"></a>

您可以在 VPC 设置中使用 CloudShell特定条件密钥，为您的 VPC 环境提供额外的权限控制。还可以指定可以使用和不能使用 VPC 环境的子网和安全组。

CloudShell 在 IAM 策略中支持以下条件键：
+ `CloudShell:VpcIds`— 允许或拒绝一个或多个 VPCs
+ `CloudShell:SubnetIds` – 允许或拒绝一个或多个子网
+ `CloudShell:SecurityGroupIds` – 允许或拒绝一个或多个安全组

**注意**  
如果修改了有权访问公共 CloudShell 环境的用户的权限以增加对`cloudshell:createEnvironment`操作的限制，他们仍然可以访问其现有的公共环境。但是，如果您想修改具有此限制的 IAM 策略并禁用他们对现有公共环境的访问权限，则必须先使用该限制更新 IAM 策略，然后确保您账户中的每个 CloudShell 用户都使用 CloudShell Web 用户界面（**操作** → **删除 CloudShell 环境）手动删除现有的公共环境**。

## 带有用于 VPC 设置的条件键的策略示例
<a name="vpc-condition-keys-examples-1"></a>

以下示例演示如何将条件键用于 VPC 设置。创建具有所需限制的策略语句后，为目标用户或角色附加策略语句。

### 确保用户仅创建 VPC 环境并拒绝创建公共环境
<a name="permission-to-create-VPC-env-only-example-1"></a>

要确保用户只能创建 VPC 环境，请使用拒绝权限，如以下示例所示：

```
{
  "Statement": [
    {
      "Sid": "DenyCloudShellNonVpcEnvironments",
      "Action": [
        "cloudshell:CreateEnvironment"
      ],
      "Effect": "Deny",
      "Resource": "*",
      "Condition": {
        "Null": {
          "cloudshell:VpcIds": "true"
        }
      }
    }
  ]
}
```

### 拒绝用户访问特定 VPCs、子网或安全组
<a name="permission-deny-access-example-1"></a>

要拒绝用户访问特定的 VPCs内容，`StringEquals`请使用检查`cloudshell:VpcIds`条件的值。以下示例拒绝用户访问 `vpc-1` 和 `vpc-2`：

要拒绝用户访问特定的 VPCs内容，`StringEquals`请使用检查`cloudshell:SubnetIds`条件的值。以下示例拒绝用户访问 `subnet-1` 和 `subnet-2`：

要拒绝用户访问特定的 VPCs内容，`StringEquals`请使用检查`cloudshell:SecurityGroupIds`条件的值。以下示例拒绝用户访问 `sg-1` 和 `sg-2`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "EnforceOutOfSecurityGroups",
      "Action": [
        "cloudshell:CreateEnvironment"
      ],
      "Effect": "Deny",
      "Resource": "*",
      "Condition": {
        "ForAnyValue:StringEquals": {
          "cloudshell:SecurityGroupIds": [
            "sg-1",
            "sg-2"
          ]
        }
      }
    }
  ]
}
```

------

### 允许用户使用特定 VPC 配置创建环境
<a name="create-env-with-specific-VPC-conf-1"></a>

要允许用户访问特定的 VPCs，`StringEquals`请使用检查`cloudshell:VpcIds`条件的值。以下示例允许用户访问 `vpc-1` 和 `vpc-2`：

要允许用户访问特定的 VPCs，`StringEquals`请使用检查`cloudshell:SubnetIds`条件的值。以下示例允许用户访问 `subnet-1` 和 `subnet-2`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "EnforceStayInSpecificSubnets",
      "Action": [
        "cloudshell:CreateEnvironment"
      ],
      "Effect": "Allow",
      "Resource": "*",
      "Condition": {
        "ForAllValues:StringEquals": {
          "cloudshell:SubnetIds": [
            "subnet-1",
            "subnet-2"
          ]
        }
      }
    }
  ]
}
```

------

要允许用户访问特定的 VPCs，`StringEquals`请使用检查`cloudshell:SecurityGroupIds`条件的值。以下示例允许用户访问 `sg-1` 和 `sg-2`：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "EnforceStayInSpecificSecurityGroup",
      "Action": [
        "cloudshell:CreateEnvironment"
      ],
      "Effect": "Allow",
      "Resource": "*",
      "Condition": {
        "ForAllValues:StringEquals": {
          "cloudshell:SecurityGroupIds": [
            "sg-1",
            "sg-2"
          ]
        }
      }
    }
  ]
}
```

------