What is an ECS Task Group and how do I create one?


This is the only doc i have found for Task Group and it doesn’t explain how or where to create one.

I can’t find any docs that adequately explain what a Task Group actually is with an example of how to create and use one. It sound like its a way for a service to run multiple different Task Definitions which would be useful to me.

For example, I added a container to a task definition and the service is balancing multiple instances of it on the cluster. But I have another container I want to deploy along with the first one, but I only want a single instance of it to run. So I can’t add it to the same task definition because I’d be creating multiple instances of it and consuming unnecessary resources. Seems like this is what Task Groups are for.


You are indeed correct, there exists no proper documentation on this (I opened a support case with our AWS team to verify!).

However, all is not lost. A solution to your conundrum does indeed exist, and is a solution we use every day. You don’t have to use the task group, whatever that is (since we don’t actually know yet (AWS engineer is writing up some docs for me, will post them here when I get them)).

All you need though are placement constraints (your same doc), which are easy enough to setup. If you have a launch configuration, you can add something like this to the Advanced > User Data section, so that it gets run during boot (or just add it when launching your instance manually (or if you’re feeling exceptionally hacky, you can logon to your instance and run the commands manually.. for science and stuff)):

echo ECS_INSTANCE_ATTRIBUTES={\"env\": \"prod\",\"primary\": \"app1\",\"secondary\": \"app2\"} >> /etc/ecs/ecs.config

Everything in quotes is arbitrarily defined by you, so use whatever tags and values make sense for your use case. If you go this route, make sure you add the following line to your docker launch command: --env-file=/etc/ecs/ecs.config

So now that you have an instance that’s properly tagged (and make sure it’s only the single instance you want (which means you probably need a dedicated launch configuration for this specific type of instance)), you can go ahead and create your ECS service like you were wanting to do. However, make sure you setup your Task Placement correctly, to match the roles that are now configured for your instances:


So for the example above, this service is configured to only launch this task on instances that are configured for both env==prod and secondary==app2 — since your other two instances aren’t configured for secondary==app2, they’re not allowed to host this task.

It can be confusing at first, and took us a while to get right, but I hope this helps!

Response from AWS Support

I looked into the procedure how to use the Task Groups and here were my findings: – The assumption is that you already have a task group named “databases” if you had existing tasks launched from RunTask/StartTask API.

When you launch a task using the RunTask or StartTask action, you can specify the name of the task group for the task. If you don’t specify a task group for the task, the default name is the family name of the task definition (for example, family:my-task-definition) – So to create a Task Group, either you define a TaskGroup (say webserver) while creating a Task on Task Console or use following command : $ aws ecs run-task --cluster <ecs-cluster> --task-definition taskGroup-td:1 --group webserver

Once created you will notice a Task running with a group: webserver.

Now you can use following placement constraints with the Task Definition to place your tasks only on the containers that are running tasks with this Task Group.

If you try to run a task with above placementConstraint, but you do not have any task running with taskGroup : webserver, you will receive following error: Run tasks failed Reasons : ["memberOf constraint unsatisfied"].

References: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html

Leave a Reply