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

本地运行Step Functions时WaitMachine卡在FirstState的问题求助

本地运行AWS Step Functions时状态机卡在Task状态的问题

启动名为WaitMachine的状态机后,卡在触发hello Lambda函数的FirstState,虽已打印hello但无法进入下一个FinalState,报错信息如下:

{"Type":"ExecutionFailed","PreviousEventId":5,"ExecutionFailedEventDetails":{"Error":"Lambda.SdkClientException","Cause":"Unable to execute HTTP request: Request did not complete before the request timeout configuration."}}

相关配置文件

serverless.yml

service: stepfunctions
frameworkVersion: '2'
plugins:
  - serverless-step-functions-local
  - serverless-step-functions
  - serverless-offline-lambda
  - serverless-offline
custom:
  serverless-offline:
  stepFunctionsLocal:
    accountId: 101010101010
    region: us-east-1
    TaskResourceMapping:
      FirstState: arn:aws:lambda:us-east-1:101010101010:function:hello
      FinalState: arn:aws:lambda:us-east-1:101010101010:function:world
provider:
  name: aws
  runtime: nodejs12.x
functions:
  hello:
    handler: handler.hello 
  world:
    handler: handler.world
  startSF:
    handler: handler.startSF
    events:
      - http:
          path: hello
          method: GET
stepFunctions:
  stateMachines:
    WaitMachine:
      definition:
        Comment: "An example of the Amazon States Language using wait states"
        StartAt: FirstState
        States:
          FirstState:
            Type: Task
            Resource: 
              Fn::GetAtt: [hello, Arn]
            Next: FinalState
          FinalState:
            Type: Task
            Resource: 
              Fn::GetAtt: [world, Arn]
            End: true

handler.js

const AWS = require('aws-sdk');
module.exports.startSF = (event, context, callback) => {
  const stepFunctions = new AWS.StepFunctions({ endpoint: 'http://localhost:8083' })
  const stateMachineArn = process.env.OFFLINE_STEP_FUNCTIONS_ARN_WaitMachine
  const params = {
    stateMachineArn
  }

  return stepFunctions.startExecution(params).promise().then(() => {
    callback(null, `Your state machine ${stateMachineArn} executed successfully `)
  }).catch(error => {
    callback(error.message);
  })
}

module.exports.hello = async event => {
  console.log('hello')
  return { foo: 1 }
}

module.exports.there = async event => {
  console.log('there')
  return { message: 'Final thing!!!', event }
}

module.exports.world = async event => {
  console.log('world')
  return { message: 'Final thing!!!', event }
}

package.json

{
  "name": "stepfunction",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "delete": "aws stepfunctions --endpoint-url http://localhost:8083 delete-state-machine --state-machine-arn 'arn:aws:states:us-east-1:101010101010:stateMachine:WaitMachine' &",
    "start": " serverless offline start --stage local-dev"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "serverless-offline": "^8.0.0",
    "serverless-offline-lambda": "^1.0.6",
    "serverless-step-functions-local": "^0.1.6"
  },
  "dependencies": {
    "aws-sdk": "^2.1446.0",
    "serverless-step-functions": "^3.0.0"
  }
}

修复方案

1. 修正serverless.yml配置

调整插件加载顺序

插件加载顺序影响服务初始化逻辑,将serverless-offline放在最后:

plugins:
  - serverless-step-functions
  - serverless-step-functions-local
  - serverless-offline-lambda
  - serverless-offline

完善custom配置

添加Lambda本地端点,并修正TaskResourceMapping中的ARN格式(本地Lambda ARN遵循serviceName-functionName-stage规则):

custom:
  serverless-offline:
    httpPort: 3000
  stepFunctionsLocal:
    accountId: 101010101010
    region: us-east-1
    lambdaEndpoint: http://localhost:3001 # 对应serverless-offline-lambda默认端口
    TaskResourceMapping:
      FirstState: arn:aws:lambda:us-east-1:101010101010:function:stepfunctions-hello-local-dev
      FinalState: arn:aws:lambda:us-east-1:101010101010:function:stepfunctions-world-local-dev

2. 修复handler.js中的startSF函数

改用async/await语法避免callback与promise混用的问题,同时指定Step Functions的区域:

module.exports.startSF = async (event, context) => {
  const stepFunctions = new AWS.StepFunctions({ 
    endpoint: 'http://localhost:8083',
    region: 'us-east-1'
  });
  const stateMachineArn = process.env.OFFLINE_STEP_FUNCTIONS_ARN_WaitMachine;
  const params = {
    stateMachineArn
  };

  try {
    await stepFunctions.startExecution(params).promise();
    return {
      statusCode: 200,
      body: `Your state machine ${stateMachineArn} executed successfully `
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: error.message
    };
  }
};

3. 调整依赖版本解决兼容性问题

serverless-offline@8.0.0与serverless-step-functions-local@0.1.6存在兼容性冲突,降级serverless-offline到v7.x版本:
修改package.json的devDependencies:

"devDependencies": {
  "serverless-offline": "^7.1.3",
  "serverless-offline-lambda": "^1.0.6",
  "serverless-step-functions-local": "^0.1.6"
}

执行npm install重新安装依赖。

4. 检查端口占用情况

确保本地服务端口(3000、3001、8083)未被其他程序占用,若有冲突可在对应插件配置中修改端口号。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:35:34