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

为何Lambda函数无法调用Step Function的startExecution方法?

Lambda调用Step Functions startExecution无响应排查求助

我正尝试将Lambda函数与Step Function关联,但无法实现startExecution调用,特此求助。

SDK代码

import AWS from "aws-sdk";
const stepfunctions = new AWS.StepFunctions({ apiVersion: "2016-11-23" });
interface Params {
  stateMachineArn: string;
  input: string;
}

export async function handler(event: any, context: object) {
  console.log("event.body", event.body);

  const params: Params = {
    stateMachineArn: process.env.STEP_FUNCTION_ARN,
    input: JSON.stringify(event.body),
    name: "testNameField",
  };

  console.log("PARAMS", params);

  stepfunctions.startExecution(params, (err: any, data: any) => {
    if (err) {
      console.log("THERE WAS AN ERROR", err);
      console.log("ERROR STACK", err.stack);
    } // an error occurred
    else {
      console.log("data", data);
    } // successful response
  });
}

权限配置

Allow: states:DeleteStateMachine
Allow: states:StartExecution
Allow: states:CreateStateMachine
Allow: states:SendTaskSuccess
Allow: states:DeleteActivity
Allow: states:SendTaskHeartbeat
Allow: states:CreateActivity
Allow: states:SendTaskFailure
Allow: states:StopExecution
Allow: states:GetActivityTask
Allow: states:UpdateStateMachine
Allow: states:StartSyncExecution

补充信息

  • 已在控制台测试Lambda函数,测试成功,但不知问题出在哪里。
  • Step Function的所有统计列(总计/运行中/成功/失败/超时/中止)数值均为0。
  • 控制台打印的params参数信息正确无误。

问题排查与解决方案

  1. 核心问题:Lambda异步处理未等待回调完成
    你的handler是async函数,但调用startExecution使用了回调模式。Lambda的异步handler会在函数执行到末尾(无return或await)时立即终止进程,导致回调函数根本没机会执行,自然不会触发Step Functions执行,也不会打印回调里的日志。

    修改方案:改用Promise+await模式
    将回调替换为AWS SDK的.promise()方法,确保Lambda等待Step Functions调用完成:

    import AWS from "aws-sdk";
    const stepfunctions = new AWS.StepFunctions({ apiVersion: "2016-11-23" });
    interface Params {
      stateMachineArn: string;
      input: string;
      name?: string;
    }
    
    export async function handler(event: any, context: object) {
      console.log("event.body", event.body);
    
      const params: Params = {
        stateMachineArn: process.env.STEP_FUNCTION_ARN!,
        input: JSON.stringify(event.body),
        name: "testNameField",
      };
    
      console.log("PARAMS", params);
    
      try {
        const data = await stepfunctions.startExecution(params).promise();
        console.log("data", data);
        return data;
      } catch (err) {
        console.error("THERE WAS AN ERROR", err);
        if (err instanceof Error) {
          console.error("ERROR STACK", err.stack);
        }
        throw err; // 抛出错误让Lambda标记为失败,便于排查
      }
    }
    
  2. 权限配置验证
    确认权限策略中的Resource字段是否包含目标Step Function的ARN,而非仅写Allow: states:StartExecution却未指定资源。正确的权限策略应类似:

    {
      "Effect": "Allow",
      "Action": "states:StartExecution",
      "Resource": "arn:aws:states:你的区域:你的账号ID:stateMachine:你的状态机名称"
    }
    
  3. 其他排查点

    • 检查Lambda的CloudWatch完整日志:修改代码后如果仍失败,日志会捕获到具体错误(比如权限不足、ARN错误等)。
    • 确认STEP_FUNCTION_ARN环境变量值完全正确,包括区域、账号ID和状态机名称,无拼写错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 11:35:29