Francesco
Francesco

Reputation: 977

Unable to run AWS StepFunctions inside an AWS Lambda

I'm trying to start the execution of an AWS StepFunction from inside an AWS Lambda, but I receive null as result, with no error message. The StepFunctions here is an Express State Machine, so I use the method startSyncExecution(params = {}, callback), as pointed in the docs.

Here is the code of the Lambda:

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

exports.handler = async(event, context, callback) => {
    var params = {
        stateMachineArn: "arn:aws:states:us-east-1:[AccountID]:stateMachine:BookLectureStateMachine",
        input: JSON.stringify(event),
        name: "test-from-lambda"
    }
    
    var stepfunctions = new AWS.StepFunctions();

    console.log("Everything okay") //This one is logged

    stepfunctions.startSyncExecution(params, function(err, data) {

        console.log("This log isn't shown"); //This one isn't logged

        if (err) {
            callback(null, {
                statusCode: 400,
                body: err,
                headers: {
                    'Access-Control-Allow-Origin': '*'
                }
            })
        } else {
            callback(null, {
                statusCode: 200,
                body: 'Lecture booked',
                headers: {
                    'Access-Control-Allow-Origin': '*'
                }
            })
        }
    });
};

The response is null, nothing else.

I've checked the permissions, and the Lambda has Full Access to the Step Functions.

Any idea on how to solve it?

UPDATE

  1. I think the StepFunction is not executed, since the logs are empty.
  2. I increased the Lambda timeout to 1min in order to avoid timeout scenarios. The billed duration is about half a second

Upvotes: 1

Views: 1134

Answers (1)

Mark B
Mark B

Reputation: 200436

I believe it may have something to do with the mix of callbacks and async. Since you aren't using await in your handler anywhere, I would try removing async from the handler.

Either that or you could try changing the code to:

var data = await stepfunctions.startSyncExecution(params).promise()

Upvotes: 2

Related Questions