Skip to content

Instantly share code, notes, and snippets.

@gtchakama
Created March 13, 2023 19:20
Show Gist options
  • Save gtchakama/84f270399d734197ba179c04fb760589 to your computer and use it in GitHub Desktop.
Save gtchakama/84f270399d734197ba179c04fb760589 to your computer and use it in GitHub Desktop.
Recursive JavaScript Lambda
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