0
votes

One Lambda is calling another in a synchronous manner, gets some data and continues in processing.

This means, the costs are taken into account for both Lambdas in the call time period.

Is it possible to use AWS Step Functions in this case without breaking the processing into a state machine blocks, which would make the code too complex?

Node.js 8.10 code:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });

exports.handle = async event => {
    try {
        const packageId = event.pathParameters['id'];

        const entry = await packageEntry(packageId);            
        const tags = await tagsForEntry(entry.id);

        return httpResponse(200, { ...entry, tags });
    }
    catch (err) {
        return httpResponse(500, err.message);
    }
}

async function tagsForEntry(entryId) {
    const response = await lambda.invoke({
        FunctionName: process.env.TAGGING_LAMBDA,
        InvocationType: 'RequestResponse',
        Payload: JSON.stringify({
            method: 'GET_TAGS',
            payload: { entryId }
        })
    }).promise();
    const payload = response.Payload ? JSON.parse(response.Payload.toString()) : {};
    return payload.tags || [];
}

async function packageEntry(packageId) {
    // return a package entry from a database
}

function httpResponse(statusCode, data) {
    // return a HTTP response object
}
1

1 Answers

0
votes

Only modification you have to do is to return data from 1st function which required by the 2nd function. Step function will automatically get the output of the first function as the input of the 2nd function.

Also, you don't need to handle the function calls between the lambda functions, state machine will handle that.

Also, there won't be additional cost for the service, other than the lambda execution cost, so your cost remains the same.