

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

# 使用 自動化 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 Simple Notification Service (Amazon SNS) 主題，甚至執行另一個 SFN 工作流程。

**事件交付的運作方式**  
Step Functions 會產生事件並將其傳送至預設 EventBridge *事件匯流排*，該匯流排會在每個 AWS 帳戶中自動佈建。事件匯流排是接收事件，並將事件傳遞至零個或多個目的地或*目標*的路由器。目標是其他服務 AWS 。您可以指定事件匯流排的規則，以比較事件與規則*的事件模式*。當事件符合模式時，事件匯流排會將事件傳送至指定的目標 (s)。下圖顯示此程序：

![\[AWS 服務會將事件傳送至 EventBridge，其中規則會比對事件並將其傳送至目標。\]](http://docs.aws.amazon.com/zh_tw/step-functions/latest/dg/images/eventbridge-integration_shared_conceptual.png)


**標準與快速工作流程**  
只有標準工作流程會向 EventBridge 發出事件。若要監控快速工作流程的執行，您可以使用 CloudWatch Logs。請參閱 [在 CloudWatch Logs 中記錄](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 使用者指南](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)》中的 EventBridge 事件。 * EventBridge *


| 事件詳細資訊類型 | Description | 
| --- | --- | 
|  [執行狀態變更](#event-detail-execution-status-change)  |  代表狀態機器執行狀態的變更。 | 

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

若要讓 EventBridge 預設事件匯流排將 Step Functions 事件傳送至目標，您必須建立規則。每個規則都包含事件模式，EventBridge 會比對事件匯流排上收到的每個事件。如果事件資料符合指定的事件模式，EventBridge 會將該事件交付至規則的目標 (s)。

如需建立事件匯流排規則的完整說明，請參閱*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 。

如需詳細資訊，請參閱 *Amazon EventBridge 使用者指南*中的 [Amazon 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 事件的資訊，請參閱《*Amazon EventBridge 使用者指南*》中的[事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)。

如需有關事件以及 EventBridge 如何處理事件的詳細資訊，請參閱《Amazon 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` 不適用於快速工作流程。如果您想要查看完整的輸入/輸出，您可以：
  + 使用標準工作流程包裝您的快速工作流程。
  + 使用 Amazon S3 ARNs。如需使用 ARNs的詳細資訊，請參閱 [在 Step Functions 中使用 Amazon S3 ARNs 而非傳遞大型承載](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
    }
}
```