UNPKG

lambee

Version:

A tool to help developer work with AWS Lambda.

73 lines (56 loc) 2.24 kB
const AWS = require('aws-sdk') const cache = require('../../lib/cache') /** * Returns all AWS Lambda function configuration objects. * * @param boolean useCache Will cache results to make things quicker. * @param string awsProfile The profile to use for authentication with AWS. * @param string awsRegion The AWS region to connect to. * @param boolean verbose Prints the full object returned by AWS SDK instead of just the function name. * @param boolean debug Print debug logs to console. */ module.exports = async function list(useCache, awsProfile, awsRegion, verbose, debug) { debug && console.debug('list()') const cacheKey = `fn-list-${awsProfile}-${awsRegion}-${verbose}` if (useCache && cache.get(cacheKey)) return cache.get(cacheKey) const fullResult = [] let nextMarker while (nextMarker !== 'done') { const data = await listFunctions(nextMarker, awsProfile, awsRegion, debug) if (data.Functions) fullResult.push(...data.Functions) nextMarker = data.NextMarker ? data.NextMarker : 'done' } const result = verbose ? JSON.stringify(fullResult, null, 4) : fullResult.map(it => it.FunctionName).sort().join('\n') cache.put(cacheKey, result) return result } /** * Runs the AWS SDK listFunctions function and returns it as a promise. * * @param nextMarker Support for paging through a large result set. * @param verbose Turns on debug logging. */ function listFunctions(nextMarker, awsProfile, awsRegion, debug) { return new Promise((resolve, reject) => { debug && console.debug(`listFunctions(${nextMarker})`) const lambda = new AWS.Lambda({ apiVersion: '2015-03-31', region: awsRegion, credentials: awsProfile ? new AWS.SharedIniFileCredentials({profile: awsProfile}) : undefined }) debug && console.debug('AWS Config', lambda.config) const options = { MaxItems: 50, Marker: nextMarker } lambda.listFunctions(options, (err, data) => { if (err) { reject(err) } else { resolve(data) } }) }) }