Skip to content

Instantly share code, notes, and snippets.

@Rud5G
Forked from rnag/parameterStore.ts
Created August 3, 2023 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rud5G/043075f17b4418ca4356c9cc19b117de to your computer and use it in GitHub Desktop.
Save Rud5G/043075f17b4418ca4356c9cc19b117de to your computer and use it in GitHub Desktop.
Create or obtain values from the AWS Parameter store with Typescript/node
import { SSM } from 'aws-sdk';
/**
* Get the value for a parameter in SSM Parameter Store. By default, decrypt
* the value as we assume it is stored as a "SecretString"
*
* Ref: https://gist.github.com/cbschuld/938190f81d00934f7a158ff223fb5e02
*
* @param ssm The SSM client
* @param name Name of the parameter
* @param decrypt True to decrypt a secret parameter (default is true)
* @returns The parameter value
*/
export const getParamValue = async (
ssm: SSM,
name: string,
decrypt: boolean = true
): Promise<string> => {
const params: SSM.GetParameterRequest = {
Name: name,
WithDecryption: decrypt,
};
const res = await ssm.getParameter(params).promise();
return res.Parameter?.Value!;
};
/**
* Create a parameter in SSM Parameter Store.
*
* @param ssm The SSM client
* @param name Name of the parameter
* @param value Value of the parameter
* @param type Type of the parameter, defaults to "SecretString" so the value
* is encrypted.
* @returns The parameter value
*/
export const createParamValue = (
ssm: SSM,
name: string,
value: string,
type: string = 'SecureString'
) => {
const params: SSM.PutParameterRequest = {
Type: type,
Name: name,
Value: value,
};
ssm.putParameter(params).send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment