问题
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