Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 349
red888 Avatar asked Mar 05 '18 16:03

red888


People also ask

How do you create a task in ECS?

To create a new task definition (New Amazon ECS console) In the navigation pane, choose Task definitions, Create new task definition. For Task definition family, specify a unique name for the task definition.

What is Task Group?

Task Group “a group of people who are brought together to do a particular job […]” (Cambridge dictionary) A TG is much more focused and specific than a Working Group. A Working Group is a group of experts working together to achieve specified goals.

What is an ECS task?

An Elastic Container Service Task consumes resources to run containers based on the configuration in a task definition. Tasks may or may not be long-lived, and expose several attributes to help you monitor their state.

What is an example of task group?

In a task group, members perform the same functions but they do not share a hierarchical command structure. For example, all the sales staff members together subordinate to the manager of the shop.

What is an ECS Service and a task?

They are part of an ECS Service. The Service and Tasks span 2 Container Instances. The Container Instances are part of a logical group called an ECS Cluster. I did not show a Task Definition in the diagram because a Task is simply an “instance” of Task Definition.

How to create tasks in Amazon ECS console?

Login to Amazon ECS console. Under the task definitions page, click on the Create new Task Definition button. Task definitions start with defining the launch type. Choose launch type and click the Next step button. Tasks will be launched on infra managed by AWS. (serverless) Tasks will be billed on resources being used and usage duration.

How to enable ECS managed tags?

Enable ECS managed tags as ECS tag tasks with cluster and service name which is pretty much easy for identification later. Click on Run Task button. The task will be started and in a couple of minutes, you should be seeing them in RUNNING state.

What is an ECS cluster?

SOME OTHER ECS TERMS! C LUSTER: ECS cluster is a regional grouping of one or more container instances on which you can run task requests.When an instance launches, the ECS-agent software on the server registers the instance to an ECS Cluster. You can choose an existing VPC, or create a new one to spin up your container instances.


1 Answers

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:

blah

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.

"placementConstraints": 
[ 
  { 
    "expression": "task:group == webserver", "type": "memberOf" 
  } 
] 

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

like image 75
MrDuk Avatar answered Sep 22 '22 04:09

MrDuk