最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - aws call a step function from lambda - Stack Overflow

programmeradmin5浏览0评论

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

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?

I've found the code from the tutorial on ;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.

Thanks

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

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

any help would be greatfull thanky

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

};

the logs for this show the following

    
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.

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

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?

I've found the code from the tutorial on https://www.youtube./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.

Thanks

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

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

any help would be greatfull thanky

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

};

the logs for this show the following

    
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.
Share Improve this question edited Feb 28, 2020 at 4:28 Ben T 4,9563 gold badges24 silver badges23 bronze badges asked Dec 7, 2017 at 6:56 JohnJohn 3,94522 gold badges84 silver badges174 bronze badges 2
  • Did you get a solution for this? I tried your update part codes, the lambda execution is success, but it is not calling the step function. – Sreejith Sree Commented Mar 31, 2021 at 12:02
  • take a look here How to call a step funtion from Node.js Lambda function?. I did the exact same thing, invoking a step function from the lambda. – samtoddler Commented Apr 5, 2021 at 9:20
Add a ment  | 

2 Answers 2

Reset to default 1

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

{
"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
}
}
}
发布评论

评论列表(0)

  1. 暂无评论