Last active
January 20, 2021 21:50
-
-
Save mitchellsimoens/628142196d5485dd18e02be0624afd2e to your computer and use it in GitHub Desktop.
Workaround MaxItems not working for AWS Lambda listFunctions method
This file contains 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
// https://github.com/aws/aws-sdk-js/issues/1118 | |
const AWS = require('aws-sdk') | |
const instance = new AWS.Lambda() | |
function listFunctionsReally (lambda, params = {}) { | |
return new Promise((resolve, reject) => { | |
const req = lambda.listFunctions(params) | |
const results = [] | |
const { MaxItems = 50 } = params | |
// returning `false` will stop `eachPage` from requesting more results | |
req.eachPage((error, data) => { | |
if (error) { | |
reject(error) | |
return false | |
} else if (data) { | |
const length = data.Functions.length | |
for (let i = 0; i < length; i++) { | |
if (results.length >= MaxItems) { | |
// stop at MaxItems if we have achieved it | |
resolve(results) | |
return false | |
} | |
results.push(data.Functions[ i ]) | |
} | |
} else { | |
// data may be `null` if the page that was requested does not exist | |
// this happens when `MaxItems` is more than the number of lambdas | |
// causing a page to be null | |
resolve(results) | |
return false | |
} | |
}) | |
}) | |
} | |
listFunctionsReally(instance, { MaxItems: 100 }) | |
.then(console.log, console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment