

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

# 建立和使用 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 存取權的 IAM 政策，包括對 VPC 的存取
<a name="cshell-vpc-full-access-example-1"></a>

下列範例顯示如何啟用 CloudShell 的完整許可，包括對 VPC 的存取：

------
#### [ 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"
				}
			}
		}
	]
}
```

------

## 針對 VPC 環境使用 IAM 條件金鑰
<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"
        }
      }
    }
  ]
}
```

### 拒絕使用者存取特定 VPC、子網路或安全群組
<a name="permission-deny-access-example-1"></a>

若要拒絕使用者存取特定 VPC，請使用 `StringEquals` 來檢查 `cloudshell:VpcIds` 條件的值。下列範例拒絕使用者存取 `vpc-1`和 `vpc-2`：

若要拒絕使用者存取特定 VPC，請使用 `StringEquals` 來檢查 `cloudshell:SubnetIds` 條件的值。下列範例拒絕使用者存取 `subnet-1`和 `subnet-2`：

若要拒絕使用者存取特定 VPC，請使用 `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"
          ]
        }
      }
    }
  ]
}
```

------