如何从 Node.js Lambda 函数调用阶跃函数?

How to call a step funtion from Node.js Lambda function?

我正在尝试从 Node.js lambda 函数调用步进函数。我尝试了这个 thread.

的解决方案和更新的实现

The solution showing the error response but the updated code showing success response. But the updated code is not calling the step function.

我的代码:


console.log('Loading function');
const AWS = require('aws-sdk');
exports.handler = function(event, context) {
    console.log('Loading step functions');
    const stepFunctions = new AWS.StepFunctions({
    region: 'us-east-2'
});
console.log('Loading init');
module.exports.init = (event, context, callback) => {
console.log('Loading params');
const params = {
        stateMachineArn: 'ARN of My State Machine',
        // input: JSON.stringify({}), Optional if your statemachine requires an application/json input, make sure its stringified 
        name: 'TestExecution' // name can be anything you want, but it should change for every execution
    };

console.log('start step functions');
stepFunctions.startExecution(params, (err, data) => {
        if (err) {
            console.log(err);
            const response = {
                statusCode: 500,
                body: JSON.stringify({
                    message: 'There was an error'
                })
            };
            callback(null, response);
        } else {
            console.log(data);
            const response = {
                statusCode: 200,
                body: JSON.stringify({
                    message: 'Step function worked'
                })
            };
            callback(null, response);
            console.log(response);
        }
    });
    };
};

我已将上述代码添加到 Lambda 函数中并部署代码。之后我使用了 lambda 函数的测试选项。这是执行 Lambda 函数的正确方法吗?测试结果是成功的,但是我查看状态机时,没有最近的执行。帮助我找到解决方案,我对 step 功能很陌生。提前致谢。

以下是我所做的事情:

  • 创建了 lambda 并在 lambda role
  • 中添加了步骤函数执行权限
  • 创建了 standard 类型的步骤函数(只是一个 hello world)。在创建时我选择了 ALL LogsCloudWatch Log Group。即使它们显示在 step function 控制台的 Logging 选项卡下,如下所示。

下面是我调用step函数的代码:

var aws = require('aws-sdk')
exports.handler = (event, context, callback) => {
  var params = {
    stateMachineArn: 'arn:aws:states:us-east-1:1234567890:stateMachine:Helloworld',
    input: JSON.stringify({})
  };
  var stepfunctions = new aws.StepFunctions()
  stepfunctions.startExecution(params, (err, data) => {
    if (err) {
    console.log(err);
    const response = {
        statusCode: 500,
        body: JSON.stringify({
        message: 'There was an error'
        })
    };
    callback(null, response);
    } else {
    console.log(data);
    const response = {
        statusCode: 200,
        body: JSON.stringify({
        message: 'Step function worked'
        })
    };
    callback(null, response);
    }
});
}

Lambda 执行日志

Step 函数执行日志