Last active
February 19, 2019 19:48
-
-
Save zoellner/09631a770cefd3fec272268c41cf5ff6 to your computer and use it in GitHub Desktop.
Delete AWS ECS Task Definition for Task Family
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
'use strict'; | |
const AWS = require('aws-sdk'); | |
const _h = require('highland'); | |
const ecs = new AWS.ECS({region: process.env.AWS_REGION || 'us-east-1'}); | |
let familyPrefix = null; | |
if (process.argv.length >= 3){ | |
familyPrefix = process.argv[2]; | |
} else { | |
return(console.error('Error: No familyPrefix provided. Usage: node sctiptName familyPrefix')); | |
} | |
const checkFamily = (familyPrefix, callback) => { | |
ecs.listTaskDefinitionFamilies({status: 'ACTIVE', familyPrefix}, (err, data) => { | |
if (err) {return callback(err);} | |
if (!(data && data.families && data.families.indexOf(familyPrefix) > -1)) { | |
return callback(new Error('taskDefinitionFamily not found')); | |
} | |
return callback(); | |
}); | |
}; | |
const deregisterAll = (familyPrefix, callback) => { | |
let nextToken = null; | |
_h((push, next) => { | |
const params = { | |
familyPrefix, | |
nextToken | |
}; | |
ecs.listTaskDefinitions(params, function(err, data) { | |
if (err) { | |
push(err); | |
return next(); | |
} | |
if (data && data.taskDefinitionArns) { | |
push(null, data.taskDefinitionArns); | |
} | |
if (data.nextToken) { | |
nextToken = data.nextToken; | |
return next(); | |
}; | |
return push(null, _h.nil); //end stream | |
}); | |
}) | |
.flatten() | |
.flatMap(_h.wrapCallback((taskDefinition, callback) => { | |
ecs.deregisterTaskDefinition({taskDefinition}, (err, result) => { | |
if (err) {return callback(err);} | |
console.log(`${taskDefinition} ${(result && result.taskDefinition && result.taskDefinition.status === 'INACTIVE') ? 'deregistered' : 'check again <--' }`); | |
return callback(); | |
}); | |
})) | |
.ratelimit(20, 15000) //just a guess - can't find documentation about actual rate limit | |
.collect() | |
.toCallback(callback); | |
}; | |
checkFamily(familyPrefix, (err) => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
deregisterAll(familyPrefix, (err) => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
console.log('done'); | |
process.exit(); | |
}); | |
}); |
Thanks for this, very useful script!
I found that the rate limiting kicked in when set to these values in eu-west-2. I've set
.ratelimit(20, 30000)
That seems to work for me. I've asked AWS to confirm the rate limiting spec.
@thewire247 did you ever get a response from AWS?
I just tweaked the rate since I had another chance to use the script to delete a few task definitions with hundreds of revisions. 20/15000 worked fine
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs aws-sdk and highland node modules
Install them with
npm i aws-sdk highland
Then run script with familyPrefix as parameter:
node deleteTaskDefinition.js familyPrefix