Sub topic 10
Running a long-lived service
This chapter assumes you can write a Fargate task definition, you know what a VPC subnet and a security group are, and you can use the AWS CLI. You have already registered the web-app task definition from the previous chapters.
Tasks versus services
A task runs once. If it exits or crashes, ECS does nothing more. That suits batch jobs. A web application needs the opposite: a fixed number of copies that stay up.
An ECS service provides this. You give it a task definition and a desired count. The service scheduler compares the number of running tasks with the desired count and starts or stops tasks to close the gap. If a task crashes, fails a health check, or its infrastructure fails, the scheduler launches a replacement.
Creating the service
This command creates the service in the demo cluster.
aws ecs create-service \
--cluster demo \
--service-name web \
--task-definition web-app \
--desired-count 2 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-aaa111,subnet-bbb222],securityGroups=[sg-0123456789abcdef0],assignPublicIp=DISABLED}" \
--load-balancers "targetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/web/0123456789abcdef,containerName=web,containerPort=8080" \
--health-check-grace-period-seconds 60 \
--deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true}"The main options work as follows:
--task-definition web-appuses the latest active revision of the family. Useweb-app:3to pin a specific revision.--desired-count 2sets how many tasks to keep running.- The two subnets should be in different Availability Zones. The scheduler spreads tasks across them, so one zone failure does not remove the whole service.
assignPublicIp=DISABLEDkeeps tasks private. They need a NAT gateway or VPC endpoints to pull from ECR and write logs.--load-balancersregisters each task's IP address in the target group. Later chapters cover this, the grace period, and the circuit breaker.
Inspecting service state
Check the service with describe-services:
aws ecs describe-services --cluster demo --services web \
--query "services[0].{status:status,desired:desiredCount,running:runningCount,pending:pendingCount}"During a rollout, running may briefly differ from desired. The events list in the same response records scheduler decisions, such as a task failing to start or being replaced. Read it first when a service does not reach its desired count.
Common errors
| Symptom | Likely cause |
|---|---|
| Tasks stay in PROVISIONING, then stop | Subnet or security group wrong, or no route to ECR |
| `CannotPullContainerError` | Missing NAT or VPC endpoints, or the execution role cannot read ECR |
| Tasks start, then are replaced repeatedly | Health check failing, or the grace period is too short |
| `InvalidParameterException` on create | Subnets and target group are in different VPCs |
To stop a service, set the desired count to zero with aws ecs update-service --cluster demo --service web --desired-count 0. You can then delete it.
Exercise
A service has a desired count of 3. One task is stopped because its container crashed. What does the service scheduler do, and where do you look to see why it acted?
Show answer
The scheduler sees two running tasks against a desired count of three and launches a replacement from the service's task definition. The service's events list in describe-services records the action, and the container logs in CloudWatch Logs show why the container crashed.