Skip to content

Instantly share code, notes, and snippets.

@PopeFelix
Created January 28, 2021 21:32
Show Gist options
  • Save PopeFelix/0625795f5ed033fb7ac770ecae507e80 to your computer and use it in GitHub Desktop.
Save PopeFelix/0625795f5ed033fb7ac770ecae507e80 to your computer and use it in GitHub Desktop.
Take a configuration stored in SSM as a series of hierarchical path / value pairs and return it as an object
// Take a configuration stored in SSM as a series of hierarchical path / value pairs and return it as an object
import {
GetParametersByPathCommand,
GetParametersByPathCommandOutput,
SSMClient,
} from '@aws-sdk/client-ssm
async function getBackupConfig = async (parameterRoot: string) => {
const ssm = new SSMClient({})
let NextToken: string
const config = {}
while (true) {
let res: GetParametersByPathCommandOutput
try {
res = await ssm.send(
new GetParametersByPathCommand({
Path: parameterRoot,
Recursive: true,
NextToken,
})
)
} catch (e) {
e.message = `Failed to get config: ${e}`
throw e
}
for (let param of res.Parameters) {
let { Name, Value } = param
Name = Name.replace(new RegExp(`^${parameterRoot}`), '')
let bits = Name.split('/')
let ptr: unknown = config
for (let i = 0; i < bits.length; i++) {
let bit = bits[i].toLowerCase()
if (i === bits.length - 1) {
ptr[bit] = Value
} else {
if (!ptr[bit]) {
ptr[bit] = {}
}
ptr = ptr[bit]
}
}
}
NextToken = res.NextToken
if (!NextToken) {
break
}
}
return config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment