Assign value of callback to constant

447 views Asked by At

For a tool we are trying to use we have a very small program in node.js (I have 0 knowledge of node.js). This tool requires some configs, we have been asked by security to not have those configs as environment variables but to get them from some kind of store so what we used to have:

const loggerConfig = {
    url: process.env.SPLUNK_HEC_URL,
    token: process.env.SPLUNK_HEC_TOKEN,
};

const SplunkLogger = require('./lib/mysplunklogger');
const zlib = require('zlib');

const logger = new SplunkLogger(loggerConfig);

exports.handler = (event, context, callback) => {
 console.log('Received event:', JSON.stringify(event, null, 2));
 // Do stuff
}

Is no longer allowed, it needs to get in with a method (from the aws parameter store) and later on we would like to still use this loggerConfig variable the same as we had. What I've tried is the following but I believe because of the asynchronous nature of javascript this is never going to work. I don't know how to make this work and was hoping someone can help me rewrite this to get the assignment working.

const loggerConfig = {
 url: process.env.SPLUNK_HEC_URL,
 token: getParameterFromSystemManager(process.env.SPLUNK_PARAM)
};

function getParameterFromSystemManager(parameter) {
 const AWS = require('aws-sdk');
 AWS.config.update({ region: process.env.AWS_REGION });
 const SSM = require('aws-sdk/clients/ssm');
 const ssm = new SSM()

 let param;
 ssm.getParameter({Name: parameter,WithDecryption: true}, (err, data) => {
 console.log('error = %o', err);
 console.log('raw data = %o', data);
 param = data;
 });

 return param
}

const SplunkLogger = require('./lib/mysplunklogger');
const zlib = require('zlib');

const logger = new SplunkLogger(loggerConfig);

exports.handler = (event, context, callback) => {
 console.log('Received event:', JSON.stringify(event, null, 2));
 // Do stuff
}
0

There are 0 answers