It appears that one can either run a Task or a Service based on a Task Definition. What are the differences and similarities between Task and Service? Is there a clue in the fact that one can specify "Task Group" when creating Task but not Service? Are Task and Service hierarchically equal instantiations of Task Definition, or is Service composed of Tasks?
4 Answers
A Task Definition is a collection of 1 or more container configurations. Some Tasks may need only one container, while other Tasks may need 2 or more potentially linked containers running concurrently. The Task definition allows you to specify which Docker image to use, which ports to expose, how much CPU and memory to allot, how to collect logs, and define environment variables.
A Task is created when you run a Task directly, which launches container(s) (defined in the task definition) until they are stopped or exit on their own, at which point they are not replaced automatically. Running Tasks directly is ideal for short-running jobs, perhaps as an example of things that were accomplished via CRON.
A Service is used to guarantee that you always have some number of Tasks running at all times. If a Task's container exits due to an error, or the underlying EC2 instance fails and is replaced, the ECS Service will replace the failed Task. This is why we create Clusters so that the Service has plenty of resources in terms of CPU, Memory and Network ports to use. To us it doesn't really matter which instance Tasks run on so long as they run. A Service configuration references a Task definition. A Service is responsible for creating Tasks.
Services are typically used for long-running applications like web servers. For example, if I deployed my website powered by Node.JS in Oregon (us-west-2) I would want say at least three Tasks running across the three Availability Zones (AZ) for the sake of High-Availability; if one fails I have another two and the failed one will be replaced (read that as self-healing!). Creating a Service is the way to do this. If I had 6 EC2 instances in my cluster, 2 per AZ, the Service will automatically balance Tasks across zones as best it can while also considering CPU, memory, and network resources.
UPDATE:
I'm not sure it helps to think of these things hierarchically.
Another very important point is that a Service can be configured to use a load balancer, so that as it creates the Tasks—that is it launches containers defined in the Task Definition—the Service will automatically register the container's EC2 instance with the load balancer. Tasks cannot be configured to use a load balancer, only Services can.

- 18,137
- 13
- 50
- 91

- 7,511
- 1
- 26
- 26
-
What I don't understand: why when task created I can change values of environment variables but it doesn't seem to be possible for service – Nikolay Klimchuk Oct 07 '17 at 18:23
-
3@NikolayKlimchuk services only manage the tasks - it's the tasks themselves that define and use the envars. – bwobst Dec 31 '17 at 05:28
-
what is a "task group" – red888 Mar 05 '18 at 16:06
-
@NikolayKlimchuk sorry for the late reply. Services are used to *schedule* containers, tasks are used to *define* containers. This is why you use the tasks to control env vars. – talentedmrjones Mar 14 '18 at 19:13
-
1@red888a *task group* is a way to control the placement of containers on instances. When you have a collection of containers defined by a task, the scheduler needs a way of determining *placement*. It essentially asks "what host instances can I place this task (set of containers) on?" A *task group* allows you to control the placement of related containers. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html#task-groups – talentedmrjones Mar 14 '18 at 19:19
-
Question: If i have multiple websites hosted on subdomains of the same website: Should i create 1 service with 1 task for subdomain or 1 service with multiple taks(task per subdomain).Thank you! – Crerem Sep 18 '19 at 14:59
-
If a task is just a configuration, it's weird that the AWS console says "run new task" to create a new task. I guess that's just an oversight though. Edit: It looks like there is now Tasks and also Task Definitions -- so maybe this answer could use an update, not sure. – Josh M. Feb 02 '23 at 18:26
-
Small correction: Tasks can be configured to use a load balancer where the load balancer target group points to the IP address of the task. – nurikabe Jul 04 '23 at 00:53
Beautifully explained in words by @talentedmrjones. Picture below will help you visualize it easily :)

- 2,630
- 29
- 22
-
12If any answer viewer wants to take a deep dive into Amazon ECS, please visit https://www.freecodecamp.org/news/amazon-ecs-terms-and-architecture-807d8c4960fd/. Beautifully explained! – realPK Jun 09 '19 at 03:12
-
This differs to the answer of @xwa130, where an *ECS container instance* is an *EC2 instance* and above/around a service. What exactly is EC2? – Jennifer Kiesel May 16 '22 at 12:14
-
Task Definition:
This is the blueprint describing which Docker containers to run and represents your application. It includes several tasks.
Service:
An instance of Task Definition. It also defines the minimum and maximum Tasks from one Task Definition run at any given time, autoscaling, and load balancing.
ECS Container Instances:
This is an EC2 instance that has Docker and an ECS Container Agent running on it. The Agent takes care of the communication between ECS and the instance, providing the status of running containers and managing running new ones.
Relationship:

- 579
- 4
- 6
-
Thanks for the above answer. I had one doubt, if I initiate 5 tasks from a task definition, would that mean that there are 5 EC2 instances assigned to that service or is there some other configuration which defines the exact number of EC2 instances running behind a service? – mshikher Mar 12 '21 at 22:52
-
@mshikher the ECS container instance has ECS container agent which will coordinate where to run the tasks. But of course you could assign the number of EC2 instances you want to run. – xwa130 Mar 16 '21 at 06:53
-
1Ref : https://www.freecodecamp.org/news/amazon-ecs-terms-and-architecture-807d8c4960fd/ – Vivek Doshi Sep 10 '22 at 19:40
Task Definition: (It is a configuration) A task definition is a blueprint for your application and describes one or more containers through attributes. Some attributes are configured at the task level, but the majority of attributes are configured per container.
You are defining your containers and how to launch them via Task definitions. You describe how containers should be provisioned (link to ECR’s saved container images, CPU units, Memory, Container ports to expose, network type).
Task definitions specify the container information for your application (web), such as how many containers are part of your task, what resources they will use, how they interact with each other and which host port they will use. It can be of Fargate and EC2 type.

- 1,373
- 3
- 30
- 62

- 1
- 1
-
hello, downvoters! Do you mind explaining the reason for the downvote? – AATHITH RAJENDRAN Jul 13 '23 at 05:37