创建和使用 CloudShell VPC 环境所需的 IAM 权限 - AWS CloudShell

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

创建和使用 CloudShell VPC 环境所需的 IAM 权限

要创建和使用 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 策略

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

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCloudShellOperations", "Effect": "Allow", "Action": [ "cloudshell:*" ], "Resource": "*" }, { "Sid": "AllowDescribeVPC", "Effect": "Allow", "Action": [ "ec2:DescribeDhcpOptions", "ec2:DescribeNetworkInterfaces", "ec2:DescribeSubnets", "ec2:DescribeSecurityGroups", "ec2:DescribeVpcs" ], "Resource": "*" }, { "Sid": "AllowCreateTagWithCloudShellKey", "Effect": "Allow", "Action": [ "ec2:CreateTags" ], "Resource": "arn:aws:ec2:*:*:network-interface/*", "Condition": { "StringEquals": { "ec2:CreateAction": "CreateNetworkInterface" }, "ForAnyValue:StringEquals": { "aws:TagKeys": "ManagedByCloudShell" } } }, { "Sid": "AllowCreateNetworkInterfaceWithSubnetsAndSG", "Effect": "Allow", "Action": [ "ec2:CreateNetworkInterface" ], "Resource": [ "arn:aws:ec2:*:*:subnet/*", "arn:aws:ec2:*:*:security-group/*" ] }, { "Sid": "AllowCreateNetworkInterfaceWithCloudShellTag", "Effect": "Allow", "Action": [ "ec2:CreateNetworkInterface" ], "Resource": "arn:aws:ec2:*:*:network-interface/*", "Condition": { "ForAnyValue:StringEquals": { "aws:TagKeys": "ManagedByCloudShell" } } }, { "Sid": "AllowCreateNetworkInterfacePermissionWithCloudShellTag", "Effect": "Allow", "Action": [ "ec2:CreateNetworkInterfacePermission" ], "Resource": "arn:aws:ec2:*:*:network-interface/*", "Condition": { "StringEquals": { "aws:ResourceTag/ManagedByCloudShell": "" } } }, { "Sid": "AllowDeleteNetworkInterfaceWithCloudShellTag", "Effect": "Allow", "Action": [ "ec2:DeleteNetworkInterface" ], "Resource": "arn:aws:ec2:*:*:network-interface/*", "Condition": { "StringEquals": { "aws:ResourceTag/ManagedByCloudShell": "" } } } ] }

将 IAM 条件键用于 VPC 环境

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

CloudShell 在 IAM 策略中支持以下条件键:

  • CloudShell:VpcIds— 允许或拒绝一个或多个 VPCs

  • CloudShell:SubnetIds – 允许或拒绝一个或多个子网

  • CloudShell:SecurityGroupIds – 允许或拒绝一个或多个安全组

注意

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

带有用于 VPC 设置的条件键的策略示例

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

确保用户仅创建 VPC 环境并拒绝创建公共环境

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

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

拒绝用户访问特定 VPCs、子网或安全组

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

{ "Version": "2012-10-17", "Statement": [ { "Sid": "EnforceOutOfVpc", "Action": [ "cloudshell:CreateEnvironment" ], "Effect": "Deny", "Resource": "*", "Condition": { "StringEquals": { "cloudshell:VpcIds": [ "vpc-1", "vpc-2" ] } } } ] }

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

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

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

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

允许用户使用特定 VPC 配置创建环境

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

{ "Version": "2012-10-17", "Statement": [ { "Sid": "EnforceStayInSpecificVpc", "Action": [ "cloudshell:CreateEnvironment" ], "Effect": "Allow", "Resource": "*", "Condition": { "StringEquals": { "cloudshell:VpcIds": [ "vpc-1", "vpc-2" ] } } } ] }

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

{ "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-1sg-2

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