从正在运行的执行中启动新的 Amazon Step Functions 状态机 - Amazon Step Functions
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

从正在运行的执行中启动新的 Amazon Step Functions 状态机

Step Functions 与其自己的 API 集成以作为服务集成。了解如何使用 Step Functions 直接从正在运行的执行的 Task 状态启动新的状态机执行。构建新的工作流时,请使用嵌套工作流执行降低主要工作流的复杂性并重用常用流程。

经优化的 Step Functions 集成的主要功能

有关更多信息,请参阅下列内容:

经过优化的步进函数 APIs

工作流示例

下面包括一个 Task 状态,此状态开始执行另一个状态机并等待其完成。

{ "Type":"Task", "Resource":"arn:aws:states:::states:startExecution.sync:2", "Arguments":{ "Input":{ "Comment": "Hello world!" }, "StateMachineArn":"arn:aws:states:region:account-id:stateMachine:HelloWorld", "Name":"ExecutionName" }, "End":true }

下面包括一个 Task 状态,此状态开始执行另一个状态机。

{ "Type":"Task", "Resource":"arn:aws:states:::states:startExecution", "Arguments":{ "Input":{ "Comment": "Hello world!" }, "StateMachineArn":"arn:aws:states:region:account-id:stateMachine:HelloWorld", "Name":"ExecutionName" }, "End":true }

以下内容包括实施回调服务集成模式的 Task 状态。

{ "Type":"Task", "Resource":"arn:aws:states:::states:startExecution.waitForTaskToken", "Arguments":{ "Input":{ "Comment": "Hello world!", "token": "{% $states.context.Task.Token %}" }, "StateMachineArn":"arn:aws:states:region:account-id:stateMachine:HelloWorld", "Name":"ExecutionName" }, "End":true }

要将嵌套工作流程执行与启动该工作流程的父执行相关联,请传递一个包含从 Conte xt 对象中提取的执行 ID 的特殊命名的参数。启动嵌套执行时,使用名为 AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID 的参数。通过附加.$到参数名称并使用引用 Context 对象中的 ID 来传递执行 ID。$$.Execution.Id有关更多信息,请参阅 访问 Context 对象

{ "Type":"Task", "Resource":"arn:aws:states:::states:startExecution.sync", "Arguments":{ "Input":{ "Comment": "Hello world!", "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id" }, "StateMachineArn":"arn:aws:states:region:account-id:stateMachine:HelloWorld", "Name":"ExecutionName" }, "End":true }

嵌套状态机返回以下内容:

资源 输出
startExecution.sync 字符串
startExecution.sync:2 JSON

两者都将等待嵌套状态机完成,但它们返回不同的 Output 格式。例如,如果您创建一个返回对象 { "MyKey": "MyValue" } 的 Lambda 函数,则会得到以下响应:

对于 startExecution.sync:

{ <other fields> "Output": "{ \"MyKey\": \"MyValue\" }" }

对于 startExecution.sync:2:

{ <other fields> "Output": { "MyKey": "MyValue" } }

为嵌套状态机配置 IAM 权限

父状态机使用轮询和事件来确定子状态机是否已完成执行。轮询需要权限,states:DescribeExecution而发送 EventBridge 到 Step Functions 的事件则需要events:PutTargetsevents:PutRule、和的权限events:DescribeRule。如果您的 IAM 角色中缺少这些权限,则在父状态机意识到子状态机的执行已完成之前,可能会有一段延迟。

对于为单个嵌套工作流执行调用 StartExecution 的状态机,应使用 IAM 策略限制该状态机的权限。

用于调用嵌套 Step Functions 工作流程的 IAM 策略

对于为单个嵌套工作流执行调用 StartExecution 的状态机,应使用 IAM 策略限制该状态机的权限。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "states:StartExecution" ], "Resource": [ "arn:aws:states:region:account-id:stateMachine:stateMachineName" ] } ] }

有关更多信息,请参阅下列内容:

Synchronous
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "states:StartExecution" ], "Resource": [ "arn:aws:states:region:account-id:stateMachine:[[stateMachineName]]" ] }, { "Effect": "Allow", "Action": [ "states:DescribeExecution", "states:StopExecution" ], "Resource": [ "arn:aws:states:region:account-id:execution:stateMachineName:*" ] }, { "Effect": "Allow", "Action": [ "events:PutTargets", "events:PutRule", "events:DescribeRule" ], "Resource": [ "arn:aws:events:region:account-id:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule" ] } ] }
Asynchronous
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "states:StartExecution" ], "Resource": [ "arn:aws:states:region:account-id:stateMachine:stateMachineName" ] } ] }
需要的 ARN 类型

同步策略中,请注意,states:StartExecution需要状态机 ARN,states:DescribeExecutionstates:StopExecution需要执行 ARN。

如果您错误地组合了所有三个操作,则 JSON 将有效,但 IAM 策略将不正确。不正确的策略可能会导致工作流程停滞和/或在工作流程执行期间出现访问问题。

有关嵌套工作流执行的更多信息,请参阅在 Step Functions 中从 Task 状态启动工作流程执行