Last active
August 12, 2017 16:33
-
-
Save pgqueme/d790f14d0ffb3e905966e88d69e8d4ec to your computer and use it in GitHub Desktop.
Update an AWS ECS Service by updating it's Task Definition. Code works with Environment Variables. Forked from @dennis-tra
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
//Original Gist published by dennis-tra | |
//https://gist.github.com/dennis-tra/ae0dacbab1576d956bf62b4ab2209f5b | |
exports.handler = (event, context, callback) => { | |
var aws = require('aws-sdk'); | |
aws.config.update({region: process.env.ECS_REGION}); | |
var ecs = new aws.ECS(); | |
//Fetching last Task Definition | |
ecs.describeTaskDefinition({ taskDefinition: process.env.ECS_TASK_NAME }, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
else { | |
//Fetched last Task Definition | |
delete data.taskDefinition["taskDefinitionArn"]; | |
delete data.taskDefinition["revision"]; | |
delete data.taskDefinition["status"]; | |
delete data.taskDefinition["requiresAttributes"]; | |
ecs.registerTaskDefinition(data.taskDefinition, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
} else { | |
console.log("Successfully registered new task definition"); | |
var params = { | |
service: process.env.ECS_SERVICE, | |
cluster: process.env.ECS_CLUSTER, | |
deploymentConfiguration: { | |
maximumPercent: 200, | |
minimumHealthyPercent: 0 | |
}, | |
desiredCount: 1, | |
taskDefinition: process.env.ECS_TASK_NAME + ':' + data.taskDefinition.revision | |
}; | |
ecs.updateService(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
} | |
else { | |
console.log("Successfully updated service."); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment