|
| 1 | +const AWS = require('aws-sdk'); |
| 2 | +const PromiseBlue = require('bluebird'); |
| 3 | +const winston = require('winston'); |
| 4 | + |
| 5 | +const logger = new winston.Logger({ |
| 6 | + transports: [new winston.transports.Console({ |
| 7 | + level: 'info' |
| 8 | + })] |
| 9 | +}); |
| 10 | + |
| 11 | +if (typeof (process.env.AWS_REGION) !== 'string') { |
| 12 | + throw (new TypeError('$AWS_REGION must be defined')); |
| 13 | +} |
| 14 | + |
| 15 | +const stepfunctions = new AWS.StepFunctions({ |
| 16 | + region: process.env.AWS_REGION |
| 17 | +}); |
| 18 | + |
| 19 | +const reg = new RegExp('stateMachine:test-state-machine'); |
| 20 | + |
| 21 | +const removeStateMachines = function (reg) { |
| 22 | + const params = {}; |
| 23 | + return stepfunctions.listStateMachines(params).promise().then(data => { |
| 24 | + const stateMachinesArn = []; |
| 25 | + data.stateMachines.forEach(stm => { |
| 26 | + if (reg.test(stm.stateMachineArn)) { |
| 27 | + stateMachinesArn.push(stm.stateMachineArn); |
| 28 | + } |
| 29 | + }); |
| 30 | + return stateMachinesArn; |
| 31 | + }).then(stateMachinesArn => { |
| 32 | + return PromiseBlue.map(stateMachinesArn, stateMachineArn => { |
| 33 | + const params = { |
| 34 | + stateMachineArn |
| 35 | + }; |
| 36 | + return stepfunctions.describeStateMachine(params).promise().then(data => { |
| 37 | + const definition = JSON.parse(data.definition); |
| 38 | + const activityArn = definition.States.FirstState.Resource; |
| 39 | + const params = { |
| 40 | + activityArn |
| 41 | + }; |
| 42 | + return stepfunctions.deleteActivity(params).promise().then(() => { |
| 43 | + logger.info('Activity ', params.activityArn, ' was deleted'); |
| 44 | + }); |
| 45 | + }).then(() => { |
| 46 | + return stepfunctions.deleteStateMachine(params).promise().then(() => { |
| 47 | + logger.info('StateMachine ', params.stateMachineArn, ' was deleted'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }, {concurrency: 1}); |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +removeStateMachines(reg).catch(err => { |
| 55 | + logger.error(err); |
| 56 | +}); |
0 commit comments