

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

# 使用 Amazon Bedrock 自動化 AWS 基礎設施操作
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock"></a>

*Ishwar Chauthaiwale 和 Anand Bukkapatnam Tirumala，Amazon Web Services*

## 摘要
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-summary"></a>

在雲端原生解決方案中，自動化常見的基礎設施操作在維護有效率、安全且符合成本效益的環境方面扮演重要角色。手動處理操作耗時且容易發生人為錯誤。此外，具有不同 AWS 專業知識層級的團隊成員需要執行這些任務，同時確保遵守安全通訊協定。此模式示範如何使用 Amazon Bedrock 透過自然語言處理 (NLP) 自動化常見的 AWS 基礎設施操作。

此模式可協助組織開發可重複使用、模組化且安全的程式碼，以便在多個環境中部署生成式 AI 型基礎設施。透過專注於基礎設施即程式碼 (IaC) 和自動化，它提供了重要的 DevOps 優勢，包括版本控制、一致的部署、減少錯誤、更快速的佈建和改善的協同合作。

模式實作安全架構，讓團隊能夠管理與金鑰相關的操作， AWS 服務 包括：
+ Amazon Simple Storage Service (Amazon S3) 儲存貯體版本控制管理
+ 建立 Amazon Relational Database Service (Amazon RDS) 快照
+ Amazon Elastic Compute Cloud (Amazon EC2) 執行個體管理

架構採用 Amazon Virtual Private Cloud (Amazon VPC) 端點和私有聯網進行安全通訊，其 AWS Lambda 函數在私有子網路中做為任務執行器運作。Amazon S3 提供資料管理並實作全方位 AWS Identity and Access Management (IAM) 角色和許可，以確保適當的存取控制。此解決方案不包含聊天歷史記錄功能，也不會儲存聊天。

## 先決條件和限制
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-prereqs"></a>
+ 作用中 AWS 帳戶。
+ 應採取適當的存取控制措施，以協助保護和控制存取。存取控制的範例包括使用 AWS Systems Manager、基礎模型存取、用於部署的 IAM 角色，以及以服務為基礎的角色、停用對 Amazon S3 儲存貯體的公開存取，以及設定無效字母佇列。
+  AWS Key Management Service (AWS KMS) [客戶受管金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk)。
+ AWS Command Line Interface (AWS CLI) 第 2 版或更新版本，已在部署環境中[安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)和[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。
+ [已安裝](https://registry.terraform.io/providers/-/aws/latest/docs/guides/version-4-upgrade)並設定 Terraform AWS Provider 第 4 版或更新版本。
+ 安裝[https://developer.hashicorp.com/terraform/install](https://developer.hashicorp.com/terraform/install)並設定 Terraform 1.5.7 版或更新版本。
+ 在 [Amazon Bedrock 中檢閱和定義代理程式動作群組的 OpenAPI 結構描述](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-api-schema.html)，以協助防止未經授權的存取和維護資料完整性。
+ [在 中針對所需的 Amazon Titan Text Embeddings v2 和 Claude 3.5 Sonnet 或 Claude 3 Haiku 基礎模型啟用](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html)存取。 AWS 帳戶 [https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html)為了避免部署失敗，請確認您的目標部署 AWS 區域 [支援所需的模型](https://docs.aws.amazon.com/bedrock/latest/userguide/models-regions.html)。
+ 設定虛擬私有雲端 (VPC)，遵循 [AWS Well Architected Framework](https://docs.aws.amazon.com/wellarchitected/latest/framework/sec-design.html) 最佳實務。
+ 已完成 [Amazon Responsible AI 政策](https://aws.amazon.com/ai/responsible-ai/policy/)的檢閱。

**產品版本**
+ Amazon Titan Text Embeddings v2
+ Anthropic Claude 3.5 Sonnet 或 Claude 3 Haiku
+ Terraform AWS Provider 第 4 版或更新版本
+ Terraform 1.5.7 版或更新版本

## Architecture
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-architecture"></a>

下圖顯示此模式的工作流程和架構元件。

![\[使用 Amazon Bedrock 自動化常見 AWS 基礎設施操作的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/924e503f-bfc5-4452-abdf-d72a58d4d36f/images/bd56ad29-b435-4543-8ee8-dc4e1d38df18.png)


解決方案架構包含多個層，可共同處理自然語言請求並執行對應的 AWS 操作：

1. 使用者透過 Amazon Bedrock 聊天主控台提出操作請求。

1. 聊天機器人使用 Amazon Bedrock 知識庫來處理請求。它實作 Amazon Titan Text Embeddings v2 模型進行自然語言處理。

1. 如果使用者提示包含動作請求，Amazon Bedrock 動作群組會使用 Anthropic Claude 3 Haiku 或 Claude 3.5 Sonnet 模型 （取決於您的選擇） 執行邏輯，並透過 OpenAPI 結構描述定義操作。

1. 動作群組使用 到達 Amazon VPC [端點](https://docs.aws.amazon.com/whitepapers/latest/aws-privatelink/what-are-vpc-endpoints.html)， AWS PrivateLink 以安全進行服務通訊。

1. 透過 Amazon Bedrock 服務的 Amazon VPC 端點達到 AWS Lambda 函數。

1. Lambda 函數是主要執行引擎。根據請求，Lambda 函數會呼叫 API 以在 上執行動作 AWS 服務。Lambda 函數也會處理操作路由和執行。

1. 從 Lambda 函數 AWS 服務 取得 API 請求並執行對應的操作。

1. Lambda 函數會計算 Amazon Bedrock 理解的輸出承載。

1. 此承載會透過使用 PrivateLink 進行安全的服務通訊，傳送至 Amazon Bedrock。Amazon Bedrock 使用的大型語言模型 (LLM) 了解此承載，並將其轉換為人類可理解的格式。

1. 然後，輸出會在 Amazon Bedrock 聊天主控台上向使用者顯示。

解決方案會啟用下列主要操作：
+ Amazon S3 – 啟用儲存貯體版本控制以進行版本控制。
+ Amazon RDS – 建立資料庫快照以進行備份。
+ Amazon EC2 – 列出執行個體並控制執行個體的啟動和停止。

## 工具
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-tools"></a>

**AWS 服務**
+ [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) 是一項全受管服務，可讓您透過統一 API 使用來自領導 AI 新創公司的高效能基礎模型 (FMs) 和 Amazon。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您 AWS 服務 透過命令列 shell 中的命令與 互動。
+ [Amazon Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，，並快速進行擴展或縮減。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon OpenSearch Serverless](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-overview.html) 是 Amazon OpenSearch Service 的隨需無伺服器組態。
+ [AWS PrivateLink](https://docs.aws.amazon.com/vpc/latest/privatelink/what-is-privatelink.html) 可協助您建立從虛擬私有雲端 (VPCs) 到 VPC 外部服務的單向私有連線。
+ [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html) 可協助您在 中設定、操作和擴展關聯式資料庫 AWS 雲端。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 可協助您管理在 中執行的應用程式和基礎設施 AWS 雲端。它可簡化應用程式和資源管理、縮短偵測和解決操作問題的時間，並協助您大規模安全地管理 AWS 資源。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。

**其他工具**
+ [Git](https://git-scm.com/docs) 是開放原始碼的分散式版本控制系統。
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的基礎設施即程式碼 (IaC) 工具，可協助您建立和管理雲端和內部部署資源。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [aws-samples/infra-ops-orchestrator](https://github.com/aws-samples/infra-ops-orchestrator) 儲存庫中使用。

## 最佳實務
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-best-practices"></a>
+ 定期監控 Lambda 執行日誌。如需詳細資訊，請參閱[監控和疑難排解 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/lambda-monitoring.html)。如需最佳實務的詳細資訊，請參閱[使用 AWS Lambda 函數的最佳實務](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)。
+ 定期檢閱安全組態，以確保符合組織的需求。如需詳細資訊，請參閱[安全最佳實務](https://docs.aws.amazon.com/wellarchitected/latest/framework/sec-bp.html)。
+ 遵循最低權限原則，並授予執行任務所需的最低許可。如需詳細資訊，請參閱 IAM 文件中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 史詩
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-epics"></a>

### 部署解決方案
<a name="deploy-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製儲存庫。 | 若要在本機電腦上複製儲存庫，請執行下列命令：<pre>git clone "git@github.com:aws-samples/infra-ops-orchestrator.git"<br />cd infra-ops-orchestrator</pre> | AWS DevOps，DevOps 工程師 | 
| 編輯環境變數。 | 在複製儲存庫的根目錄中編輯 `terraform.tfvars` 檔案。檢閱 指示的預留位置`[XXXXX]`，並根據您的環境進行更新。 | AWS DevOps，DevOps 工程師 | 
| 建立基礎設施。 | 若要建立基礎設施，請執行下列命令：<pre>terraform init</pre><pre>terraform plan</pre>請仔細檢閱執行計畫。如果計劃的變更是可接受的，請執行下列命令：<pre>terraform apply --auto-approve</pre> | AWS DevOps，DevOps 工程師 | 

### 存取解決方案
<a name="access-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 存取解決方案。 | 成功部署後，請依照下列步驟使用聊天型界面：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-aws-infrastructure-operations-by-using-amazon-bedrock.html) | AWS DevOps，DevOps 工程師 | 

### 清除資源
<a name="clean-up-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除建立的資源。 | 若要刪除此模式建立的所有基礎設施，請執行下列命令：<pre>terraform plan -destroy </pre>請仔細檢閱銷毀計畫。如果計劃刪除是可接受的，請執行下列命令：<pre>terraform destroy</pre>注意：此命令將永久刪除此模式建立的所有資源。命令會在移除任何資源之前提示確認。 | AWS DevOps，DevOps 工程師 | 

## 疑難排解
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 代理程式行為  | 如需此問題的相關資訊，請參閱 Amazon Bedrock 文件中的[測試和疑難排解代理程式行為](https://docs.aws.amazon.com/lambda/latest/dg/troubleshooting-networking.html)。 | 
| Lambda 網路問題 | 如需有關這些問題的資訊，請參閱 [Lambda 文件中的對 Lambda 中的聯網問題進行故障診斷](https://docs.aws.amazon.com/lambda/latest/dg/troubleshooting-networking.html)。 | 
| IAM 許可 | 如需這些問題的相關資訊，請參閱 [IAM 文件中的疑難排解](https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot.html) IAM。 | 

## 相關資源
<a name="automate-aws-infrastructure-operations-by-using-amazon-bedrock-resources"></a>
+ [為 Amazon RDS 的單一可用區域資料庫執行個體建立資料庫快照](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_CreateSnapshot.html)
+ [在 Amazon Bedrock 中為代理程式的動作群組定義 OpenAPI 結構描述](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-api-schema.html)
+ [在儲存貯體上啟用版本控制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/manage-versioning-examples.html)
+ [Amazon Bedrock 代理程式的運作方式](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-how.html)
+ [使用 Amazon Bedrock 知識庫擷取資料並產生 AI 回應](https://docs.aws.amazon.com/whitepapers/latest/aws-privatelink/aws-privatelink.html)
+ [透過 安全地存取服務 AWS PrivateLink](https://docs.aws.amazon.com/whitepapers/latest/aws-privatelink/aws-privatelink.html)
+ [停止和啟動 Amazon EC2 執行個體](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html)
+ [使用動作群組來定義代理程式要執行的動作](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html)