Services

A service keeps a desired number of tasks running and integrates with load balancers.

A service is a long-running controller. You tell it a desired count, and it launches tasks, watches their health, and replaces any that fail.

What services add over bare tasks

  • Self-healing: crashed tasks are automatically replaced.
  • Load balancer registration for web traffic.
  • Rolling and blue/green deployments.
  • Auto scaling based on metrics.

Use a service for anything that should stay up. Use a bare task for one-off or batch jobs.

Example

Example · bash
aws ecs create-service \
  --cluster prod \
  --service-name web \
  --task-definition web:1 \
  --desired-count 3 \
  --launch-type FARGATE \
  --network-configuration 'awsvpcConfiguration={subnets=[subnet-abc,subnet-def],securityGroups=[sg-123]}'

When to use it

  • A team creates an ECS service linked to an ALB target group so HTTP traffic is distributed across all healthy task instances.
  • An on-call engineer updates the desired count of a service to zero during a late-night incident to immediately stop all tasks.
  • A release engineer triggers a service update with a new task definition revision to perform a rolling deployment with no downtime.

More examples

Create Service with ALB Target Group

Creates a Fargate service with three tasks registered behind an ALB target group for load-balanced HTTP traffic.

Example · bash
aws ecs create-service \
  --cluster prod-cluster \
  --service-name api-svc \
  --task-definition api:5 \
  --desired-count 3 \
  --launch-type FARGATE \
  --load-balancers targetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/api-tg/abc123,containerName=api,containerPort=8080 \
  --network-configuration 'awsvpcConfiguration={subnets=[subnet-a,subnet-b],securityGroups=[sg-api],assignPublicIp=DISABLED}'

Update a Service to New Revision

Triggers a rolling deployment by pointing the service at a new task definition revision and forcing replacement of existing tasks.

Example · bash
aws ecs update-service \
  --cluster prod-cluster \
  --service api-svc \
  --task-definition api:6 \
  --force-new-deployment

Scale Service Down to Zero

Sets desired count to zero so all running tasks are stopped immediately, useful for emergency shutdowns or cost saving.

Example · bash
aws ecs update-service \
  --cluster prod-cluster \
  --service api-svc \
  --desired-count 0

Discussion

  • Be the first to comment on this lesson.