

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

# 使用 在 Amazon Bedrock 中設定模型調用記錄 AWS CloudFormation
<a name="configure-bedrock-invocation-logging-cloudformation"></a>

*Vikramaditya Bhatnagar，Amazon Web Services*

## 總結
<a name="configure-bedrock-invocation-logging-cloudformation-summary"></a>

您可以設定 Amazon Bedrock 為 中的所有模型調用收集調用日誌、模型輸入資料和模型輸出資料 AWS 帳戶。這是使用 Amazon Bedrock 建置強大生成式 AI 應用程式的[最佳實務](https://aws.amazon.com/blogs/machine-learning/best-practices-for-building-robust-generative-ai-applications-with-amazon-bedrock-agents-part-2/)。您可以將模型調用日誌存放在 Amazon CloudWatch Logs 日誌群組、Amazon Simple Storage Service (Amazon S3) 儲存貯體或兩者中。在 CloudWatch Logs 中擁有日誌資料可協助您建立自訂指標篩選條件、警示和儀表板。Amazon S3 非常適合用於跨資料複寫， AWS 區域 或用於長期儲存，這取決於您組織的政策。

此模式提供範例 AWS CloudFormation 範本，使用基礎設施做為程式碼 (IaC) 方法來設定 Amazon Bedrock 的模型調用記錄。範本會在 CloudWatch Logs 和 Amazon S3 中設定日誌儲存。

## 先決條件和限制
<a name="configure-bedrock-invocation-logging-cloudformation-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 下列許可：
  + 建立 CloudFormation 堆疊的[許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html) 
  + 存取 Amazon Bedrock [的許可](https://docs.aws.amazon.com/bedrock/latest/userguide/security-iam-awsmanpol.html#security-iam-awsmanpol-updates) 
  + 建立和存取 Amazon S3 儲存貯體的[許可](https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-policies-s3.html) 
  + 建立和存取 CloudWatch Logs 日誌群組的[許可](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/iam-identity-based-access-control-cwl.html) 
  + 建立和存取 AWS Lambda 函數[的許可](https://docs.aws.amazon.com/lambda/latest/dg/security-iam-awsmanpol.html) 
  + 建立和存取 AWS Key Management Service (AWS KMS) 金鑰[的許可](https://docs.aws.amazon.com/kms/latest/developerguide/customer-managed-policies.html) 

**限制**

此模式會將模型調用記錄到 CloudWatch Logs 和 Amazon S3。它不支援僅選擇這兩個服務之一。

## Architecture
<a name="configure-bedrock-invocation-logging-cloudformation-architecture"></a>

**目標架構**

CloudFormation 範本會在您的目標中佈建下列資源 AWS 帳戶：
+ 用於儲存模型調用日誌的 CloudWatch Logs 日誌群組
+ 用於儲存模型調用日誌的 Amazon S3 儲存貯體和對應的儲存貯體政策
+ Amazon S3 儲存貯體，用於存放伺服器端存取日誌和對應的儲存貯體政策
+ 在 Amazon Bedrock 中設定記錄設定的 AWS Lambda 函數
+  AWS KMS key 和對應的金鑰別名
+ Amazon Bedrock 的 AWS Identity and Access Management (IAM) 服務角色

下圖顯示在您部署與此模式相關聯的 CloudFormation 堆疊後，如何存放叫用日誌。當基礎模型交付文字、影像、影片或內嵌資料時，Amazon Bedrock 會發佈日誌資料。如圖所示，Amazon S3 儲存貯體和 CloudWatch Logs 日誌群組會使用 加密 AWS KMS key。

![記錄 Amazon Bedrock 基礎模型調用的工作流程。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/a55e7495-ec84-4d41-886e-5c37b37aac67/images/a958d52f-9072-40af-80cb-360f6c1c7fd5.png)


該圖顯示以下工作流程：

1. 使用者向 Amazon Bedrock 中的基礎模型提交查詢。

1. Amazon Bedrock 擔任 IAM 服務角色。

1. Amazon Bedrock 會產生日誌資料，並將其存放在 CloudWatch Logs 日誌群組和 Amazon S3 儲存貯體中。

1. 如果使用者讀取、上傳或刪除 Amazon S3 儲存貯體中包含模型叫用日誌的任何檔案，這些活動會記錄在另一個 Amazon S3 儲存貯體中做為伺服器端存取日誌。

**自動化和擴展**

若要擴展此解決方案，您可以將 CloudFormation 範本部署為堆疊設定為多個 AWS 區域 和 AWS 帳戶。如需詳細資訊，請參閱 CloudFormation 文件中的[使用 StackSets 管理跨帳戶和區域的堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)。

## 工具
<a name="configure-bedrock-invocation-logging-cloudformation-tools"></a>

**AWS 服務**
+ [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) 是一項全受管服務，可讓您透過統一 API 使用來自領導 AI 公司和 Amazon 的高效能基礎模型 (FMs)。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。
+ [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.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) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可協助您建立和控制密碼編譯金鑰，以協助保護您的資料。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種物件儲存服務，可提供業界領先的可擴展性、資料可用性、安全性和效能。

**其他工具**
+ [Git](https://git-scm.com/docs) 是一種開放原始碼的分散式版本控制系統。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [enable-bedrock-logging-using-cloudformation](https://github.com/aws-samples/enable-bedrock-logging-using-cloudformation) 儲存庫中使用。

## 史詩
<a name="configure-bedrock-invocation-logging-cloudformation-epics"></a>

### 建立 CloudFormation 堆疊
<a name="create-the-cfnshort-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載 CloudFormation 範本。 | 從 GitHub 儲存庫下載 [CloudFormation 範本](https://github.com/aws-samples/enable-bedrock-logging-using-cloudformation/blob/main/enable-bedrock-logging-using-cloudformation.yaml)。 | 雲端架構師 | 
| 部署 範本。 | 在目標帳戶和區域中建立堆疊。在**參數**區段中，指定範本中定義的參數值。如需說明，請參閱 CloudFormation 文件中的[建立堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)。 | 雲端架構師 | 

### 測試解決方案
<a name="test-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟用模型存取。 | 在 Amazon Bedrock 中，新增基礎模型的存取權。如需說明，請參閱 [Amazon Bedrock 文件中的新增或移除對 Amazon Bedrock 基礎模型的存取權](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html)。 | 雲端架構師 | 
| 執行範例提示。 | 在 Amazon Bedrock 遊樂場中，執行範例提示。如需說明，請參閱[《Amazon Bedrock 文件》中的使用遊樂場在主控台中產生回應](https://docs.aws.amazon.com/bedrock/latest/userguide/playgrounds.html)。 | 雲端架構師 | 
| 檢閱記錄組態。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-bedrock-invocation-logging-cloudformation.html) | 雲端架構師 | 
| 檢閱 Amazon S3 儲存貯體。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-bedrock-invocation-logging-cloudformation.html) | 雲端架構師 | 
| 檢閱日誌群組。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/configure-bedrock-invocation-logging-cloudformation.html) | 雲端架構師 | 

## 相關資源
<a name="configure-bedrock-invocation-logging-cloudformation-resources"></a>

**AWS 文件**
+ [存取 Amazon S3 儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html)體 (Amazon S3 文件）
+ [建立和管理堆疊 ](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacks.html)(CloudFormation 文件）
+ [監控模型調用](https://docs.aws.amazon.com/bedrock/latest/userguide/model-invocation-logging.html) (Amazon Bedrock 文件）
+ [使用日誌群組和日誌串流 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html)(CloudWatch Logs 文件）

**AWS 部落格文章**
+ [使用 Amazon Bedrock 和 Amazon CloudWatch 整合監控生成式 AI 應用程式](https://aws.amazon.com/blogs/mt/monitoring-generative-ai-applications-using-amazon-bedrock-and-amazon-cloudwatch-integration/)
+ [使用 Amazon Bedrock Agents 建置強大生成式 AI 應用程式的最佳實務 – 第 1 部分](https://aws.amazon.com/blogs/machine-learning/best-practices-for-building-robust-generative-ai-applications-with-amazon-bedrock-agents-part-1/)
+ [使用 Amazon Bedrock Agents 建置強大生成式 AI 應用程式的最佳實務 – 第 2 部分](https://aws.amazon.com/blogs/machine-learning/best-practices-for-building-robust-generative-ai-applications-with-amazon-bedrock-agents-part-2/)