问题描述
我正在尝试从 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 执行日志
Step 函数执行日志