Amazon ECS Service – Task Definition Not Updating

amazon ec2amazon-ecsamazon-web-servicesdocker

In the ecs cluster, I have a service running with 2 ec2 instances. And i update the task definition to take the new docker image. But the old task definition is still running even though there is a new task definition.

I have used the following commands to update the task definition and service.

aws ecs register-task-definition --family service90-task --cli-input-json file://service90-task.json

aws ecs update-service --cluster service90-cluster --service service90-service --desired-count 0

TASK_REVISION=`aws ecs describe-task-definition --task-definition service90-task | egrep "revision" | tr "/" " " | awk '{print $2}' | sed 's/"$//'`

aws ecs update-service --cluster service90-cluster --service service90-service --task-definition service90-task:${TASK_REVISION} --desired-count 2

I tried several times but can't figure out where i went wrong. I want to get the ecs service to run the new task definition instead of the old one.

Best Answer

As I found out later on, the reason for not updating the task is that the desired count is set to 2 and there are only 2 EC2 instances available. So the ECS agent tries to retain the desired count even though the task has been updated.

Solution - Have one extra EC2 instance (in this case 3 EC2 instances). Or have one extra instance than the preferred number of tasks.

In this way the new task definition can run on the extra instance. After it is stabilized on the extra EC2 instance, the ECS agent will drain the connection on the other two instances for the old task definition, while the load-balancer redirect the traffic to the updated instance. The ECS agent replaces the old task definition with the new ones. And then it maintains the desired count as 2.