Skip to content

Instantly share code, notes, and snippets.

@tkgregory tkgregory/index.js

Last active May 9, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.