

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

# 使用 Amazon Bedroc AWS Step Functions k 对中的状态进行故障排除
<a name="troubleshooting-states-in-aws-step-functions"></a>

*Aniket Kurzadkar 和 Sangam Kushwaha，Amazon Web Services*

## Summary
<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 工作流中出现错误时，此模式将显示如何将错误消息和上下文发送到 Step Functions 支持的基础模型（FM），例如 Claude 3。基础模型可以分析错误，对其进行分类，并提出可以采取的补救措施。

## 先决条件和限制
<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)，然后选择相应服务的链接。

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

下图显示了此模式的工作流和架构组件。

![用于使用 Step Functions、Amazon Bedrock 和 Amazon SNS 来处理错误和发送通知的工作流。](http://docs.aws.amazon.com/zh_cn/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）托管着生成式人工智能模型的 Amazon Bedrock 将处理输入的提示。（此模式使用 Anthropic Claude 3 基础模型 (FM)，这是 FMs 亚马逊 Bedrock 支持的众多模型之一。） 人工智能模型将分析错误上下文。随后，此模型会生成一个响应，其中可能包含对错误发生原因的解释、错误的潜在解决方案以及有关如何避免将来再犯同样错误的建议。

     Amazon Bedrock 将向 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 提供来自领先的人工智能初创公司和亚马逊的高性能基础模型 (FMs) 供您使用。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。
+ [Amazon Simple Notiﬁcation 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 是一款从经过训练的数据中学习的生成式人工智能模型，因此它还会使用这些数据来训练和生成上下文。作为一项最佳实践，隐藏任何可能导致数据泄露问题的私有信息。
+ 尽管生成式人工智能可以提供宝贵的洞察，但关键的错误处理决策仍然涉及人工监督，尤其是在生产环境中。

## 操作说明
<a name="troubleshooting-states-in-aws-step-functions-epics"></a>

### 为工作流创建状态机
<a name="create-a-state-machine-for-your-workflow"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建一个状态机。 | 要创建一个适合工作流的状态机，请执行以下操作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 

### 创建 Lambda 函数
<a name="create-a-lam-function"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建一个 Lambda 函数。 | 要创建一个 Lambda 函数，请执行以下操作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/prescriptive-guidance/latest/patterns/troubleshooting-states-in-aws-step-functions.html) | AWS DevOps | 

### 将 Step Functions 与 Lambda 集成
<a name="integrate-sfn-with-lam"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 设置 Lambda，以便处理 Step Functions 中的错误。 | 要设置 Step Functions 以便在不中断工作流的情况下处理错误，请执行以下操作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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` 策略。有关更多信息，请参阅[AmazonBedrockFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonBedrockFullAccess.html)《*AWS 托管策略参考指南》*。 | 
| 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) 