Created
January 18, 2020 21:55
-
-
Save hendrixroa/cb4dbc9fd5029c7fc7771d3ed30d9583 to your computer and use it in GitHub Desktop.
AWS Secrets manager formatter to get secrets in format json, ideally for backups
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
import AWS = require('aws-sdk'); | |
import fs = require('fs'); | |
import * as _ from 'lodash'; | |
const ssm = new AWS.SSM({ | |
region: process.env.AWS_DEFAULT_REGION || 'us-east-2', | |
}); | |
export class FormatSSM { | |
public describeParameters(): Promise<string[]> { | |
return new Promise((resolve: any, reject: any) => { | |
ssm.describeParameters({ MaxResults: 50 }, (err: any, data: any) => { | |
if (err) { | |
reject(err); | |
} | |
const names: string[] = data.Parameters.map((param: any) => { | |
return param.Name; | |
}); | |
resolve(names); | |
}); | |
}); | |
} | |
public getParameters(names: string[]): Promise<any[]> { | |
const params: any = { | |
Names: names, | |
WithDecryption: true, | |
}; | |
return new Promise((resolve: any, reject: any) => { | |
ssm.getParameters(params, (err: any, data: any) => { | |
if (err) { | |
reject(err); | |
} | |
const hashmap = _.reduce( | |
data.Parameters, | |
(hash: any, value: any) => { | |
const key = value.Name; | |
hash[key] = value.Value; | |
return hash; | |
}, | |
{}, | |
); | |
resolve(hashmap); | |
}); | |
}); | |
} | |
public async formatSSMParameters() { | |
const names: string[] = await this.describeParameters(); | |
const chunkNames = _.chunk(names, 10); | |
let secretString = {}; | |
for (const group of chunkNames) { | |
const values: any = await this.getParameters(group); | |
secretString = { ...secretString, ...values }; | |
} | |
fs.writeFileSync('secrets.json', JSON.stringify(secretString)); | |
} | |
} | |
const format: FormatSSM = new FormatSSM(); | |
format | |
.formatSSMParameters() | |
.then((data: any) => { | |
// tslint:disable-next-line: no-console | |
console.log('Finished'); | |
}) | |
.catch((err: any) => { | |
// tslint:disable-next-line: no-console | |
console.error('error: ', err); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment