aws从lambda调用step函数

时间:2017-12-07 06:56:22

标签: javascript node.js amazon-web-services amazon-s3 aws-lambda

所以我设置了一个步骤函数来调用lamba,它将发送一封电子邮件。

我已经手动测试了它并且它有效...现在我想最初用一个新的lambda调用这个步骤函数....我在网上发现了一些代码并且iv用它来玩....通过测试和并没有解决任何错误....有没有人知道我失踪了什么因为它不起作用?

iv找到了https://www.youtube.com/watch?v=9MKL5Jr2zZ4&t=306s教程中的代码,我认为直接复制它应该没问题,因为它的唯一用途就是调用stepF

由于

    '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做的就是调用step函数' executeSendEmailLambda'

任何帮助都会非常棒。

更新 感谢我的帮助,我认为我离我们更近一点,但我们又回到了测试传递的方方面面,但是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.

2 个答案:

答案 0 :(得分: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
}
}
}

答案 1 :(得分:0)

我稍微修改了你的代码并用我的一个步骤函数对它进行了测试,这段代码似乎对我有用:)

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