

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

# 使用 AWS CloudShell 存取 Amazon Keyspaces
<a name="using-aws-with-cloudshell"></a>

AWS CloudShell 是以瀏覽器為基礎的預先驗證 Shell，您可以直接從 啟動 AWS 管理主控台。您可以使用您偏好的 shell (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`或 第 2 AWS CLI 版 （預先安裝在 shell 的運算環境） 與 Amazon Keyspaces 等 AWS 服務互動時，略過設定登入資料。

## AWS CloudShell 整合選項
<a name="cloudshell-integration-options"></a>

Amazon Keyspaces 提供兩種使用方式 AWS CloudShell：

1. **整合 AWS CloudShell 體驗** - 直接從 Amazon Keyspaces 主控台提供的簡化連線方法，可自動初始化 `cqlsh-expansion`並連接至特定的金鑰空間。如需使用此整合體驗的完整說明，請參閱 [AWS CloudShell 從主控台使用 連線至 Amazon Keyspaces](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：//[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建立並管理的獨立政策。下列 CloudShell 受 AWS 管政策可以連接到 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，您需要下載所需的 Amazon 數位憑證。如需下載和設定 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)。

## 搭配 Amazon Keyspaces 使用 CQLSH
<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) 以取得詳細步驟。

**注意**  
在 `cqlsh-expansion` 中使用 時 AWS CloudShell，您不需要在呼叫之前設定登入資料，因為您已在 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)
   ```