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

Node.js 18.x+环境下JS无法执行Step Function但MJS可正常运行

问题分析与解决方案

对比两段代码后,能直接定位到CommonJS版本的两个关键错误,这是导致Lambda超时的核心原因:

1. 代码中的明显错误

(1) State Machine ARN格式错误

MJS版本的ARN是合规格式:

stateMachineArn: 'arn:aws:states:us-east-1:xxxxx'

而CommonJS版本的ARN多了重复的us-east-1:段:

stateMachineArn: 'arn:aws:states:us-east-1:us-east-1:xxxxx'

AWS状态机ARN的标准格式为arn:aws:states:<区域>:<账号ID>:stateMachine:<状态机名称>,重复的区域段会导致AWS无法定位目标资源,SDK会自动重试请求,最终触发Lambda超时。

(2) 未初始化SFNClient实例

CommonJS代码直接调用stepFunctions.send(command),但从未定义stepFunctions变量(缺少MJS里的const stepFunctions = new SFNClient({region: 'us-east-1'});),这会抛出ReferenceError。但由于你在.catch()中仅打印错误却未终止函数,async函数会一直处于等待状态,最终超时。

2. 修正后的CommonJS代码

const { SFNClient, StartExecutionCommand } = require("@aws-sdk/client-sfn");

exports.handler = async (event) => {
    const ev = {
        "txid": "8787878787877v423-98vh349hv34878787878787879",
        "sid": "8ac36b76-e102-5e1b-ad94-5bb5e6564956",
        "val": "80.0",
        "cur": "USD",
        "type": "s"
    };

    // 补充SFNClient实例初始化
    const stepFunctions = new SFNClient({region: 'us-east-1'});
    // 修正ARN格式
    let params = { 
        input: JSON.stringify(ev), 
        stateMachineArn: 'arn:aws:states:us-east-1:xxxxx'
    };
            
    const command = new StartExecutionCommand(params);
            
    await stepFunctions.send(command)
        .then((result) => console.log(result))
        .catch((er) => {
            console.error(er.stack);
            throw er; // 抛出错误让Lambda终止并记录到CloudWatch
        });
};

3. 额外排查建议

  • 查看CloudWatch日志:确保Lambda的日志组中能看到错误信息,若之前的catch未抛出错误,错误可能被静默处理,无法定位问题。
  • 统一SDK版本:确认CommonJS和MJS使用的@aws-sdk/client-sfn版本一致,不同版本的重试逻辑或配置可能存在差异。
  • 简化测试:可以暂时去掉async/await,改用回调模式测试,确认请求是否能正常完成,排除异步处理的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:57:43