Reputation: 595
I'm currently calling an RDS DB to get a list of items, then I loop through these items to then make a POST call for each item. My question is:
Is it possible to loop through this list and call another Lambda function for each item in list. Ideally, this would not wait for a response but would continue calling the others. Each method called would continue running till they are completed. Please note this is based on NodeJs 6.10
Here is the structure:
var pg = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL,
debug: true
});
// Internal
const time = require('./lib/time');
const response = require('./lib/response');
const helpers = require('./lib/helpers');
module.exports.createBatch = (event, context, cb) => {
// Lambda settings
context.callbackWaitsForEmptyEventLoop = false;
const now = time.getCurrentTime();
pg('table')
.join('table')
.select('*')
.then(function(rows) {
return rows;
})
.then(function(rows) {
console.log( 'rows: ' + rows );
let count = 0;
if (!_.isEmpty(rows)) {
for(let row of rows) {
// ****
// CALL OTHER LAMBDA FUNCTION HERE
// ****
axios.post(row)
.then(function (res) {
// ****
// MOVE THIS POST CALL INTO ANOTHER LAMBDA FUNCTION
// ****
})
}
}
})
Upvotes: 0
Views: 2185
Reputation: 1593
What you suggest is suboptimal for a couple of reasons. Namely
While this synchronous flow may work within a single application on a server, it introduces several avoidable problems in a distributed serverless architecture:
Cost: with Lambda, you pay for the duration of an invocation. In this example, while the Create invoice functions runs, two other functions are also running in a wait state, shown in red on the diagram.
Error handling: in nested invocations, error handling can become much more complex. Either errors are thrown to parent functions to handle at the top-level function, or functions require custom handling. For example, an error in Create invoice might require the Process payment function to reverse the charge, or it may instead retry the Create invoice process.
Tight coupling: processing a payment typically takes longer than creating an invoice. In this model, the availability of the entire workflow is limited by the slowest function.
Instead, you may create a distributed map job using AWS step functions. You may read more about it here
Upvotes: 0
Reputation: 8551
Yes you can do it using 'lambda.invoke'. Please see more details here about invoke
Upvotes: 0