Created
June 21, 2016 12:44
-
-
Save dennis-tra/ae0dacbab1576d956bf62b4ab2209f5b to your computer and use it in GitHub Desktop.
AWS Lambda function which registers a new task definition and updates the current service
This file contains 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
exports.handler = function(event, context, callback) { | |
var aws = require('aws-sdk'); | |
aws.config.update({region:'ap-southeast-2'}); | |
var ecs = new aws.ECS(); | |
var taskDefinition = 'nab-property-tool'; | |
var serviceName = 'nab-property-tool'; | |
var clusterName = 'default'; | |
console.log("Creating new Task"); | |
ecs.describeTaskDefinition({taskDefinition: taskDefinition}, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
} else { | |
console.log("Successfully fetched latest task definition"); | |
//console.log(data); // successful response | |
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"); | |
//console.log(data); // successful response | |
var params = { | |
service: serviceName, /* required */ | |
cluster: clusterName, | |
deploymentConfiguration: { | |
maximumPercent: 200, | |
minimumHealthyPercent: 0 | |
}, | |
desiredCount: 1, | |
taskDefinition: taskDefinition + ':' + 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."); | |
//console.log(data); // successful response | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} |
Thank you, from two years in the future. I was just about to write a lambda like this but googled instead on a whim.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After several hours of research in trying to figure out how I can register a new task, update service & register task in my pipe line. I found this. never used lambda before. This helped me get started and solved all my problems. Thank you for your post!