Search code examples
amazon-web-servicesamazon-ecs

ECS Cluster: Getting tasks in different services to run on different EC2 instances


I have an ECS Cluster on which i have configured 3 different Services (let's say svc1, svc2, svc3).

I would like do make sure the tasks/containers of each service are run on EC2 instances that are not running tasks/containers of the other two services.

I have read the documentation about task placement contraints and about task groups and I understood is that when a task created by a service it automatically gets a task group with the same name of the service.

So what I configured on each of my 3 services (in the service itself, not in the task definition) is the following placement constraint:

Type: memberOf Expression: task:group != service:svc2 and task:group != service:svc3

On each service I would exclude the other two.

This configuration doesn't seem to work though as I get two behaviours:

  • When I turn on my EC2 instances and all the services try to start together none of them are able to start tasks and give the following error:

service svc1 was unable to place a task because no container instance met all of its requirements. The closest matching container-instance 3d9d2d497fc9489ab26551475f4d7025 encountered error "MemberOf placement constraint unsatisfied.". For more information, see the Troubleshooting section of the Amazon ECS Developer Guide.

  • Then I tested removing the placement contraints on 2 services and keeping it only on 1. When I do a new deployment of the tasks (done with CodeDeploy, with Blue/Green deployment) they just end up running all together on the same container instance (I have more than 1 container instances up and configured in ECS)

I'm really not understanding how this is supposed to work and if I'm missing something or it's just bad documentation.


Solution

  • From official docs, the way of specifying a negative match is not(task:group == database) -> in your case it would be not(task:group == service:svcX).

    Hope it helps!