You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AWS Step Functions从同步执行中终止主状态机及自身执行?

Solution to Terminate Both Child and Parent Step Functions Executions

Let's break down how to achieve this while keeping your state machines modular, just as you need. Here's the step-by-step approach:

1. Modify the Child State Machine to Throw an Error Instead of Ending Normally

Instead of letting the child execution end quietly when hitting the "No Inventory Required" state, we'll use a Fail state to explicitly send an error signal back to the parent. This ensures the synchronous call from the parent picks up the termination trigger instead of a success signal.

Update your child state machine definition like this:

{
  "Lambda File Check": {
    "Type": "Task",
    "Resource": "${LambdaFileCheck}",
    "Catch": [
      {
        "ErrorEquals": ["NoInventoryPhaseRequired"],
        "Next": "No Inventory Required"
      }
    ],
    "End": true
  },
  "No Inventory Required": {
    "Type": "Fail",
    "Error": "NoInventoryPhaseRequired",
    "Cause": "Inventory processing is not needed; terminating parent execution."
  }
}
  • The Fail state reuses the same ErrorEquals value (NoInventoryPhaseRequired) from your Lambda's error. This ensures the parent state machine can recognize and handle the signal consistently.

2. Update the Parent State Machine to Catch the Error and Terminate

Since you're using startExecution.sync:2, the parent state machine receives all outputs (and errors) from the child execution. We'll add a Catch block to the "Inventory process" task to intercept this error and immediately end the parent flow.

Modify your parent state machine definition:

{
  "Inventory process": {
    "Type": "Task",
    "Resource": "arn:aws:states:::states:startExecution.sync:2",
    "Parameters": {
      "StateMachineArn": "${StateMachineInventory}"
    },
    "Next": "Another activity",
    "Catch": [
      {
        "ErrorEquals": ["NoInventoryPhaseRequired"],
        "Next": "Terminate Main Execution"
      }
    ]
  },
  "Terminate Main Execution": {
    "Type": "Succeed",
    "Comment": "Terminates the parent execution without proceeding to 'Another activity'"
  }
}
  • The Catch block listens specifically for the NoInventoryPhaseRequired error from the child execution.
  • The Terminate Main Execution state uses Succeed to end the parent execution cleanly. If you prefer to mark the parent as a failed execution (for logging or alerting purposes), you can swap this with a Fail state and adjust the Error/Cause fields as needed.

How This Works End-to-End

When your Lambda throws NoInventoryPhaseRequired:

  1. The child state machine catches the error and moves to the Fail state, which sends the error back to the parent's synchronous task.
  2. The parent's "Inventory process" task catches the error, skips the "Another activity" step entirely, and moves straight to the termination state.
  3. Both the child and parent executions are terminated immediately, exactly as you need.

内容的提问来源于stack exchange,提问作者Odenelle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:22:39