Ad

Chained AWS Lambda Calls In An Orchestration

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.

Ad

Answer

Just wrapping up from comment:

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

Ad
source: stackoverflow.com
Ad