0

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
}
ttulka
  • 10,309
  • 7
  • 41
  • 52

1 Answers1

0

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.

Pubudu Jayawardana
  • 2,250
  • 1
  • 13
  • 18
  • I understand step functions so - as a state machine. But this means to break the processing into really tiny blocks, which looks very exhausting here and it brings an additional complexity in communication. It looks like an overkill in this case. I'm asking if step functions are sufficient in this case and how to implement it without this complexity...? – ttulka Feb 28 '19 at 10:31