Skip to content

Instantly share code, notes, and snippets.

@dantheman213
Last active August 19, 2019 17:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantheman213/97c677fbbf4f331495cb0f05cf9d21f1 to your computer and use it in GitHub Desktop.
Save dantheman213/97c677fbbf4f331495cb0f05cf9d21f1 to your computer and use it in GitHub Desktop.
Quickly find AWS SSM params by partial search, use custom AWS profiles, get an interactive result to query, and get a AWS URL to view and edit target
#!/usr/bin/env node
//
// Quickly find AWS SSM params by partial search, use custom AWS profiles, get an interactive
// result to query, and get a AWS URL to view and edit target.
//
// Requires AWS CLI, NodeJS v10+ and Python 2/3
//
// Before running for first time do:
// npm install inquirer
const util = require('util');
const exec = util.promisify(require('child_process').exec);
class Application {
constructor() {
this.awsProfile = false;
this.search = false;
this.args = process.argv.slice(2);
this.checkDependants();
this.processArgs();
}
processArgs() {
if (this.args.length > 0) {
this.search = this.args[0];
if (typeof this.args[1] !== 'undefined') {
this.awsProfile = this.args[1];
}
(async () => {
await this.checkDependants();
await this.runSearch();
})();
} else {
this.help();
}
}
help() {
console.log('ssm-find-param <search query> [optional aws profile]');
console.log('e.g. ssm-find-param dburl preprod');
}
async checkDependants() {
await exec('npm list inquirer || npm install inquirer');
}
async runSearch() {
const { stdout } = await exec(`aws ssm describe-parameters ${this.awsProfile ? '--profile ' + this.awsProfile : ''} | grep '"Name":' | grep -i ${this.search} | awk '{print $2}'`, { shell: true });
const results = stdout.replace(/'/g, '').replace(/"/g, '').replace(/,/g, '').split('\n').filter(elem => elem !== '');
if (results.length > 0) {
console.log(`Found ${results.length} items!`);
this.prompt(results);
} else {
console.log('NO RESULTS WERE FOUND!');
}
}
prompt(results) {
const inquirer = require('inquirer');
inquirer
.prompt([{
type: 'list',
name: 'key',
message: 'Which SSM Parameter do you need?',
choices: results,
}])
.then(answers => {
const url = this.generateAwsAssetUrl(answers.key);
console.log(`Access this parameter at:\n\n${url}\n`);
exec(`python -m webbrowser ${url}`);
});
}
generateAwsAssetUrl(ssmParam) {
const url = `https://console.aws.amazon.com/systems-manager/parameters/${encodeURIComponent(ssmParam).replace(/%2F/g, '%252F')}/description`;
return url;
}
}
new Application();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment