Matthew Pans
Matthew Pans

Reputation: 783

How to call a step funtion from Node.js Lambda function?

I am trying to call a step function from a Node.js lambda function. I tried the solution and updated implementations from this thread.

The solution showing the error response but the updated code showing success response. But the updated code is not calling the step function.

My Code:


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-east-2'
});
console.log('Loading init');
module.exports.init = (event, context, callback) => {
console.log('Loading params');
const params = {
        stateMachineArn: 'ARN of My State Machine',
        // 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);
        }
    });
    };
};

I have added the above code in a Lambda function and Deploy the codes. After that I have used the Test option of the lambda function. Is that the right way of executing a Lambda function? The test result is success, but when I check the state machine, there are no recent executions. Help me to find a solution for this, I am very new to step function. Thanks in advance.

Upvotes: 7

Views: 15371

Answers (2)

sys463
sys463

Reputation: 376

It became easier with aws-sdk v3. Assuming that there's a step function created and the lambda has proper permissions set to trigger step function execution, the handler function can be:

import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn';

export const handler = async (event) => {
  const client = new SFNClient({ region: '<some-region>' });

  const command = new StartExecutionCommand({
    input: JSON.stringify({ data: <blob> }),
    stateMachineArn: '<arn-of-your-step-function>'
  });
  return await client.send(command);
};

Upvotes: 1

samtoddler
samtoddler

Reputation: 9625

Here are the things I did:

  • created the lambda and added the permissions for the step function execution in the lambda role
  • created the step function(just a hello world) of type standard. While creation I opted for ALL Logs which goes to CloudWatch Log Group. Even they are being shown in the step function console as below under the Logging tab.

Below is my code for calling the step function:

var aws = require('aws-sdk')
exports.handler = (event, context, callback) => {
  var params = {
    stateMachineArn: 'arn:aws:states:us-east-1:1234567890:stateMachine:Helloworld',
    input: JSON.stringify({})
  };
  var stepfunctions = new aws.StepFunctions()
  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 Execution Logs

enter image description here

Step Function Execution Logs

enter image description here

Upvotes: 12

Related Questions