

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

# AWS Step Functions 使用 Amazon Bedrock 對 中的狀態進行故障診斷
<a name="troubleshooting-states-in-aws-step-functions"></a>

*Aniket Kurzadkar 和 Sangam Kushwaha，Amazon Web Services*

## 摘要
<a name="troubleshooting-states-in-aws-step-functions-summary"></a>

AWS Step Functions 錯誤處理功能可協助您看到[工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-statemachines.html)中狀態期間發生的錯誤，但仍可能難以找出錯誤的根本原因並進行偵錯。此模式可解決挑戰，並顯示 Amazon Bedrock 如何協助您解決 Step Functions 中狀態期間發生的錯誤。

Step Functions 提供工作流程協同運作，讓開發人員更輕鬆地自動化程序。Step Functions 也提供錯誤處理功能，提供下列優點：
+ 開發人員可以建立更具彈性的應用程式，在發生錯誤時不會完全失敗。
+ 工作流程可以包含條件式邏輯，以不同方式處理不同類型的錯誤。
+ 系統可以自動重試失敗的操作，可能具有指數退避。
+ 您可以針對錯誤案例定義替代執行路徑，讓工作流程調整並繼續處理。

當 Step Functions 工作流程發生錯誤時，此模式會顯示錯誤訊息和內容如何傳送至基礎模型 (FM)，例如 Step Functions 支援的 Claude 3。FM 可以分析錯誤、分類錯誤，並建議潛在的修補步驟。

## 先決條件和限制
<a name="troubleshooting-states-in-aws-step-functions-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 對 [AWS Step Functions 和 工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-statemachines.html)的基本了解
+ Amazon Bedrock [API 連線](https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api.html)

**限制**
+ 您可以針對各種 使用此模式的方法 AWS 服務。不過，結果可能會根據後續由 Amazon Bedrock 評估而建立 AWS Lambda 的提示而有所不同。
+ 有些 AWS 服務 無法全部使用 AWS 區域。如需區域可用性，請參閱[依區域的 AWS 服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

## Architecture
<a name="troubleshooting-states-in-aws-step-functions-architecture"></a>

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

![\[使用 Step Functions、Amazon Bedrock 和 Amazon SNS 處理錯誤和通知的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/78f86c74-c9de-4562-adcc-105b87a77a54/images/d8eda499-ea1d-45e5-8a36-e04a44ad5c4b.png)


圖表顯示 Step Functions 狀態機器中錯誤處理和通知的自動化工作流程：

1. 開發人員會啟動狀態機器的執行。

1. Step Functions 狀態機器會開始處理其狀態。有兩種可能的結果：
   + (a) 如果所有狀態都成功執行，工作流程會直接前往 Amazon SNS 傳送電子郵件成功通知。
   + (b) 如果任何狀態失敗，工作流程會移至錯誤處理 Lambda 函數。

1. 如果發生錯誤，會發生下列情況：
   + (a) 觸發 Lambda 函數 （錯誤處理常式）。Lambda 函數會從 Step Functions 狀態機器傳遞的事件資料中擷取錯誤訊息。然後，Lambda 函數會根據此錯誤訊息準備提示，並將提示傳送至 Amazon Bedrock。提示請求與遇到的特定錯誤相關的解決方案和建議。
   + (b) 託管生成式 AI 模型的 Amazon Bedrock 會處理輸入提示。（此模式使用 Anthropic Claude 3 基礎模型 (FM)，這是 Amazon Bedrock 支援的許多 FMs之一。) AI 模型會分析錯誤內容。然後，模型會產生回應，其中包含錯誤發生原因的說明、解決錯誤的潛在解決方案，以及避免未來發生相同錯誤的建議。

     Amazon Bedrock 會將其 AI 產生的回應傳回 Lambda 函數。Lambda 函數會處理回應，可能將其格式化或擷取金鑰資訊。然後，Lambda 函數會將回應傳送至狀態機器輸出。

1. 在錯誤處理或成功執行後，工作流程會透過觸發 Amazon SNS 傳送電子郵件通知來結束。

## 工具
<a name="troubleshooting-states-in-aws-step-functions-tools"></a>

**AWS 服務**
+ [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) 是一項全受管服務，可讓您透過統一 API 使用來自領導 AI 新創公司的高效能基礎模型 (FMs) 和 Amazon。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 是一種無伺服器協同運作服務，可協助您結合 AWS Lambda 函數和其他 AWS 服務 來建置業務關鍵型應用程式。

## 最佳實務
<a name="troubleshooting-states-in-aws-step-functions-best-practices"></a>
+ 由於 Amazon Bedrock 是從訓練資料中學習的生成式 AI 模型，它也會使用該資料來訓練和產生內容。最佳實務是隱藏任何可能導致資料外洩問題的私有資訊。
+ 雖然生成式 AI 可以提供寶貴的洞見，但重要的錯誤處理決策仍應涉及人為監督，尤其是在生產環境中。

## 史詩
<a name="troubleshooting-states-in-aws-step-functions-epics"></a>

### 為您的工作流程建立狀態機器
<a name="create-a-state-machine-for-your-workflow"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立狀態機器。 | 若要建立適合您工作流程的狀態機器，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 

### 建立 Lambda 函式
<a name="create-a-lam-function"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Lambda 函式。 | 若要建立 Lambda 函數，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 
| 在 Lambda 程式碼中設定所需的邏輯。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html)<pre>client = boto3.client(<br />        service_name="bedrock-runtime", region_name="selected-region"<br />    )<br /><br />    # Invoke Claude 3 with the text prompt<br />    model_id = "your-model-id" # Select your Model ID, Based on the Model Id, Change the body format<br /><br />    try:<br />        response = client.invoke_model(<br />            modelId=model_id,<br />            body=json.dumps(<br />                {<br />                    "anthropic_version": "bedrock-2023-05-31",<br />                    "max_tokens": 1024,<br />                    "messages": [<br />                        {<br />                            "role": "user",<br />                            "content": [{"type": "text", "text": prompt}],<br />                        }<br />                    ],<br />                }<br />            ),<br />        )<br /></pre>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 

### 將 Step Functions 與 Lambda 整合
<a name="integrate-sfn-with-lam"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Lambda 來處理 Step Functions 中的錯誤。 | 若要設定 Step Functions 來處理錯誤而不中斷工作流程，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 

## 疑難排解
<a name="troubleshooting-states-in-aws-step-functions-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| Lambda 無法存取 Amazon Bedrock API （未授權執行） | 當 Lambda 角色沒有存取 Amazon Bedrock API 的許可時，會發生此錯誤。若要解決此問題，請新增 Lambda 角色`AmazonBedrockFullAccess`的政策。如需詳細資訊，請參閱《 *AWS 受管政策參考指南*》中的 [AmazonBedrockFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonBedrockFullAccess.html)。 | 
| Lambda 逾時錯誤 | 有時可能需要超過 30 秒才能產生回應並將其傳回，視提示而定。若要解決此問題，請增加組態時間。如需詳細資訊，請參閱《 *AWS Lambda 開發人員指南*》中的[設定 Lambda 函數逾時](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonBedrockFullAccess.html)。 | 

## 相關資源
<a name="troubleshooting-states-in-aws-step-functions-resources"></a>
+ [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
+ [Amazon Bedrock API 存取](https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api.html)
+ [建立您的第一個 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html)
+ [使用 Step Functions 開發工作流程](https://docs.aws.amazon.com/step-functions/latest/dg/developing-workflows.html#development-run-debug)
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html) 