I created a node module to retrieve multiple parameters as the parameterName-parameterValue Json object from AWS Parameter Store. It is called aws-ssm-parameters and is available from Npm. The module is namespaced. So, you need to add the namespace @mdhnpm when you install it.
npm i @mdhnpm/aws-ssm-parameters
The main motivation for creating this module is to use it in lambda functions where I add parameter names in environment variables and use aws-sdk to retrieve them. I find this pattern cleaner than simply adding real database passwords or API credentials to environment variables. It creates an additional layer of security and rotating credentials don’t require re-deployment of lambda functions (or worse editing credentials directly within the lambda console). I just didn’t want to repeat an equivalent logic again and again altogether the lambda functions.
Node module to retrieve multiple parameters
The great thing about npm is that the module doesn’t got to be big. It are often namespaced and your private module feels a touch more personal although it’s accessible to the general public . It also allows you to make a personal repo for an organisation. this is often an excellent thanks to share node modules within the enterprise.
My module is super simple. It is based on aws-sdk. It has a method getParameters() which retrieves all the parameters at once. What I did was creating a thin wrapper to make it usable with one line.
Here is the source code.
'use strict'; const AWS = require('aws-sdk'); let ssm; const getParameters = async (parameterNames, region, apiVersion='2014-11-06') => { if (!ssm) { ssm = new AWS.SSM({apiVersion: apiVersion, region: region}); } const params = { Names: parameterNames, WithDecryption: true }; try { const parameters = await ssm.getParameters(params).promise(); return formatParameters(parameters); } catch (e) { return e; } }; const formatParameters = (parameters) => { return parameters.Parameters.reduce((object, param) => { return { ...object, [param.Name]: param.Value }; }, {}); }; module.exports = { getParameters };
The usage is simple. My recommendation is to use ES7 async/await pattern. It makes code nicer.
'use strict'; const { getParameters } = require('@mdhnpm/aws-ssm-parameters'); // Input is an array of parameter names const parameterNames = [ 'my.db.endpoint', 'my.db.name', 'my.db.password', 'my.db.username' ]; const getParams = async () => { // Argument is (1) an array of parameter name & (2) AWS region const parameters = await getParameters(parameterNames, 'ap-southeast-2'); console.log(parameters); } // The module creates a Json object // with parameter name as key and actual value as value // Decrypt option is enabled. It works on secure string. // { // my.db.endpoint:'endpoint-url', // my.db.name: 'database-name', // my.db.password: 'database-pw', // my.db.usernme: 'database-username' // } getParams()
There are more …
Now the sdk has a method to retrieve a single parameter getParameter().
'use strict'; const getParameter = (parameterName, ssm) => { const value = ssm.getParameter({'Name': parameterName, WithDecryption: true}).promise(); return value.then( (data) =>{ return data.Parameter.Value; }) .catch((err) => { return err; }); }; module.exports = { getParameter };
Then, we can loop through them and use reduce() function to create home-made getParameters function. Isn’t it fun?
'use strict'; const AWS = require('aws-sdk'); const { getParameter } = require('../play/get-parameter'); let ssm; const getParameters = (parameterNames, region, getParameterFunc=getParameter) => { if (!ssm) { ssm = new AWS.SSM({apiVersion: '2014-11-06', region: region}); } return Promise.all(parameterNames .map(parameterName => getParameterFunc(parameterName, ssm))) .then(values => parameterNames .reduce((obj, paramName, index) => ({ ...obj, [paramName]: values[index]}), {})) .catch(err => err); }; module.exports = { getParameters };
So Guyz it was the Node Module to Retrieve Multiple Parameters from AWS Parameter Store. Please let me know in the comment box if you find some difficulties over it. and you can also contact us at info@appfinz.com