briareus
Version:
Briareus assists with Feature Branch deploys to ECS
49 lines (37 loc) • 1.48 kB
JavaScript
const AWS = require('aws-sdk');
const async = require('async');
const _ = require('lodash');
let action = module.exports = function (pipeline, payload, cb) {
action.deleteSsmParameters(_.values(payload.assets.ssmParameters), cb);
}
action.deleteSsmParameters = function (ssmParameters, cb) {
const ssm = new AWS.SSM();
if (ssmParameters.length === 0) return cb(null, []);
// AWS only allows 10 Paramaters to be deleted at a time
const ssmParametersPerBatch = 10;
const numberOfBatches = Math.ceil(ssmParameters.length / ssmParametersPerBatch);
const batches = [];
for (let i = 0; i < numberOfBatches; i++) {
batches.push(ssmParameters.slice((i * ssmParametersPerBatch), ((i + 1) * ssmParametersPerBatch)))
}
async.map(batches, (batch, next) => {
const params = {
Names: _.map(batch, 'path')
};
// AWS won't lets us run a delete operation when there is nothing to delete.. seems fair
if (params.Names.length === 0) return next(null, []);
ssm.deleteParameters(params, (err, data) => {
if (err) return next(err);
let ops = _.map(_.values(batch), (ssmParameter) => {
return { op: 'remove', path: `/assets/ssmParameters/${ssmParameter.hashedId}` };
});
next(null, ops);
});
}, (err, ops) => {
if (err) return cb(err);
cb(err, _.flatten(ops));
});
}
action.waiting = 'Destroying SSM Parameter Secrets';
action.done = 'SSM Parameter Secrets have been destroyed';