Skip to content

Instantly share code, notes, and snippets.

@ChaseIngebritson
Last active October 14, 2019 20:29
Show Gist options
  • Save ChaseIngebritson/ba7b7f3e872e57476ab8a5ac023bd590 to your computer and use it in GitHub Desktop.
Save ChaseIngebritson/ba7b7f3e872e57476ab8a5ac023bd590 to your computer and use it in GitHub Desktop.
Loads parameters from the AWS parameter store . Binds the final hierarchy level in the provided name to a corresponding environment variable.
const AWS = require('aws-sdk')
require('dotenv').config()
AWS.config.update({ region: 'us-east-1' });
const ssm = new AWS.SSM()
const params = [
'/WebApp/ead/NodeJS/CampusMap/MONGODB_URI',
'/WebApp/ead/NodeJS/CampusMap/S3_BUCKET'
]
console.log('Loading parameter store...')
loadParameters(params).then(() => {
console.log('Parameter store loaded.')
})
function loadParameters (params) {
const query = {
'Names': params,
'WithDecryption': true
}
return ssm.getParameters(query, (error, data) => {
if (error) throw new Error(error)
data.Parameters.forEach(param => {
const nameSections = param.Name.split('/')
const name = nameSections[nameSections.length - 1]
if (!process.env[name]) process.env[name] = param.Value
})
}).promise()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment