Created
March 13, 2023 19:20
-
-
Save gtchakama/84f270399d734197ba179c04fb760589 to your computer and use it in GitHub Desktop.
Recursive JavaScript Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk') | |
const lambda = new AWS.Lambda() | |
const sleep = (milliseconds) => { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
} | |
const recursionLimit = 5 | |
exports.handler = async(event) => { | |
await sleep(2000) | |
const iteration = event.iteration || 1 | |
console.log('Iteration: ' + iteration + '\n') | |
if (iteration <= recursionLimit) { | |
var params = { | |
FunctionName: 'RecursiveLambda', | |
InvocationType: 'Event', | |
Payload: JSON.stringify({ "iteration": iteration + 1 }) | |
}; | |
return new Promise(function(resolve, reject) { | |
lambda.invoke(params, function(err, data) { | |
if (err) { | |
reject(err) | |
} else { | |
resolve(data) | |
} | |
}) | |
}); | |
} | |
return 'Finished executing on iteration ' + iteration | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment