Skip to main content

ECS Services


1 - What is a Service?

An ECS Service automatically maintains the desired number of running tasks and manages the deployment of new versions.


2 - Service types

2.1 REPLICA service

Maintains a specific number of tasks spread across the cluster.

{
"serviceName": "api-service",
"desiredCount": 3,
"schedulingStrategy": "REPLICA"
}

2.2 DAEMON service

Runs exactly one task on each EC2 instance of the cluster.

{
"serviceName": "monitoring-agent",
"schedulingStrategy": "DAEMON"
}

3 - Create a Service

3.1 Via CLI

aws ecs create-service \
--cluster prod-cluster \
--service-name api-service \
--task-definition api:3 \
--desired-count 3 \
--launch-type FARGATE \
--platform-version LATEST \
--network-configuration "awsvpcConfiguration={
subnets=[subnet-111,subnet-222],
securityGroups=[sg-123],
assignPublicIp=DISABLED
}" \
--load-balancers "targetGroupArn=arn:aws:elasticloadbalancing:...:targetgroup/api/123,containerName=app,containerPort=8080" \
--health-check-grace-period-seconds 60 \
--deployment-configuration "maximumPercent=200,minimumHealthyPercent=100" \
--enable-execute-command

3.2 Via CloudFormation

ECSService:
Type: AWS::ECS::Service
DependsOn: ALBListener
Properties:
ServiceName: api-service
Cluster: !Ref ECSCluster
TaskDefinition: !Ref TaskDefinition
DesiredCount: 3
LaunchType: FARGATE
PlatformVersion: LATEST

NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroups:
- !Ref ServiceSecurityGroup
AssignPublicIp: DISABLED

LoadBalancers:
- TargetGroupArn: !Ref TargetGroup
ContainerName: app
ContainerPort: 8080

HealthCheckGracePeriodSeconds: 60

DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100
DeploymentCircuitBreaker:
Enable: true
Rollback: true

DeploymentController:
Type: ECS

EnableExecuteCommand: true

ServiceRegistries:
- RegistryArn: !GetAtt ServiceDiscoveryService.Arn

Tags:
- Key: Environment
Value: Production

4 - Deployment strategies

4.1 Rolling Update (ECS)

4.2 Blue/Green (CodeDeploy)

DeploymentController:
Type: CODE_DEPLOY

# Nécessite CodeDeploy deployment group

4.3 Deployment configuration

ParameterDescription
MaximumPercentMax % of tasks during deployment
MinimumHealthyPercentMin % of healthy tasks during deployment
DeploymentCircuitBreakerAutomatic rollback on failure
# Déploiement prudent (pas de downtime)
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100

# Déploiement rapide (accepte downtime temporaire)
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 50

5 - Circuit Breaker

DeploymentConfiguration:
DeploymentCircuitBreaker:
Enable: true
Rollback: true

The circuit breaker detects deployment failures and performs an automatic rollback if:

  • Tasks fail to start
  • Health checks fail
  • The deployment exceeds the timeout

6 - Service Discovery

6.1 AWS Cloud Map

ServiceDiscoveryNamespace:
Type: AWS::ServiceDiscovery::PrivateDnsNamespace
Properties:
Name: internal.local
Vpc: !Ref VPC

ServiceDiscoveryService:
Type: AWS::ServiceDiscovery::Service
Properties:
Name: api
NamespaceId: !Ref ServiceDiscoveryNamespace
DnsConfig:
DnsRecords:
- Type: A
TTL: 60
HealthCheckCustomConfig:
FailureThreshold: 1

# Dans le service ECS
ECSService:
Properties:
ServiceRegistries:
- RegistryArn: !GetAtt ServiceDiscoveryService.Arn

6.2 Usage

# Les tasks s'enregistrent automatiquement
# DNS: api.internal.local

# Depuis un autre conteneur
curl http://api.internal.local:8080/health

7 - Update a service

7.1 New Task Definition

# Créer nouvelle révision
aws ecs register-task-definition --cli-input-json file://task-def-v2.json

# Mettre à jour le service
aws ecs update-service \
--cluster prod-cluster \
--service api-service \
--task-definition api:4 \
--force-new-deployment

7.2 Manual scaling

# Changer le nombre de tasks
aws ecs update-service \
--cluster prod-cluster \
--service api-service \
--desired-count 5

7.3 Force new deployment

# Forcer le redéploiement (même task definition)
aws ecs update-service \
--cluster prod-cluster \
--service api-service \
--force-new-deployment

8 - ECS Exec

Shell access to running containers.

8.1 Enable

ECSService:
Properties:
EnableExecuteCommand: true

# Task Role doit avoir
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}

8.2 Use

# Se connecter à un conteneur
aws ecs execute-command \
--cluster prod-cluster \
--task abc123 \
--container app \
--interactive \
--command "/bin/bash"

9 - Managing services

# Lister les services
aws ecs list-services --cluster prod-cluster

# Détails d'un service
aws ecs describe-services \
--cluster prod-cluster \
--services api-service

# Arrêter un service (set desired count to 0)
aws ecs update-service \
--cluster prod-cluster \
--service api-service \
--desired-count 0

# Supprimer un service
aws ecs delete-service \
--cluster prod-cluster \
--service api-service \
--force

Summary

In this chapter, we learned:

  • The types of services (REPLICA vs DAEMON)
  • Creating services via CLI and CloudFormation
  • The deployment strategies (Rolling, Blue/Green)
  • The Circuit Breaker for automatic rollbacks
  • Service Discovery with Cloud Map
  • Updating services
  • ECS Exec for debugging

Next step

In the next chapter, we will look at Networking and Load Balancing.

→ Next chapter: Networking and Load Balancing


← Back to the table of contents