

# 将 Amazon CloudShell 与 Amazon Identity and Access Management 结合使用
<a name="using-aws-with-cloudshell"></a>

Amazon CloudShell 是一个已经事先完成身份验证的浏览器式 Shell，您可以直接从启动它。Amazon Web Services 管理控制台您可以使用自己惯用的 Shell（Bash、PowerShell 或 Z shell），针对 Amazon 服务（包括 Amazon Identity and Access Management）运行 Amazon CLI 命令。您无需下载或安装命令行工具，即可完成此操作。

您可以 [从 Amazon Web Services 管理控制台 启动 Amazon CloudShell](https://docs.amazonaws.cn/cloudshell/latest/userguide/working-with-cloudshell.html#launch-options)，用于登录控制台的 Amazon 凭证将在新的 Shell 会话中自动可用。通过这种对 Amazon CloudShell 用户进行预身份验证，使您可在使用 Amazon CLI 版本 2（在 Shell 的计算环境中预装）与 IAM 等 Amazon 服务进行交互时跳过凭证配置步骤。

## 获取 Amazon CloudShell 的 IAM 权限
<a name="cloudshell-permissions"></a>

使用 Amazon Identity and Access Management 提供的访问管理资源，管理员可以向 IAM 用户授予权限，使其能够访问 Amazon CloudShell 并使用环境的功能。

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

如果要限制 IAM 用户可以使用 Amazon CloudShell 执行的操作范围，则可以 `AWSCloudShellFullAccess` 托管式策略为模板创建使用的定义策略。要详细了解如何限制用户可在 CloudShell 中使用的操作，请参阅《Amazon CloudShell 用户指南》中的 [Managing Amazon CloudShell access and usage with IAM policies](https://docs.amazonaws.cn/cloudshell/latest/userguide/sec-auth-with-identities.html)**。

## 与 IAM 交互
<a name="cshell-examples"></a>

从 Amazon Web Services 管理控制台 启动 Amazon CloudShell 后，您可以立即开始使用命令行界面与 IAM 进行交互。

**注意**  
在 Amazon CloudShell 中使用 Amazon CLI 时，无需下载或安装任何其他资源。此外，由于已经在 Shell 中进行了身份验证，因此在进行调用之前无需配置凭证。

## 使用 Amazon CloudShell 创建 IAM 组并将 IAM 用户添加到该组


以下示例使用 CloudShell 创建了一个 IAM 组，向该组添加了一个 IAM 用户，然后验证命令是否成功。

1. 在 Amazon Web Services 管理控制台 中，您可以选择导航栏中提供的下列可用选项来启动 CloudShell：
   + 选择 CloudShell 图标。
   + 首先在搜索框中键入“cloudshell”，然后选择 CloudShell 选项。

1. 要创建 IAM 组，请在 CloudShell 命令行中输入以下命令。在此例中，我们将该组命名为 *east\_coast*：

   ```
   aws iam create-group --group-name east_coast
   ```

   如果调用成功，命令行将显示来自服务的响应，输出与以下类似：

   ```
           {
              "Group": {
                  "Path": "/",
                  "GroupName": "east_coast",
                  "GroupId": "AGPAYBDBW4JBY3EXAMPLE",
                  "Arn": "arn:aws:iam::111122223333:group/east_coast",
                  "CreateDate": "2023-09-11T21:02:21+00:00"
               }
           }
   ```

1. 要将用户添加到您创建的组，请使用以下命令，并指定组名和用户名。在此例中，我们将该组命名为 *east\_coast*，将用户命名为 *john*：

   ```
   aws iam add-user-to-group --group-name east_coast --user-name john
   ```

1. 要验证用户是否在该组中，请使用以下命令并指定组名。在此例中，我们继续使用组 *east\_coast*：

   ```
   aws iam get-group --group-name east_coast
   ```

   如果调用成功，命令行将显示来自服务的响应，输出与以下类似：

   ```
          {
            "Users": [
              {
                  "Path": "/",
                  "UserName": "john",
                  "UserId": "AIDAYBDBW4JBXGEXAMPLE",
                  "Arn": "arn:aws:iam::552108220995:user/john",
                  "CreateDate": "2023-09-11T20:43:14+00:00",
                  "PasswordLastUsed": "2023-09-11T20:59:14+00:00"
              }
            ],
            "Group": {
                  "Path": "/",
                  "GroupName": "east_coast",
                  "GroupId": "AGPAYBDBW4JBY3EXAMPLE",
                  "Arn": "arn:aws:iam::111122223333:group/east_coast",
                  "CreateDate": "2023-09-11T21:02:21+00:00"
               }
           }
   ```