You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
While task definitions tell ECS what to run, services tell ECS how many to run and how to keep them running. An ECS service is a long-running configuration that maintains a specified number of task instances, replaces failed tasks, and integrates with load balancers and auto-scaling. This lesson covers services, scheduling strategies, deployments, and auto-scaling.
An ECS service ensures that a desired number of tasks are running at all times. If a task fails, crashes, or is stopped, the service scheduler automatically launches a replacement.
Service: my-web-app (desired count: 3)
+--------+ +--------+ +--------+
| Task 1 | | Task 2 | | Task 3 |
| (v1.0) | | (v1.0) | | (v1.0) |
+--------+ +--------+ +--------+
↑ ↑ ↑
└─────────────┼─────────────┘
|
Service Scheduler
"Keep 3 running"
| Property | Description |
|---|---|
| Desired count | The number of task instances the service should maintain |
| Task definition | The blueprint used to launch tasks (including the revision number) |
| Launch type / capacity provider | Where tasks run (Fargate, EC2, or a mix) |
| Deployment configuration | How updates are rolled out (minimum healthy, maximum percent) |
| Load balancer | Distribute traffic across task instances |
| Service discovery | Register tasks with AWS Cloud Map for DNS-based discovery |
aws ecs create-service \
--cluster my-app-cluster \
--service-name my-web-app \
--task-definition my-web-app:3 \
--desired-count 3 \
--launch-type FARGATE \
--network-configuration '{
"awsvpcConfiguration": {
"subnets": ["subnet-0123456789abcdef0", "subnet-0abcdef1234567890"],
"securityGroups": ["sg-0123456789abcdef0"],
"assignPublicIp": "ENABLED"
}
}' \
--load-balancers '[{
"targetGroupArn": "arn:aws:elasticloadbalancing:eu-west-2:123456789012:targetgroup/my-tg/1234567890",
"containerName": "web",
"containerPort": 3000
}]'
ECS supports two service scheduler types:
The replica strategy maintains a specified number of tasks across your cluster. This is the most common approach for web applications and APIs.
Desired: 3 tasks
Strategy: REPLICA
AZ-a AZ-b AZ-c
+--------+ +--------+ +--------+
| Task 1 | | Task 2 | | Task 3 |
+--------+ +--------+ +--------+
ECS spreads tasks across Availability Zones by default for high availability.
The daemon strategy runs exactly one task per EC2 instance in the cluster. This is useful for monitoring agents, log collectors, or other infrastructure tasks that should run on every host.
Note: Daemon scheduling is only available with the EC2 launch type, not Fargate.
When using EC2 instances, ECS uses placement strategies and placement constraints to decide which instances run your tasks:
| Strategy | Description |
|---|---|
| spread | Distribute tasks evenly across a specified attribute (e.g. attribute:ecs.availability-zone) |
| binpack | Pack tasks onto the fewest instances possible (optimises cost) |
| random | Place tasks randomly |
You can combine strategies. For example, spread across AZs first, then binpack within each AZ:
"placementStrategy": [
{ "type": "spread", "field": "attribute:ecs.availability-zone" },
{ "type": "binpack", "field": "memory" }
]
| Constraint | Description |
|---|---|
| distinctInstance | Each task runs on a different EC2 instance |
| memberOf | Tasks only run on instances matching an expression |
"placementConstraints": [
{ "type": "memberOf", "expression": "attribute:ecs.instance-type =~ t3.*" }
]
When you update a service (e.g. deploy a new image version), ECS performs a rolling update by default.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.