Async Lambda example sending task succeeded status back to step function

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 task token.
  const taskToken = event.taskToken;

  // Create the send task success request.
  const sendTaskSuccessRequest = {
    taskToken: taskToken,
    output: JSON.stringify(event.output),
  };

  // Send the task success request.
  await stepFunctions.sendTaskSuccess(sendTaskSuccessRequest);
};

This code uses the aws-sdk Node.js module to interact with AWS services. The first line imports the module. The next line creates a new Step Functions client. The taskToken variable is set to the task token that was passed to the Lambda function.

The sendTaskSuccessRequest object is created with the taskToken and output properties. The output property is the output of the Lambda function.

The sendTaskSuccess method is then used to send the SendTaskSuccess request to Step Functions.

This is just a simple example of how to send a SendTaskSuccess request to Step Functions from a Lambda function. You can customize the code to fit your specific needs.

Here are some additional things to keep in mind when sending a SendTaskSuccess request to Step Functions:

You need to make sure that the Step Functions client is in the same region as the Lambda function.
You need to have the appropriate permissions to send a SendTaskSuccess request.
You need to handle errors that may occur during the request. For example, the Step Functions client may be unavailable or the request may fail.

Lambda callback example from AWS: (input from SQS)

console.log('Loading function');
const aws = require('aws-sdk');

exports.lambda_handler = (event, context, callback) => {
    const stepfunctions = new aws.StepFunctions();

    for (const record of event.Records) {
        const messageBody = JSON.parse(record.body);
        const taskToken = messageBody.TaskToken;

        const params = {
            output: "\"Callback task completed successfully.\"",
            taskToken: taskToken
        };

        console.log(`Calling Step Functions to complete callback task with params ${JSON.stringify(params)}`);

        stepfunctions.sendTaskSuccess(params, (err, data) => {
            if (err) {
                console.error(err.message);
                callback(err.message);
                return;
            }
            console.log(data);
            callback(null);
        });
    }
};

Related video