Fork and Join with Amazon Lambda

吃可爱长大的小学妹 提交于 2019-12-23 02:20:13

问题


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?


回答1:


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




回答2:


You can use JavaScript Promises to achieve this using the AWS SDK for NodeJS:

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

   let noOfPages = 20, currentPage = 1;
   var message = '';
   //let payloadArray = [];
   var convLambda = new AWS.Lambda({
       accessKeyId: 'key',
       secretAccessKey: 'secret',
       region: 'us-west-2'
   });

   let promises = [];
   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
        };

        promises.push(convLambda.invoke(params).promise());
        currentPage+=1;        
   }
   Promise.all(promises).then((values)=>{
     callback(null, values);
   }).catch((err)=>{
     callback(err);
   });
};


来源:https://stackoverflow.com/questions/41631548/fork-and-join-with-amazon-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!