lambee
Version:
A tool to help developer work with AWS Lambda.
74 lines (57 loc) • 2.33 kB
JavaScript
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 = `secret-list-${awsProfile}-${awsRegion}-${verbose}`
if (useCache && cache.get(cacheKey)) return cache.get(cacheKey)
const fullResult = []
let nextMarker
while (nextMarker !== 'done') {
const data = await listParams(nextMarker, awsProfile, awsRegion, debug)
if (data.Parameters) fullResult.push(...data.Parameters)
nextMarker = data.NextMarker ? data.NextMarker : 'done'
}
const result = verbose ?
JSON.stringify(fullResult, null, 4) :
fullResult.map(it => `${it.Name}: ${it.Description}`).sort().join('\n')
cache.put(cacheKey, result)
return result
}
/**
* Runs the AWS SDK describeParameters function and returns it as a promise.
*
* @param nextMarker Support for paging through a large result set.
* @param verbose Turns on debug logging.
*/
function listParams(nextMarker, awsProfile, awsRegion, debug) {
return new Promise((resolve, reject) => {
debug && console.debug(`listSecrets(${nextMarker})`)
const ssm = new AWS.SSM({
apiVersion: '2014-11-06',
region: awsRegion,
credentials: awsProfile ? new AWS.SharedIniFileCredentials({profile: awsProfile}) : undefined
})
debug && console.debug('AWS Config', ssm.config)
const options = {
MaxResults: 50,
NextToken: nextMarker
}
ssm.describeParameters(options, (err, data) => {
debug && console.debug('describeParameters()', {err, data})
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}