Created
January 28, 2021 21:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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