Concepts fondamentaux
1 - Clusters
1.1 Qu'est-ce qu'un Cluster ?
Un Cluster est un regroupement logique de ressources ECS. C'est le conteneur de plus haut niveau pour vos services et tasks.
1.2 Créer un Cluster
# Cluster simple (Fargate)
aws ecs create-cluster --cluster-name prod-cluster
# Avec Container Insights
aws ecs create-cluster \
--cluster-name prod-cluster \
--settings name=containerInsights,value=enabled
# Avec Capacity Providers
aws ecs create-cluster \
--cluster-name prod-cluster \
--capacity-providers FARGATE FARGATE_SPOT \
--default-capacity-provider-strategy \
capacityProvider=FARGATE,weight=1,base=1 \
capacityProvider=FARGATE_SPOT,weight=3
1.3 Capacity Providers
| Provider | Description |
|---|---|
| FARGATE | Fargate standard |
| FARGATE_SPOT | Fargate avec instances spot |
| Auto Scaling Group | EC2 avec ASG |
# CloudFormation
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: production
CapacityProviders:
- FARGATE
- FARGATE_SPOT
DefaultCapacityProviderStrategy:
- CapacityProvider: FARGATE
Base: 1
Weight: 1
- CapacityProvider: FARGATE_SPOT
Weight: 3
2 - Task Definitions
2.1 Qu'est-ce qu'une Task Definition ?
Une Task Definition est un blueprint qui décrit comment vos conteneurs doivent s'ex écuter. C'est comme un Dockerfile mais pour l'exécution.
2.2 Structure JSON
{
"family": "mon-app",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789:role/ecsTaskRole",
"containerDefinitions": [
{
"name": "app",
"image": "123456789.dkr.ecr.eu-west-1.amazonaws.com/mon-app:latest",
"essential": true,
"portMappings": [
{
"containerPort": 8080,
"protocol": "tcp"
}
],
"environment": [
{"name": "ENV", "value": "production"}
],
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:eu-west-1:123456789:secret:db-password"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/mon-app",
"awslogs-region": "eu-west-1",
"awslogs-stream-prefix": "ecs"
}
},
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 60
}
}
]
}
2.3 Paramètres importants
| Paramètre | Description |
|---|---|
family | Nom de la task definition |
cpu | vCPU alloués (256, 512, 1024, etc.) |
memory | Mémoire en MB |
networkMode | awsvpc (Fargate), bridge, host |
executionRoleArn | Rôle pour pull images, logs |
taskRoleArn | Rôle pour l'application |
3 - Tasks
3.1 Qu'est-ce qu'une Task ?
Une Task est une instance en cours d'exécution d'une Task Definition. Elle peut contenir un ou plusieurs conteneurs.
3.2 Exécuter une Task
# Task standalone (one-shot)
aws ecs run-task \
--cluster prod-cluster \
--task-definition mon-app:1 \
--count 1 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-123],securityGroups=[sg-123],assignPublicIp=ENABLED}"
# Voir les tasks
aws ecs list-tasks --cluster prod-cluster
# Détails d'une task
aws ecs describe-tasks \
--cluster prod-cluster \
--tasks arn:aws:ecs:eu-west-1:123456789:task/prod-cluster/abc123
3.3 Task vs Service
| Aspect | Task (run-task) | Service |
|---|---|---|
| Durée de vie | One-shot ou manuelle | Long-running |
| Nombre | Spécifié à l'exécution | Maintenu automatiquement |
| Restart | Non | Oui |
| Load Balancing | Non | Oui |
| Cas d'usage | Batch, migrations | API, Web |
4 - Services
4.1 Qu'est-ce qu'un Service ?
Un Service maintient un nombre spécifié de tasks en cours d'exécution et peut les intégrer à un load balancer.
4.2 Créer un Service
aws ecs create-service \
--cluster prod-cluster \
--service-name mon-api \
--task-definition mon-app:1 \
--desired-count 3 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-123,subnet-456],securityGroups=[sg-123],assignPublicIp=DISABLED}" \
--load-balancers "targetGroupArn=arn:aws:elasticloadbalancing:...,containerName=app,containerPort=8080"
4.3 Service CloudFormation
ECSService:
Type: AWS::ECS::Service
Properties:
ServiceName: mon-api
Cluster: !Ref ECSCluster
TaskDefinition: !Ref TaskDefinition
DesiredCount: 3
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
Subnets:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroups:
- !Ref ServiceSecurityGroup
AssignPublicIp: DISABLED
LoadBalancers:
- TargetGroupArn: !Ref TargetGroup
ContainerName: app
ContainerPort: 8080
DeploymentConfiguration:
MinimumHealthyPercent: 100
MaximumPercent: 200
HealthCheckGracePeriodSeconds: 60
5 - Rôles IAM
5.1 Types de rôles
5.2 Execution Role
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:log-group:/ecs/*"
},
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": "arn:aws:secretsmanager:*:*:secret:prod/*"
}
]
}
5.3 Task Role
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::mon-bucket/*"
},
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query"
],
"Resource": "arn:aws:dynamodb:*:*:table/ma-table"
}
]
}
6 - Network Modes
6.1 Comparaison
| Mode | Description | Fargate | EC2 |
|---|---|---|---|
awsvpc | ENI dédié par task | ✅ Obligatoire | ✅ |
bridge | Docker bridge network | ❌ | ✅ |
host | Network de l'hôte | ❌ | ✅ |
none | Pas de network | ❌ | ✅ |
6.2 Mode awsvpc
Avantages :
- IP dédiée par task
- Security Groups par task
- Meilleure isolation
Résumé
Dans ce chapitre, nous avons appris :
- Les Clusters et leur configuration
- Les Task Definitions et leur structure
- Les Tasks et leur cycle de vie
- Les Services et leur gestion
- Les rôles IAM (Execution, Task, Service)
- Les network modes
Prochaine étape
Dans le prochain chapitre, nous approfondirons les Task Definitions.
→ Chapitre suivant : Task Definitions