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

调用方失败时如何终止AWS Step Functions嵌套工作流及Glue任务?

解决方案:实现父工作流失败时终止子工作流及Glue任务

默认情况下,使用arn:aws:states:::states:startExecution.sync:2同步启动子工作流时,父工作流的失败/中止不会自动触发子流终止。要实现需求,需要结合EventBridge事件监听和主动终止API来完成,具体步骤如下:

1. 记录子工作流的执行ARN

在父工作流启动子流时,需要捕获子流的执行ARN,后续用来终止它。修改你的CustomState定义,添加ResultPath保留子流执行信息:

var executeGlueJobStepFunction = new CustomState(this, "ExecuteGlueJobStepFunction", new CustomStateProps
{
    StateJson = new Dictionary<string, object>
    {
        { "Type", "Task" },
        { "Resource", "arn:aws:states:::states:startExecution.sync:2" },
        { "Parameters", new Dictionary<string, object>
            {
                { "StateMachineArn.$", JsonPath.Format($"arn:aws:states:{Stack.Of(this).Region}:{Stack.Of(this).Account}:stateMachine:{{}}", JsonPath.StringAt("$.stepFunctionName")) },
                { "Input.$", JsonPath.EntirePayload }
            }
        },
        { "ResultSelector", new Dictionary<string,object>
            {
                { "Output.$", "$.Output" },
                { "ExecutionArn.$", "$.ExecutionArn" } // 保留子流执行ARN
            }
        },
        { "ResultPath", "$.ChildWorkflowInfo" } // 将结果存入父流payload
    },
});

2. 创建终止子流及Glue任务的Lambda函数

编写Lambda函数,接收父工作流失败/中止事件,提取子流执行ARN后,调用Step Functions和Glue的API终止对应资源。核心逻辑示例:

using Amazon.StepFunctions;
using Amazon.StepFunctions.Model;
using Amazon.Glue;
using Amazon.Glue.Model;

public async Task FunctionHandler(EventBridgeEvent input, ILambdaContext context)
{
    // 从事件中提取子流执行ARN
    var childExecutionArn = input.Detail["output"]?["ChildWorkflowInfo"]?["ExecutionArn"]?.ToString();
    if (string.IsNullOrEmpty(childExecutionArn)) return;

    // 终止子工作流
    var sfnClient = new AmazonStepFunctionsClient();
    await sfnClient.StopExecutionAsync(new StopExecutionRequest
    {
        ExecutionArn = childExecutionArn,
        Cause = "Parent workflow failed/aborted",
        Error = "ParentWorkflowTerminated"
    });

    // 终止关联Glue任务(需从事件中获取Glue任务名称和运行ID)
    var glueJobName = input.Detail["input"]?["glueJobName"]?.ToString();
    var glueRunId = input.Detail["output"]?["ChildWorkflowInfo"]?["Output"]?["GlueRunId"]?.ToString();
    if (!string.IsNullOrEmpty(glueJobName) && !string.IsNullOrEmpty(glueRunId))
    {
        var glueClient = new AmazonGlueClient();
        await glueClient.BatchStopJobRunAsync(new BatchStopJobRunRequest
        {
            JobName = glueJobName,
            JobRunIds = new List<string> { glueRunId }
        });
    }
}

3. 用EventBridge监听父工作流的失败/中止事件

通过CDK创建EventBridge规则,监听父工作流的ExecutionFailed和ExecutionAborted事件,触发上述Lambda:

// 获取父工作流ARN
var parentStateMachineArn = parentStateMachine.StateMachineArn;

// 创建EventBridge规则
var rule = new Rule(this, "ParentWorkflowFailureRule", new RuleProps
{
    EventPattern = new EventPattern
    {
        Source = new[] { "aws.states" },
        DetailType = new[] { "Step Functions Execution Status Change" },
        Detail = new Dictionary<string, object>
        {
            { "stateMachineArn", new[] { parentStateMachineArn } },
            { "status", new[] { "FAILED", "ABORTED" } }
        }
    }
});

// 绑定Lambda作为目标
rule.AddTarget(new LambdaFunction(terminateChildWorkflowLambda));

4. 子工作流内的辅助处理(可选)

如果子工作流直接调用Glue任务,可在子流中添加心跳检查逻辑:定期验证父工作流状态,若父流已终止,子流主动停止并终止Glue任务,减少EventBridge的响应延迟。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 11:12:26