0
votes

I am attempting to create an orchestration AWS lambda that calls two other AWS lambdas. These two other AWS lambdas can be invoked in their own right but in certain cases, there is a need for orchestration.

My orchestration lambda looks like this:

module.exports.orchestration = async (event, context, callback) => {
    const lambdaAPromise = lambdaA();
    const lambdaBPromise = lambdaB();

    const lambdaAResponse = await lambdaAPromise;
    const lambdaBResponse = await lambdaBPromise;

    if (lambdaAResponse && lambdaBResponse) {
        console.log(
            "Both streams responsed with: ",
            lambdaAResponse,
            lambdaBResponse
        );

        var orchestrationResponse = [];

        orchestrationResponse.push(lambdaAResponse);
        orchestrationResponse.push(lambdaBResponse);

        const orchestrationSucceeded = {
            statusCode: 200,
            isBase64Encoded: false,
            body: orchestrationResponse
        };

        callback(null, orchestrationSucceeded);
    } else {
        console.log(
            "At least one stream not responded: ",
            lambdaAResponse,
            lambdaBResponse
        );

        const orchestrationFailed = {
            statusCode: 400,
            isBase64Encoded: false,
            body: someresponse
        };

        callback(null, orchestrationFailed);
    }
};

function lambdaA() {
var payload = {
    groupNumber: requestBody.groupNumber
};

var params = {
    FunctionName: process.env.CCE_FUNCTION_NAME,
    InvocationType: "RequestResponse",
    LogType: "Tail",
    Payload: JSON.stringify(payload)
};

return lambda
    .invoke(params)
    .promise()
    .then(({ Payload }) => {
        var payload = JSON.parse(Payload);

        return payload.body;
    });
}

function lambdaB() {
var payload = {
    groupNumber: requestBody.groupNumber
};

var params = {
    FunctionName: process.env.CCE_FUNCTION_NAME,
    InvocationType: "RequestResponse",
    LogType: "Tail",
    Payload: JSON.stringify(payload)
};

return lambda
    .invoke(params)
    .promise()
    .then(({ Payload }) => {
        var payload = JSON.parse(Payload);

        return payload.body;
    });
}

Both lambdaA and lambdaB functions look like this:

module.exports.lambdaA = (event) => {
    return new Promise((resolve) => {
        do something ...

        resolve(boolean value);
    });
};

My issue was that the await function did not occur as I had an incorrect signature (was still using callback rather than promise). I have updated the code snippets that are now working correctly.

1
you should really consider step functions when orchestrating lambda calls - LostJon
Yes, that is one approach but I still want to understand why what I coded is not working as I thought it should. - Perry Hoekstra
well, from the first code snippet (no clue what role the second snippet is doing), your lambdaA and lambdaB use callbacks, hence you cannot await them - LostJon
@LostJon, that was the issue, I had an invalid signature that was still invoking callbacks. Thank you for catching that error on my part. - Perry Hoekstra

1 Answers

0
votes

Just wrapping up from comment:

Issue was lambdaA and lambdaB use callbacks, hence you cannot await them. [From Snippet #1]