微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 Node.js Lambda 函数调用步骤函数?

如何解决如何从 Node.js Lambda 函数调用步骤函数?

我正在尝试从 Node.js lambda 函数调用步进函数。我尝试了此 thread 中的解决方案和更新的实现。

显示错误响应的解决方案,但显示成功响应的更新代码。但是更新后的代码并没有调用 step 函数

我的代码


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 函数的 Test 选项。这是执行 Lambda 函数的正确方法吗?测试结果是成功,但是当我查看状态机时,没有最近的执行。帮我找到解决方案,我对步进函数很陌生。提前致谢。

解决方法

这是我做过的事情:

  • 创建了 lambda 并在 lambda role 中添加了执行 step 函数的权限
  • 创建了 standard 类型的步进函数(只是一个 hello world)。在创建时,我选择了 ALL Logs,它转到 CloudWatch 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 执行日志

enter image description here

Step 函数执行日志

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。