Search code examples
amazon-web-servicesdockeramazon-ecsamazon-ecr

Deploying new docker image with AWS ECS


I have an ECS cluster with a service in it that is running a task I have defined. It's just a simple flask server as I'm learning how to use ECS. Now I'm trying to understand how to update my app and have it seamlessly deploy.

  1. I start with the flask server returning Hello, World! (rev=1).
  2. I modify my app.py locally to say Hello, World! (rev=2)
  3. I rebuild the docker image, and push to ECR
  4. Since my image is still named image_name:latest, I can simply update the service and force a new deployment with: aws ecs update-service --force-new-deployment --cluster hello-cluster --service hello-service
  5. My minimum percent is set to 100 and my maximum is set to 200% (using rolling updates), so I'm assuming that a new EC2 instance should be set up while the old one is being shutdown. What I observe (continually refreshing the ELB HTTP endpoint) is that that the rev=? in the message alternates back and forth: (rev=1) then (rev=2) without fail (round robin, not randomly).
  6. Then after a little bit (maybe 30 secs?) the flipping stops and the new message appears: Hello, World! (rev=2)
  7. Throughout this process I've noticed that no more EC2 instances have been started. So all this must have been happening on the same instance.

What is going on here? Is this the correct way to update an application in ECS?


Solution

  • This is the normal behavior and it's linked to how you configured your minimum and maximum healthy percent.

    A minimum healthy percent of 100% means that at every moment there must be at least 1 task running (for a service that should run 1 instance of your task). A maximum healthy percent of 200% means that you don't allow more than 2 tasks running at the same time (again for a service that should run 1 instance of your task). This means that during a service update ECS will first launch a new task (reaching the maximum of 200% and avoiding to go below 100%) and when this new task is considered healthy it will remove the old one (back to 100%). This explains why both tasks are running at the same time for a short period of time (and are load balanced).

    This kind of configuration ensures maximum availability. If you want to avoid this, and can allow a small downtime, you can configure your minimum to 0% and maximum to 100%.

    About your EC2 instances: they represent your "cluster" = the hardware that your service use to launch tasks. The process described above happens on this "fixed" hardware.