

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

# 使用自动执行 Step Functions 事件传送 EventBridge
<a name="eventbridge-integration"></a>

使用 EventBridge，您可以从 Step Functions 标准工作流程中选择事件，将其发送到其他服务进行额外处理。此技术提供了一种灵活的方法来松散地连接组件并监控资源。

Amazon EventBridge 是一项无服务器服务，可将应用程序组件连接在一起以构建可扩展的事件驱动型应用程序。事件驱动型架构是一种构建松耦合软件系统的风格，这些系统通过发出和响应*事件* 来协同工作。事件表示状态更改或更新。

通过使用 EventBridge 将 Step Functions 事件传送到其他服务，您可以监控您的标准工作流程，而无需持续调用 [DescribeExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeExecution.html)API 来获取状态。状态机执行中的状态更改 EventBridge 会自动发送到。可以使用这些事件来定位服务。例如，事件可能会调用 AWS Lambda 函数，向亚马逊简单通知服务 (Amazon SNS) Simple Notification Service 主题发布消息，甚至运行另一个 SFN 工作流程。

**事件传送的工作原理**  
Step Functions 生成事件并将其发送到默认 EventBridge *事件总线，该总线*会在每个 AWS 账户中自动配置。事件总线是接收事件并将其传送到零个或多个目的地或*目标*的路由器。目标是其他 AWS 服务。可以为事件总线指定规则，来将事件与规则的*事件模式* 进行比较。当事件与模式匹配时，事件总线会将事件发送到指定的目标。下图显示了此流程：

![\[AWS 服务将事件发送到规则与事件匹配 EventBridge 的地方，然后将其发送到目标。\]](http://docs.aws.amazon.com/zh_cn/step-functions/latest/dg/images/eventbridge-integration_shared_conceptual.png)


**标准工作流程与快速工作流程**  
只有标准工作流程才会向发送事件。 EventBridge要监控快速工作流程的执行情况，您可以使用 CloudWatch 日志。请参阅[登录 CloudWatch 日志](cw-logs.md)。

## Step Functions 事件
<a name="supported-events"></a>

Step Functions 会自动将以下 EventBridge 事件发送到默认事件总线。与规则的事件模式相匹配的事件会[尽力](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html#eb-service-event-delivery-level)传送给指定的目标。事件可能不按顺序传送。

有关更多信息，请参阅 *Amazon EventBridge 用户指南中的[EventBridge 事件](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。*


| 事件详细信息类型 | 说明 | 
| --- | --- | 
|  [执行状态更改](#event-detail-execution-status-change)  |  表示状态机执行状态的变化。 | 

## 使用交付 Step Functions 事件 EventBridge
<a name="eventbridge-using-events-rules"></a>

要让 EventBridge 默认事件总线将 Step Functions 事件发送到目标，必须创建规则。每条规则都包含一个事件模式，该模式与事件总线上接收到的每个事件进行 EventBridge 匹配。如果事件数据与指定的事件模式匹配，则将该事件 EventBridge 传送到规则的目标。

有关创建事件总线规则的全面说明，请参阅《EventBridge 用户指南》**中的[创建对事件作出反应的规则](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule.html)。

您也可以从 Step Functions 控制台为特定状态机创建事件总线规则：
+ 在状态机的**详细信息**页面上，选择**操作**，然后选择**创建*EventBridge*规则**。

   EventBridge 控制台将打开 “**创建规则**” 页面，状态机被选为规则的事件源。
+ 按照*EventBridge 用户指南*中[创建对事件做出反应的规则](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule.html)中详述的步骤进行操作。

### 创建与 Step Functions 事件匹配的事件模式
<a name="eventbridge-using-events-rules-patterns"></a>

每个事件模式是一个 JSON 对象，其中包含：
+ 标识发送事件的服务的 `source` 属性。对于 Step Functions 事件，源为 `aws.states`。
+ （可选）：包含要匹配的事件类型数组的 `detail-type` 属性。
+ （可选）：包含要匹配的其他事件数据的 `detail` 属性。

例如，以下事件模式与 Step Functions 中的所有执行状态更改事件相匹配：

```
{
  "source": ["aws.states"],
  "detail-type": ["Step Functions Execution Status Change"]
}
```

尽管以下示例和与特定状态机关联的特定执行相匹配，但当执行失败或超时时：

```
{
  "source": ["aws.states"],
  "detail-type": ["Step Functions Execution Status Change"],
  "detail": {
    "status": ["FAILED", "TIMED_OUT"],
    "stateMachineArn": ["arn:aws:states:region:account-id:stateMachine:state-machine"],
    "executionArn": ["arn:aws:states:region:account-id:execution:state-machine-name:execution-name"]
  }
}
```

有关写入事件模式的更多信息，请参阅《EventBridge 用户指南》**中的[事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)。

## 使用事件触发 Step Functions 状态机
<a name="eventbridge-stepfunctions-as-target"></a>

您也可以将 Step Functions 状态机指定为 EventBridge 事件总线规则的目标。这使您能够触发 Step Functions 工作流程的执行，以响应来自其他 AWS 服务的事件。

有关更多信息，请参阅《[亚马逊* EventBridge 用户指南》中的亚马逊 EventBridge*目标](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html)。

## Step Functions 事件详细信息参考
<a name="events-detail-reference"></a>

来自 AWS 服务的所有事件都有一组公共字段，其中包含有关事件的元数据，例如作为事件来源的 AWS 服务、事件的生成时间、事件发生的账户和区域等。有关这些常规字段的定义，请参阅 *Amazon EventBridge 用户指南*中的[事件结构参考](https://docs.aws.amazon.com/eventbridge/latest/ref/overiew-event-structure.html)。

此外，每个事件都有一个 `detail` 字段，其中包该特定事件专有的数据。

使用 EventBridge 来选择和管理 Step Functions 事件时，请记住以下几点很有用：
+ 来自 Step Functions 的所有事件的 `source` 字段都设置为 `aws.states`。
+ `detail-type` 字段指定事件类型。

  例如 `Step Functions Execution Status Change`。
+ `detail` 字段包含该特定事件专有的数据。

有关构建事件模式以使规则能够匹配 Step Functions [事件的信息，请参阅*亚马逊 EventBridge 用户指南*中的事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)。

有关事件及其 EventBridge 处理方式的更多信息，请参阅《[亚马逊* EventBridge 用户指南》中的 Amazon EventBridge* 事件](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。

### 执行状态更改
<a name="event-detail-execution-status-change"></a>

表示状态机执行状态的变化。

之所以包含下面的 `source` 和 `detail-type` 字段，是因为它们包含 Step Functions 事件的特定值。有关所有事件中包含的其他元数据字段的定义，请参阅 *Amazon EventBridge 用户指南*中的[事件结构参考](https://docs.aws.amazon.com/eventbridge/latest/ref/overiew-event-structure.html)。

#### 事件结构
<a name="event-detail-execution-status-change-structure"></a>

```
{
  . . .,
  "detail-type": "Step Functions Execution Status Change",
  "source"": "aws.states",
  . . .,
  "detail"": {
    "executionArn"" : "string",
    "input" : "string",
    "inputDetails" : {
      "included" : "boolean"
    },
    "name" : "string",
    "output" : "string",
    "outputDetails" : {
      "included" : "boolean"
    },
    "startDate" : "integer",
    "stateMachineArn" : "string",
    "stopDate" : "integer",
    "status" : "RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED | PENDING_REDRIVE"
  }
}
```

#### 备注
<a name="event-detail-execution-status-change-remarks"></a>

执行状态更改事件可以在其定义中包含输入属性。对于某些事件，执行状态更改事件还可以在其定义中包含输出属性。
+ 如果发送到的转义输入和转义输出的组合 EventBridge 超过 248 KiB，则该输入将被排除在外。同样，如果转义输出也超过 248 KiB，则该输出也将被排除在外。这是事件配额的结果。
+  您可以使用 `inputDetails` 和 `outputDetails` 属性确定有效负载是否已被截断。有关更多信息，请参阅 [`CloudWatchEventsExecutionDataDetails` 数据类型](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CloudWatchEventsExecutionDataDetails.html)。
+  对于标准工作流程，[DescribeExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeExecution.html)请使用查看完整的输入和输出。

   `DescribeExecution` 不适用于快速工作流。如果要查看完整的输入/输出，则可以：
  + 将快速工作流程与标准工作流程相结合。
  + 使用亚马逊 S3 ARNs。有关使用的信息 ARNs，请参见[使用 Amazon S3 ARNs 而不是在 Step Functions 中传递大型有效负载](sfn-best-practices.md#avoid-exec-failures)。

#### 示例
<a name="event-detail-execution-status-change-examples"></a>

**Example 执行状态更改：执行已启动**  <a name="event-detail-execution-status-change-started.example"></a>

```
{
    "version": "0",
    "id": "315c1398-40ff-a850-213b-158f73e60175",
    "detail-type": "Step Functions Execution Status Change",
    "source": "aws.states",
    "account": "account-id",
    "time": "2019-02-26T19:42:21Z",
    "region": "us-east-2",
    "resources": [
      "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name"
    ],
    "detail": {
        "executionArn": "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name",
        "stateMachineArn": "arn:aws::states:us-east-2:account-id:stateMachine:state-machine",
        "name": "execution-name",
        "status": "RUNNING",
        "startDate": 1551225271984,
        "stopDate":  null,
        "input": "{}",
        "inputDetails": {
             "included": true
        },
        "output": null,
        "outputDetails": null
    }
}
```

**Example 执行状态更改：执行已成功**  <a name="event-detail-execution-status-change-succeeded.example"></a>

```
{
    "version": "0",
    "id": "315c1398-40ff-a850-213b-158f73e60175",
    "detail-type": "Step Functions Execution Status Change",
    "source": "aws.states",
    "account": "account-id",
    "time": "2019-02-26T19:42:21Z",
    "region": "us-east-2",
    "resources": [
      "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name"
    ],
    "detail": {
        "executionArn": "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name",
        "stateMachineArn": "arn:aws:states:us-east-2:account-id:stateMachine:state-machine",
        "name": "execution-name",
        "status": "SUCCEEDED",
        "startDate": 1547148840101,
        "stopDate": 1547148840122,
        "input": "{}",
        "inputDetails": {
             "included": true
        },
        "output": "\"Hello World!\"",
        "outputDetails": {
             "included": true
        }
    }
}
```

**Example 执行状态更改：执行已失败**  <a name="event-detail-execution-status-change-failed.example"></a>

```
{
    "version": "0",
    "id": "315c1398-40ff-a850-213b-158f73e60175",
    "detail-type": "Step Functions Execution Status Change",
    "source": "aws.states",
    "account": "account-id",
    "time": "2019-02-26T19:42:21Z",
    "region": "us-east-2",
    "resources": [
      "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name"
    ],
    "detail": {
        "executionArn": "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name",
        "stateMachineArn": "arn:aws:states:us-east-2:account-id:stateMachine:state-machine",
        "name": "execution-name",
        "status": "FAILED",
        "startDate": 1551225146847,
        "stopDate": 1551225151881,
        "input": "{}",
        "inputDetails": {
             "included": true
        },
        "output": null,
        "outputDetails": null
    }
}
```

**Example 执行状态更改：已超时**  <a name="event-detail-execution-status-change-timed-out.example"></a>

```
{
    "version": "0",
    "id": "315c1398-40ff-a850-213b-158f73e60175",
    "detail-type": "Step Functions Execution Status Change",
    "source": "aws.states",
    "account": "account-id",
    "time": "2019-02-26T19:42:21Z",
    "region": "us-east-2",
    "resources": [
      "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name"
    ],
    "detail": {
        "executionArn": "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name",
        "stateMachineArn": "arn:aws:states:us-east-2:account-id:stateMachine:state-machine",
        "name": "execution-name",
        "status": "TIMED_OUT",
        "startDate": 1551224926156,
        "stopDate": 1551224927157,
        "input": "{}",
        "inputDetails": {
             "included": true
        },
        "output": null,
        "outputDetails": null
```

**Example 执行状态更改：已中止**  <a name="event-detail-execution-status-change-aborted.example"></a>

```
{
    "version": "0",
    "id": "315c1398-40ff-a850-213b-158f73e60175",
    "detail-type": "Step Functions Execution Status Change",
    "source": "aws.states",
    "account": "account-id",
    "time": "2019-02-26T19:42:21Z",
    "region": "us-east-2",
    "resources": [
      "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name"
    ],
    "detail": {
        "executionArn": "arn:aws:states:us-east-2:account-id:execution:state-machine-name:execution-name",
        "stateMachineArn": "arn:aws:states:us-east-2:account-id:stateMachine:state-machine",
        "name": "execution-name",
        "status": "ABORTED",
        "startDate": 1551225014968,
        "stopDate": 1551225017576,
        "input": "{}",
        "inputDetails": {
             "included": true
        },
        "output": null,
        "outputDetails": null
    }
}
```