适用于 Step Functions 的 CloudWatch Events 执行状态更改 - AWS Step Functions
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

适用于 Step Functions 的 CloudWatch Events 执行状态更改

Amazon CloudWatch Events 是一项 AWS 服务,可让您在响应 AWS 资源中的状态更改。您可以通过两种方式将 AWS Step Functions 用于 CloudWatch Events。

您可以配置 Step Functions 以在执行状态更改时发出 CloudWatch Events。这使您能够监控您的工作流,而不必持续不断地使用 DescribeExecution API 进行轮询。根据状态机执行中的更改,您可以使用 CloudWatch Events 目标来启动新的状态机执行,调用 AWS Lambda 函数,将消息发布到 Amazon Simple Notification Service (Amazon SNS) 主题,等等。

您也可以配置 Step Functions 状态机作为 CloudWatch Events 中的目标。这使您能够触发 Step Functions 工作流的执行,以响应另一项 AWS 服务中的事件。

有关更多信息,请参阅 Amazon CloudWatch Events 用户指南

Step Functions 事件示例

以下是 Step Functions 向 CloudWatch Events 发送事件的示例:

在每种情况下,事件数据中的 detail 部分都提供与 DescribeExecution API 相同的信息。status 字段指明发送事件时执行的状态,根据发送的事件,状态可以是:RUNNINGSUCCEEDEDFAILEDTIMED_OUTABORTED

执行已启动

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

执行已成功

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

执行已失败

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

执行超时

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

执行已中止

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

在 CloudWatch Events 控制台中将 Step Functions 事件路由到 CloudWatch Events 目标

  1. 在状态机的 Details (详细信息) 页面上,依次选择 Actions (操作)Create CloudWatch event rule (创建 CloudWatch 事件规则)

    
            创建 CloudWatch 事件规则

    或者,通过以下网址打开 CloudWatch 控制台:https://console.aws.amazon.com/cloudwatch/。在导航窗格中,依次选择 EventsRules

  2. 选择 Create rule

  3. Event Source (事件源) 下,确保选中 Event Pattern (事件模式)

  4. 对于 Service Name (服务名称),选择 Step Functions

  5. 对于 Event Type (事件类型),选择 Step Functions Execution Status Change (Step Functions 执行状态更改)

  6. (可选)配置特定的状态、状态机 Amazon 资源名称 (ARN) 或执行 ARN。

  7. Targets (目标) 下,选择 Add target (添加目标),然后从列表中选择一个目标。例如,您可以启动一个 Lambda 函数,或者开始执行 Step Functions 状态机。

  8. 添加特定目标的详细信息。

  9. 选择 Configure details。在 Configure rule details (配置规则详细信息) 页面上,输入Name (名称)Description (说明)

  10. 选择 Create rule