Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork and Join with Amazon Lambda

I am trying to write a lambda function that triggers many small lambdas

exports.handler = (event, context, callback) => {
   var AWS = require('aws-sdk');

   let noOfPages = 20, currentPage = 1;
   var message = '';
   //let payloadArray = [];
   while(currentPage <= noOfPages){
        message = '{"first_page": '+ currentPage +', "last_page": '+ (currentPage) + ', "file_name" : "something.doc"' +'}';
        console.log(message);
        var params = {
            FunctionName: 'test',
            InvocationType: 'Event',
            LogType: 'Tail',
            Payload: message
        };
        var convLambda = new AWS.Lambda({
            accessKeyId: 'key',
            secretAccessKey: 'secret',
            region: 'us-west-2'
        });

        convLambda.invoke(params, function(err, data) {
            if (err) {
                context.fail(err);
            } else {
                context.succeed('Processed : '+ data);
            }
        })
    currentPage+=1;        
   }
};

This works well and triggers, say 20 lambdas. I would however, like to wait till all the async lambdas are done (fork and join). Is there way to achieve this currently in Amazon Lambda?

like image 931
Jay Avatar asked Jan 28 '26 04:01

Jay


1 Answers

You could consider using AWS Step Functions, which can fork and join Lambda processes. It also shows the relationships in pretty graphs:

Step Functions

like image 178
John Rotenstein Avatar answered Jan 29 '26 23:01

John Rotenstein