

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

# AWS CloudShell 用于访问 Amazon Keyspaces
<a name="using-aws-with-cloudshell"></a>

AWS CloudShell 是一个基于浏览器、经过预先验证的 shell，您可以直接从启动。 AWS 管理控制台您可以使用首选外壳（Bash PowerShell 或 Z shell）对 AWS 服务运行 AWS CLI 命令。 AWS CloudShell 已使用您的控制台凭据进行预先身份验证，并包含用于处理 AWS 服务的常用工具，包括 AWS CLI、Python 和 Node.js。

您可以[AWS CloudShell 从启动 AWS 管理控制台，](https://docs.aws.amazon.com/cloudshell/latest/userguide/working-with-cloudshell.html#launch-options)用于登录控制台的 AWS 凭据将在新的 shell 会话中自动可用。这种对 AWS CloudShell 用户的预身份验证允许您在`cqlsh`使用 AWS CLI 或版本 2（预安装在外壳的计算环境中）与 Amazon Keyspaces 等 AWS 服务进行交互时跳过配置凭证。

## AWS CloudShell 集成选项
<a name="cloudshell-integration-options"></a>

Amazon Keyspaces 提供两种使用方式： AWS CloudShell

1. **集成 AWS CloudShell 体验**-一种简化的连接方法，可直接从 Amazon Keyspaces 控制台中获得，可自动初始化`cqlsh-expansion`并连接到特定的密钥空间。有关使用此集成体验的完整说明，请参阅[使用控制台连接到 Amazon Keysp AWS CloudShell aces](console_cloudshell_integration.md)。

1. **手动 AWS CloudShell 设置**-`cqlsh` 在中手动安装和配置 AWS CloudShell。以下各节将介绍这些步骤，这些步骤提供了对设置过程的更多控制并启用自定义配置。

## 先决条件
<a name="cloudshell-prerequisites"></a>

在将 Amazon Keyspaces 与配合使用之前 AWS CloudShell，请确保您已具备以下条件：
+ 有权访问 Amazon Keyspaces 的 AWS 账户和 AWS CloudShell
+ 使用这两项服务的相应的 IAM 权限
+ 基本熟悉 Cassandra 查询语言 (CQL)

## 入门 AWS CloudShell
<a name="getting-started-cloudshell"></a>

要访问 AWS CloudShell：

1. 登录 AWS 管理控制台

1.  AWS CloudShell 通过选择控制台顶部导航栏中的 AWS CloudShell 图标或导航到 [https://console.aws.amazon.com/cloudshell/](https://console.aws.amazon.com/cloudshell/)

1. 等待 AWS CloudShell 环境初始化

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

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

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

如果您想限制 IAM 用户可以执行的操作范围 AWS CloudShell，则可以创建使用`AWSCloudShellFullAccess`托管策略作为模板的自定义策略。有关限制中可供用户执行的操作的更多信息 CloudShell，请参阅*AWS CloudShell 用户指南*中的[使用 IAM 策略管理 AWS CloudShell 访问和使用情况](https://docs.aws.amazon.com/cloudshell/latest/userguide/sec-auth-with-identities.html)。

**注意**  
您的 IAM 身份还需要一项策略，来授予调用 Amazon Keyspaces 的权限。

您可以使用 AWS 托管策略向您的 Amazon Keyspaces 授予您的 IAM 身份访问权限，也可以从托管策略作为模板开始删除不需要的权限。您还可以限制访问特定键空间和表来创建自定义策略。以下适用于 Amazon Keyspaces 的托管式策略可以附加到 IAM 身份：
+ [AmazonKeyspacesFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonKeyspacesFullAccess.html)— 本政策允许用户使用 Amazon Keyspaces，并拥有对所有功能的完全访问权限。

有关托管式策略中定义的操作的详细说明，请参阅[AWS Amazon Keyspaces 的托管策略](security-iam-awsmanpol.md)。

 有关如何限制操作或者限制访问 Amazon Keyspaces 中特定资源的信息，请参阅[Amazon Keyspaces 如何与 IAM 配合使用](security_iam_service-with-iam.md)。

## 安装 cqlsh 客户端
<a name="installing-cqlsh-client"></a>

AWS CloudShell 不附带`cqlsh`预装。您需要安装它才能与 Amazon Keyspaces 进行交互。您可以安装特定于 Amazon KeySpaces 的版本。`cqlsh`有关 `cqlsh-expansion` 安装说明，请参阅[使用 `cqlsh-expansion` 连接 Amazon Keyspaces](programmatic.cqlsh.md#using_cqlsh)。

## 配置 TLS 证书
<a name="configuring-ssl-certificates"></a>

要使用 TLS 连接连接到 Amazon Keyspaces，您需要下载所需的亚马逊数字证书。有关下载和配置 SSL 证书的详细说明，请参阅[如何为 TLS 手动配置 `cqlsh` 连接](programmatic.cqlsh.md#encrypt_using_tls)。

## 连接到 Amazon Keyspaces
<a name="connecting-to-keyspaces"></a>

您可以使用 IAM 证书（推荐）或特定于服务的凭证连接到 Amazon Keyspaces。

### 使用 IAM 证书连接（推荐）
<a name="connecting-iam-credentials"></a>

AWS CloudShell 已使用您的 IAM 证书进行身份验证。要使用这些凭据进行连接，可以使用 Sigv4 身份验证插件。有关将 IAM 证书与一起使用的详细说明`cqlsh`，请参阅[使用 `cqlsh-expansion` 连接 Amazon Keyspaces](programmatic.cqlsh.md#using_cqlsh)。

### 使用服务特定凭证进行连接
<a name="connecting-service-specific-credentials"></a>

如果您已为 Amazon Keyspaces 创建了特定于服务的凭证，则可以使用它们进行连接。有关创建和使用特定于服务的凭证的信息，请参阅[创建用于通过编程方式访问 Amazon Keyspaces 的服务特定凭证。](programmatic.credentials.ssc.md)。

## 将 CQLSH 与 Amazon Keyspaces 配合使用
<a name="using-cqlsh-keyspaces"></a>

连接后，您可以使用标准 CQL 命令与密钥空间和表进行交互。有关支持的 CQL 命令和操作的详细信息，请参阅[使用 `cqlsh-expansion` 连接 Amazon Keyspaces](programmatic.cqlsh.md#using_cqlsh)。

## 将数据保存在中 AWS CloudShell
<a name="persisting-data-cloudshell"></a>

AWS CloudShell 在您的主目录中提供 1 GB 的永久存储空间。这意味着您创建的脚本、证书和配置文件在各个会话中仍然可用。您可以为创建配置文件`cqlsh`以简化连接。

使用配置文件，`cqlsh`无需指定其他参数即可运行。有关创建和使用`cqlsh`配置文件的更多信息，请参阅[使用 `cqlsh-expansion` 连接 Amazon Keyspaces](programmatic.cqlsh.md#using_cqlsh)。

## 最佳实践
<a name="best-practices-cloudshell"></a>

将 Amazon Keyspaces 与配合使用时 AWS CloudShell，我们建议采用以下最佳实践：
+ 为重复性任务创建脚本并将其存储在您的永久主目录中。
+ 使用环境变量来存储敏感信息，而不是对其进行硬编码。
+ 请记住， AWS CloudShell 会话在处于非活动状态后可能会超时。
+ 请注意 AWS CloudShell 服务配额和限制。
+ 考虑使用对 Amazon Keyspaces 进行自动操作。 AWS CLI 

## 问题排查
<a name="troubleshooting-cloudshell"></a>

如果您在通过以下方式连接到 Amazon Keyspaces 时遇到问题： AWS CloudShell
+ 验证您的 IAM 权限包括必要的 Amazon Keyspaces 操作。
+ 确保您使用的是适用于您所在地区的正确终端节点。
+ 检查 SSL 证书是否正确下载和引用。
+ 验证您的服务专用凭证是否正确且未过期。
+ 如果使用的是 cqlshrc 文件，请检查其格式和权限。

## 示例：如何使用与 Amazon Keyspaces 进行交互 AWS CloudShell
<a name="cshell-examples"></a>

 AWS CloudShell 从启动后 AWS 管理控制台，您可以使用`cqlsh`或命令行界面立即开始与 Amazon Keyspaces 进行交互。如果您尚未安装 `cqlsh-expansion`，请参阅[使用 `cqlsh-expansion` 连接 Amazon Keyspaces](programmatic.cqlsh.md#using_cqlsh) 来了解详细步骤。

**注意**  
使用 in 时 AWS CloudShell，您无需`cqlsh-expansion`在调用之前配置凭据，因为您已经在 shell 中进行了身份验证。

**连接到 Amazon Keyspaces 并创建新的键空间。然后从系统表中读取以确认密钥空间是使用创建的 AWS CloudShell**

1. 从中 AWS 管理控制台，您可以 CloudShell 通过选择导航栏上的以下可用选项来启动：
   +  选择图 CloudShell 标。
   + 开始`cloudshell`在搜索框中键入，然后选择 CloudShell选项。

1. 您可以使用以下命令建立与 Amazon Keyspaces 的连接。请务必`cassandra.us-east-1.amazonaws.com`替换为您所在地区的正确终端节点。

   ```
   cqlsh-expansion cassandra.us-east-1.amazonaws.com 9142 --ssl
   ```

   如果连接成功，您应该会看到类似于以下内容的输出：

   ```
   Connected to Amazon Keyspaces at cassandra.us-east-1.amazonaws.com:9142
   [cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
   Use HELP for help.
   cqlsh current consistency level is ONE.
   cqlsh>
   ```

1. 使用名称 `mykeyspace` 创建新的键空间。您可以使用以下命令来执行此操作。

   ```
   CREATE KEYSPACE mykeyspace WITH REPLICATION = {'class': 'SingleRegionStrategy'};
   ```

1. 要确认键空间已创建，您可以使用以下命令从系统表中读取。

   ```
   SELECT * FROM system_schema_mcs.keyspaces WHERE keyspace_name = 'mykeyspace';
   ```

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

   ```
    keyspace_name  | durable_writes | replication
   ----------------+----------------+-------------------------------------------------------------------------------------
    mykeyspace     |           True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'}
   
   (1 rows)
   ```