Skip to content

Commit

Permalink
Updated Fetch SSM Parameter Logic
Browse files Browse the repository at this point in the history
Signed-off-by: David Deal <dealako@gmail.com>
  • Loading branch information
dealako committed Aug 19, 2020
1 parent 5a31c7e commit ab73aea
Show file tree
Hide file tree
Showing 4 changed files with 1,013 additions and 1,525 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
}
},
"license": "MIT"
}
}
11 changes: 7 additions & 4 deletions src/app/config/scripts/prefetch-ssm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ const stageEnv = process.env.STAGE_ENV;
const AWS_SSM_JSON_PATH = './src/app/config/cla-env-config.json';

async function prefetchSSM() {
let result = {};
console.log(`Start to fetch SSM values at ${stageEnv}...`);
result = await RetrieveSSMValues(configVarArray, stageEnv, region, profile);
const result = await RetrieveSSMValues(configVarArray, stageEnv, region, profile);
console.log('Fetching completed.');

//test for local
// result['cla-api-url'] = 'http://localhost:5000';
console.log(`Saving configuration to file: ${AWS_SSM_JSON_PATH}...`);
fs.writeFile(AWS_SSM_JSON_PATH, JSON.stringify(result), function (err) {
if (err) throw new Error(`Couldn't save SSM paramters to disk with error ${err}`);
console.log('Fetching completed...');
if (err) {
throw new Error(`Couldn't save SSM parameters to disk with error ${err}`);
}
console.log('Save complete.');
});
}

Expand Down
22 changes: 14 additions & 8 deletions src/app/config/scripts/read-ssm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const AWS = require('aws-sdk');
* @returns {Promise<{ [key:string]: string}>}
*/
async function retrieveSSMValues(variables, stage, region, profile) {
const scopedVariables = variables.map((param) => {
return `cla-${param}-${stage}`;
});
const scopedVariables = variables.map((param) => `cla-${param}-${stage}`);
const result = await requestSSMParameters(scopedVariables, stage, region, profile);
const parameters = result.Parameters;
const error = result.$response.error;
Expand All @@ -24,8 +22,9 @@ async function retrieveSSMValues(variables, stage, region, profile) {
);
}
const scopedParams = createParameterMap(parameters, stage);
let params;
const params = new Map();
Object.keys(scopedParams).forEach((key) => {
// console.log(`processing ${key}`);
const param = scopedParams[key];
key = key.replace('cla-', '');
key = key.replace(`-${stage}`, '');
Expand All @@ -39,23 +38,30 @@ async function retrieveSSMValues(variables, stage, region, profile) {
);
}
});

return params;
}

/**
* Performs a bulk request of the specified SSM parameters.
* @param {string[]} variables
* @param {string} stage
* @param {string} region
* @param {string} profile
*/
function requestSSMParameters(variables, stage, region, profile) {
async function requestSSMParameters(variables, stage, region, profile) {
console.log(`Loading AWS credentials from profile: ${profile}`)
AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile });
const ssm = new AWS.SSM({ region: region });
const ssm = new AWS.SSM({ region });
const ps = {
Names: variables,
WithDecryption: true
};
console.log(AWS.config.credentials);
return ssm.getParameters(ps).promise();
// console.log(AWS.config.credentials);
// console.log(`fetching ssm parameters: ${variables}`);
const response = await ssm.getParameters(ps).promise();
// console.log(response);
return response;
}

/**
Expand Down
Loading

0 comments on commit ab73aea

Please sign in to comment.