aws从lambda调用step函数 [英] aws call a step function from lambda

查看:130
本文介绍了aws从lambda调用step函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经设置了一个步进功能来调用将发送电子邮件的lamba.

SO I have setup a step function to call a lamba which will send an email.

我已经对此进行了手动测试,现在可以使用...现在,我想首先使用新的lambda来调用此step函数....我已经在网上找到了一些代码,并且已经开始使用它了....通过测试并且不会引发任何错误....有人知道我在缺少什么吗?

I have manually tested this and it works...Now I want to initially call this step function with a new lambda....I've found some code online and I've played about with it....passes the test and doesn't fire any errors....does anyone know what I am missing as it isn't working?

我在 https://www.youtube.com/watch?v=9MKL5Jr2zZ4&t=306s 上找到了该教程中的代码,我认为可以直接复制它因为她唯一的用处就是调用阶跃函数.

I've found the code from the tutorial on https://www.youtube.com/watch?v=9MKL5Jr2zZ4&t=306s and I think it should be ok to directly copy it as her only use for it is to call a step function.

谢谢

'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

all I want this lambda to do is call the step function executeSendEmailLambda

任何帮助都将是巨大的

更新多亏了我的帮助,我离测试更加接近了,但是我们回到了测试通过的平方,但是lambda没有调用步骤F

UPDATE Thanks to the help of I think I am one bit closer but we are back to square one of test passing but the lambda is not calling the step 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.

推荐答案

我对您的代码进行了一些修改,并使用我的一个步进函数"对其进行了测试,该代码似乎对我有用:)

I modified your code a little bit and tested it with one of my Step Functions and this code seems to work for me :)

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

这篇关于aws从lambda调用step函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆