

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

# 使用 IAM 策略管理 AWS CloudShell 访问和使用情况
<a name="sec-auth-with-identities"></a>

通过提供的访问管理资源 AWS Identity and Access Management，管理员可以向 IAM 用户授予权限。这样，这些用户就可以访问 AWS CloudShell 和使用环境的功能。管理员还可以创建策略，在精细级别上指定这些用户可以在 Shell 环境中执行哪些操作。

管理员向用户授予访问权限的最快方法是通过 AWS 托管策略。[AWS 托管策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies) 是由 AWS创建和管理的独立策略。以下的 AWS 托管策略 AWS CloudShell 可以附加到 IAM 身份：
+ **AWS CloudShellFullAccess**：授予使用 AWS CloudShell 的权限，并具有对所有功能的完全访问权限。

该**AWS CloudShellFullAccess**策略使用通配符 (\$1) 来授予 IAM 身份（用户、角色或群组）对 CloudShell和功能的完全访问权限。有关此策略的更多信息，请参阅[AWS CloudShellFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSCloudShellFullAccess.html)《*AWS 托管策略用户指南》*。

**注意**  
也可以启动具有以下 AWS 托管策略的 IAM 身份 CloudShell。但是，这些策略提供了广泛的权限。因此，我们建议您仅在这些策略对于 IAM 用户的工作角色必不可少的情况下才授予这些策略。  
[管理员](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions.html#jf_administrator)：为 IAM 用户提供完全访问权限，并允许他们向中的每项服务和资源委派权限 AWS。
[开发者高级用户](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions.html#jf_developer-power-user)：使 IAM 用户能够执行应用程序开发任务，创建和配置支持 AWS 感知型应用程序开发的资源和服务。
有关如何将托管策略附加到您的实体的更多信息，请参阅* IAM 用户指南*中的[添加 IAM 身份权限（控制台）](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html#add-policies-console)。

## AWS CloudShell 使用自定义策略管理允许的操作
<a name="restricting-actions"></a>

要管理 IAM 用户可以执行的操作 CloudShell，请创建使用 CloudShellPolicy 托管策略作为模板的自定义策略。或者，编辑嵌入在相关 IAM 身份（用户、组或角色）中的[内联策略]()。

例如，您可以允许 IAM 用户访问 CloudShell，但阻止他们转发用于登录的 CloudShell 环境证书 AWS 管理控制台。

 

**重要**  
要 AWS CloudShell 从启动 AWS 管理控制台，IAM 用户需要执行以下操作的权限：  
`CreateEnvironment`
`CreateSession`
`GetEnvironmentStatus`
`StartEnvironment`
如果附加的策略未明确允许其中一项操作，则在您尝试启动时会返回 IAM 权限错误 CloudShell。


**AWS CloudShell 权限**  

| Name | 对已授予权限的描述 | 需要启动 CloudShell吗？ | 
| --- | --- | --- | 
|  `cloudshell:CreateEnvironment`  |  创建 CloudShell 环境，在会 CloudShell 话开始时检索布局，并将来自 Web 应用程序的当前布局保存在后端。如[适用于 IAM 策略的示例 CloudShell](#policy-examples)中所述，此权限仅期望将 `*` 作为 `Resource` 的值。  |  是  | 
|  `cloudshell:CreateSession`  | 从连接到 CloudShell 环境 AWS 管理控制台。 |  是  | 
|  `cloudshell:GetEnvironmentStatus`  |  读取 CloudShell 环境的状态。  |  是  | 
|  `cloudshell:DeleteEnvironment`  |  删除 CloudShell 环境。  |  否  | 
|   `cloudshell:GetFileDownloadUrls`  |  生成预签名 URLs 的 Amazon S3，用于 CloudShell 通过 CloudShell 网络界面下载文件。这不适用于 VPC 环境。  |  否  | 
|   `cloudshell:GetFileUploadUrls`  |  生成预签名 URLs 的 Amazon S3，用于 CloudShell 通过 CloudShell 网络界面上传文件。这不适用于 VPC 环境。  |  否  | 
| `cloudshell:DescribeEnvironments` | 描述环境。 | 否 | 
|  `cloudshell:PutCredentials`  |  将用于登录的凭据转发 AWS 管理控制台 到。 CloudShell  |  否  | 
|   `cloudshell:StartEnvironment`  |  启动已停止的 CloudShell 环境。  |  是  | 
|   `cloudshell:StopEnvironment`  |  停止正在运行的 CloudShell 环境。  |  否  | 
|  `cloudshell:ApproveCommand`  | 批准 CloudShell 从其他 AWS 服务控制台发送到的命令。 | 否 | 

### 适用于 IAM 策略的示例 CloudShell
<a name="policy-examples"></a>

以下示例说明如何创建策略来限制谁可以访问 CloudShell。这些示例还显示可在 Shell 环境中执行的操作。

以下政策强制完全拒绝访问 CloudShell 及其功能。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "DenyCloudShell",
        "Effect": "Deny",
        "Action": [
            "cloudshell:*"
        ],
        "Resource": "*"
    }]
}
```

------

以下策略允许 IAM 用户进行访问， CloudShell 但禁止他们生成 URLs 用于文件上传和下载的预签名。用户仍然可以将文件传入和传出环境，例如使用 `wget` 等客户端。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
        "Sid": "AllowUsingCloudshell",
        "Effect": "Allow",
        "Action": [
            "cloudshell:*"
        ],
        "Resource": "*"
    },
    {
        "Sid": "DenyUploadDownload",
        "Effect": "Deny",
        "Action": [
            "cloudshell:GetFileDownloadUrls",
            "cloudshell:GetFileUploadUrls"
        ],
        "Resource": "*"
    }]
}
```

------

以下策略允许 IAM 用户进行访问 CloudShell。但是，该策略禁止将您用于登录的凭据转发 AWS 管理控制台 到 CloudShell 环境。使用此策略的 IAM 用户需要在其中手动配置其证书 CloudShell。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
        "Sid": "AllowUsingCloudshell",
        "Effect": "Allow",
        "Action": [
            "cloudshell:*"
        ],
        "Resource": "*"
    },
    {
        "Sid": "DenyCredentialForwarding",
        "Effect": "Deny",
        "Action": [
            "cloudshell:PutCredentials"
        ],
        "Resource": "*"
    }]
}
```

------

以下策略允许 IAM 用户创建 AWS CloudShell 环境。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "CloudShellUser",
        "Effect": "Allow",
        "Action": [ 
            "cloudshell:CreateEnvironment",
            "cloudshell:CreateSession",
            "cloudshell:GetEnvironmentStatus",
            "cloudshell:StartEnvironment"
        ],
        "Resource": "*"
     }]
}
```

------

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

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

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

****  

```
{
  "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 环境
<a name="cloudshell-vpc-condition-keys"></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"></a>

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

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

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

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

#### 拒绝用户访问特定 VPCs、子网或安全组
<a name="permission-deny-access-example"></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"></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"
          ]
        }
      }
    }
  ]
}
```

------

## 访问权限 AWS 服务
<a name="aws-permissions"></a>

CloudShell 使用您用于登录的 IAM 证书 AWS 管理控制台。

**注意**  
 要使用您用于登录的 IAM 证书 AWS 管理控制台，您必须拥有`cloudshell:PutCredentials`权限。

的这种预身份验证功能 CloudShell 便于使用 AWS CLI。但是，IAM 用户仍然需要通过命令行调 AWS 服务 用的显式权限。

例如，假设 IAM 用户需要创建 Amazon S3 存储桶并将文件作为对象上传给这些存储桶。您可以创建明确允许这些操作的策略。IAM 控制台提供了一个交互式[可视化编辑器](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html#access_policies_create-start)，用于指导构建 JSON 格式的策略文档的过程。创建策略后，您可以将其附加到相关的 IAM 身份（用户、组或角色）。

有关如何将托管策略附加到您的实体的更多信息，请参阅* IAM 用户指南*中的[添加 IAM 身份权限（控制台）](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html#add-policies-console)。

## 访问中 Amazon Q CLI 功能的权限 CloudShell
<a name="q-cli-permissions-cloudshell"></a>

要在中使用 Amazon Q CLI 功能 CloudShell，例如内联建议、聊天和翻译，请确保您拥有所需的 IAM 权限。如果您无法访问中的 Amazon Q CLI 功能 CloudShell，请联系您的管理员为您提供必要的 IAM 权限。有关更多信息，请参阅《Amazon Q 开发者版用户指南》**中的[适用于 Amazon Q 开发者版的基于身份的策略示例](https://docs.aws.amazon.com/en_us/amazonq/latest/qdeveloper-ug/security_iam_id-based-policy-examples.html)。