Created
January 24, 2023 02:25
-
-
Save pwmcintyre/199a9e399afffe840aeee962205a5a18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
/* | |
This will disable all EventBridge rules for a given prefix | |
Steps: | |
- lists all rules for a given prefix | |
- for all rules found, disables it | |
example: | |
AWS_PROFILE=admin AWS_REGION=us-west-2 \ | |
RULE_PREFIX='lambda-export-' \ | |
node ./EventBridge/DisableByPrefix | |
*/ | |
// validate params | |
[ | |
'RULE_PREFIX', | |
].forEach( (name) => { | |
if ( !( name in process.env ) ) { | |
console.error(`required env var not found: ${name}`) | |
process.exit(1) | |
} | |
}); | |
// deps | |
const AWS = require('aws-sdk') | |
// aws clients | |
const eventbridge = new AWS.EventBridge() | |
// list rules | |
const NamePrefix = process.env.RULE_PREFIX | |
eventbridge.listRules({ NamePrefix }).promise() | |
.catch( error => { | |
error.message = `failed to get rules: ${error.message}` | |
throw error | |
}) | |
.then( r => { | |
// disable each rule | |
const results = r.Rules.map( rule => { | |
// disable rule | |
return eventbridge.disableRule({ Name: rule.Name }).promise() | |
.then( _ => rule.Name ) | |
.catch( error => { | |
error.message = `failed to disable rules: ${error.message}` | |
throw error | |
}) | |
}) | |
// wait | |
return Promise.all( results ) | |
}) | |
.then( results => { | |
console.info("disabled rules", { | |
NamePrefix: process.env.RULE_PREFIX, | |
count: results.length, | |
results, | |
}) | |
}) | |
.catch( error => { | |
console.error("failed", { error }) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment