使用Lambda调用AWS Step Functions

4

我已经设置了一个步骤函数来调用lambda发送电子邮件。

我已经进行手动测试,它的确有效...现在我想使用新的lambda最初调用此步骤函数...我已经在网上找到了一些代码,并进行了尝试...通过了测试并且没有触发任何错误...有人知道我缺少什么吗,因为它没有起作用?

我从 https://www.youtube.com/watch?v=9MKL5Jr2zZ4&t=306s 的教程中找到了代码,并认为直接复制它应该没问题,因为她唯一使用它的方式是调用步骤函数。

谢谢

'use strict';

const AWS = require('aws-sdk');
const stepFunctions = new AWS.StepFunctions();

//module.exports.hello = (event, context, callback) => {
exports.handler = function(event, context) {
    const response = {
        statusCode:200,
        body: JSON.stringify({
            message: 'Hello World!',
            input: event,
        }),
    };

//  callback(null, response);
};

module.exports.init = (event, context, callback) => {

    const params = {
        stateMachineArn: 'STATE-MACHINE-ARN',
        input: '',
        name: 'Execution lambda'
    }

    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函数只需调用步骤函数executeSendEmailLambda

任何帮助都将不胜感激。

更新 在得到I think I am one的帮助后,我们又回到了测试通过但Lambda未调用步骤F的起点。

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 West (Oregon)'
});

console.log('Loading init');
module.exports.init = (event, context, callback) => {

    console.log('Loading params');

    const params = {
        stateMachineArn: 'STATE-MACHINE-ARN',
        // 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);
        }
    });
    };

};

这个的日志显示如下

    
23:54:47
2017-12-07T23:54:47.448Z    016133fa-dbaa-11e7-8473-7147adf52922    Loading function

23:54:47
START RequestId: 016133fa-dbaa-11e7-8473-7147adf52922 Version: $LATEST

23:54:47
2017-12-07T23:54:47.767Z    016133fa-dbaa-11e7-8473-7147adf52922    Loading step functions

23:54:47
2017-12-07T23:54:47.905Z    016133fa-dbaa-11e7-8473-7147adf52922    Loading init

23:54:47
END RequestId: 016133fa-dbaa-11e7-8473-7147adf52922

23:54:47
REPORT RequestId: 016133fa-dbaa-11e7-8473-7147adf52922  Duration: 178.97 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 31 MB
No newer events found at the moment. Retry.

你解决了这个问题吗?我尝试了你的更新部分代码,Lambda 执行成功,但没有调用步骤函数。 - Sreejith Sree
请看这里如何从Node.js Lambda函数调用步骤函数?。我做了完全相同的事情,从Lambda中调用了一个步骤函数。 - samtoddler
2个回答

1

我稍微修改了你的代码,并在我的一台步函数上进行了测试,这段代码似乎对我有效 :)

const AWS = require('aws-sdk');

const stepFunctions = new AWS.StepFunctions({
region: 'YOUR_REGION_NAME'
});

module.exports.init = (event, context, callback) => {
const params = {
    stateMachineArn: 'YOUR_STATE_MACHINE_ARN',
    // 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
};

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);
    }
});
};

嘿Keshav,感谢你抽出时间回复我...我已经尝试了你建议的添加我的区域名称和状态机arn的方法。但是这会触发一个错误响应:{“errorMessage”:“模块'index'上缺少处理程序'handler'”}......所以我添加了缺失的exports.handler = function(event, context) { const response = { statusCode:200, body: JSON.stringify({ message: 'Hello World!', input: event, }), };}; - John
这似乎是有效的,测试时没有出现错误,但它没有调用步骤函数。您有任何想法为什么它可能不起作用吗? - John
抱歉回复晚了,但是如果您只是在serverless.yml文件中将handler.handler的名称更改为handler.init,我的代码就应该能够正常工作了。您不需要导出另一个名为handler的模块,因为当您这样做时,Lambda函数将仅运行此模块内的代码,并且不会考虑init模块。 - Keshav Kumaresan
我已经更新了区域和状态机ARN值。但是当我执行时,我收到以下错误消息:{ "errorMessage": "2021-03-31T11:53:06.420Z *使用-*分隔的一些键* 任务在3.02秒后超时" } - Sreejith Sree

1

enter image description here

{
"Comment": "Call Library_ReIndex Lambda",
"StartAt": "Library_StepFun_RDSAccess",
"States": {
"Library_StepFun_RDSAccess": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east- 
    1:163806924483:function:Library_StepFun_RDSAccess",
  "OutputPath": "$",
  "Next": "MappingState"
},
"MappingState": {
  "Type": "Map",
  "InputPath": "$",
  "ItemsPath": "$.documents",
  "MaxConcurrency": 10,
  "Iterator": {
    "StartAt": "Choice",
    "States": {
      "Choice": {
        "Type": "Choice",
        "InputPath": "$",
        "Choices": [
          {
            "Variable": "$.doc_source_type",
            "StringEquals": "slides",
            "Next": "callSlides"
          },
          {
            "Variable": "$.doc_source_type",
            "StringEquals": "docs",
            "Next": "callDocs"
          }
        ]
      },
      "callDocs": {
        "Type": "Task",
        "InputPath": "$",
        "Resource": "arn:aws:lambda:us-east-1:163806924483:function:Library-Docs- 
          Parser-2",
        "Catch": [ {
        "ErrorEquals": ["States.ALL"],
        "Next": "CatchAllFallback"
        } ],
        "OutputPath": "$",
        "End": true
      },
      "callSlides": {
        "Type": "Task",
        "InputPath": "$",
        "Resource": "arn:aws:lambda:us-east-1:163806924483:function:Library-Slides-Parser",
        "Catch": [ {
        "ErrorEquals": ["States.ALL"],
        "Next": "CatchAllFallback"
        } ],
        "OutputPath": "$",
        "End": true
      },
      "CatchAllFallback": {
      "Type": "Pass",
      "Result": "This is a fallback from any error code",
      "End": true
      }
    }
  },
  "End": true
}
}
}

代码缩进可以使代码更加优美 - Adi Prasetyo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接