Skip to content

Commit

Permalink
get config value from ssm
Browse files Browse the repository at this point in the history
  • Loading branch information
marjisound committed Jan 24, 2024
1 parent 838fa45 commit 8fa3d76
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
29 changes: 29 additions & 0 deletions packages/api/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { findParameter, getParameters } from "./configHelpers";
import { Parameter, SSM } from '@aws-sdk/client-ssm';

interface TranscriptionConfig {
test: string // TODO: This is just the foundation of getting params from SSM
}

const region = process.env['AWS_REGION'];

const ssm = new SSM({
region,
});

export const getConfig = async (): Promise<TranscriptionConfig> => {
const stage = process.env['STAGE'] || 'DEV';
const paramPath = `/${stage}/investigations/transcription-service/`;

const parameters = await getParameters(paramPath, ssm);
const parameterNames = parameters.map((param: Parameter) => {
return param.Name;
});

console.log(`Parameters fetched: ${parameterNames.join(", ")}`);
const testParam = findParameter(parameters, paramPath, "test");

return {
test: testParam
}
};
26 changes: 26 additions & 0 deletions packages/api/src/configHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ export const getParameters = async (
throw err;
}
};

export const findParameter = (
parameters: Parameter[],
paramPath: string,
paramKey: string
): string => {
const parameter = parameters.find(
(param: Parameter) => param.Name === `${paramPath}${paramKey}`
);

return getValueOfParam(paramKey, parameter);
};

export const getValueOfParam = (
paramKey: string,
parameter?: Parameter
): string => {
if (!parameter) {
throw new Error(`The parameter ${paramKey} hasn't been configured`);
}
if (!parameter.Value) {
throw new Error(`The parameter ${paramKey} has no value`);
}
console.log(`Found value of parameter: ${paramKey}`);
return parameter.Value;
};
21 changes: 5 additions & 16 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@ import asyncHandler from 'express-async-handler';
import serverlessExpress from '@codegenie/serverless-express';
import bodyParser from 'body-parser';
import path from 'path';
import { getParameters } from './configHelpers';
import { SSM } from '@aws-sdk/client-ssm';
import { getConfig } from './config';

const region = process.env['AWS_REGION'];

const ssm = new SSM({
region,
});

export const getConfig = async (): Promise<void> => {
const stage = process.env['STAGE'] || 'DEV';
const paramPath = `/${stage}/investigations/transcription-service/`;

const parameters = await getParameters(paramPath, ssm);
const getApp = async () => {
const config = await getConfig();

console.log(parameters);
};
// TODO: This is just for testing. Actual config values should never be logged
console.log(`config value retrieved: ${config.test}`);

const getApp = async () => {
const app = express();
const apiRouter = express.Router();

Expand Down

0 comments on commit 8fa3d76

Please sign in to comment.