4

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

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.
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. 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. Commented Apr 5, 2021 at 9:20

2 Answers 2

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);
    }
});
};
Sign up to request clarification or add additional context in comments.

4 Comments

Hey Keshav, thanks for taking the time to reply...I have tried what you suggested adding in my region name, and state machine arn. But this fires an error Response:{ "errorMessage": "Handler 'handler' missing on module 'index'"}......So I have added in the missing exports.handler = function(event, context) { const response = { statusCode:200, body: JSON.stringify({ message: 'Hello World!', input: event, }), };};
this seems to work, fires no error on test, but it does not call the step function. Would you have any idea as to why this might not be working?
Hi, sorry for the late reply, but if you just change the handler's name in the serverless.yml file from handler.handler to handler.init, my code should work, you don't need to export another module named handler, because when you do this lambda will only run the code within this module and it won't consider the init module.
I have updated the region and state machine ARN values. But when I execute this I am getting the below error message: { "errorMessage": "2021-03-31T11:53:06.420Z *some keys separated using -* Task timed out after 3.02 seconds" }
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 Comment

the code can be properly indented to give more awesomeness

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.