Lambda invoke step function nodejs example

const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
  // Get the Step Functions client.
  const stepFunctions = new AWS.StepFunctions({
    region: process.env.AWS_REGION,
  });

  // Get the state machine ARN.
  const stateMachineArn = process.env.STATE_MACHINE_ARN;

  // Create the start execution request.
  const startExecutionRequest = {
    stateMachineArn: stateMachineArn,
    input: JSON.stringify(event),
  };

  // Start the execution.
  const startExecutionResult = await stepFunctions.startExecution(startExecutionRequest);

  // Get the execution ARN.
  const executionArn = startExecutionResult.executionArn;

  // Return the execution ARN.
  return {
    statusCode: 200,
    body: JSON.stringify({ executionArn: executionArn }),
  };
};

In this example, the parameter1 event variable is passed as a parameter to the Step Functions state machine. The state machine can then use this parameter to perform its tasks.

Here is an explanation of the code:

  • The AWS module is imported to get the AWS SDK for Node.js.
  • The exports.handler function is the main function of the Lambda function. It gets the Step Functions client, the state machine ARN, the input data, and then invokes the Step Functions state machine.
  • The input_data object contains the data that will be passed to the Step Functions state machine. The parameter1 key-value pair is the parameter that is being passed to the state machine.
  • The client.start_execution method is used to invoke the Step Functions state machine. The stateMachineArn and input parameters are passed to this method.
  • The response object is returned from the client.start_execution method. This object contains the information about the execution of the Step Functions state machine.