Fargate in detail
1 - What is Fargate?
AWS Fargate is a serverless compute engine for containers that works with ECS and EKS, eliminating the need to provision and manage servers.
2 - Fargate vs EC2 Launch Type
| Aspect | Fargate | EC2 |
|---|---|---|
| Server management | None | You manage |
| Scaling | Per task | Cluster + tasks |
| Pricing | Pay-per-task | Pay-per-instance |
| GPU | Not supported | Supported |
| Spot instances | Fargate Spot | EC2 Spot |
| Instance access | No | Yes |
| Isolation | Task-level | Instance-level |
3 - Fargate configuration
3.1 Fargate Task Definition
{
"family": "fargate-app",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"executionRoleArn": "arn:aws:iam::123456789:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "app",
"image": "nginx:latest",
"essential": true,
"portMappings": [
{"containerPort": 80, "protocol": "tcp"}
]
}
]
}
3.2 Platform Versions
| Version | Description |
|---|---|
| LATEST | Latest stable version |
| 1.4.0 | EFS, secrets support |
| 1.3.0 | Container Insights support |
ECSService:
Properties:
PlatformVersion: "1.4.0"
4 - Fargate Spot
Save up to 70% with spot capacity.
4.1 Configuration
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
- FARGATE_SPOT
DefaultCapacityProviderStrategy:
- CapacityProvider: FARGATE
Base: 2 # 2 tasks minimum sur Fargate
Weight: 1
- CapacityProvider: FARGATE_SPOT
Weight: 4 # 4x plus de tasks sur Spot
4.2 Placement strategy
# 20% Fargate, 80% Fargate Spot
aws ecs create-service \
--cluster prod-cluster \
--service-name web \
--task-definition web:1 \
--desired-count 10 \
--capacity-provider-strategy \
capacityProvider=FARGATE,base=2,weight=1 \
capacityProvider=FARGATE_SPOT,weight=4
4.3 Handling interruptions
5 - ARM64 Architecture
5.1 Advantages
- Up to 40% cheaper
- Better performance per watt
- Ideal for CPU-bound workloads
5.2 Configuration
{
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
},
"containerDefinitions": [
{
"name": "app",
"image": "my-arm64-image:latest"
}
]
}
5.3 Multi-architecture build
# Dockerfile multi-arch
docker buildx create --use
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myapp:latest \
--push .
6 - Ephemeral storage
6.1 Configuration
{
"ephemeralStorage": {
"sizeInGiB": 100
}
}
| Default size | Max | Additional cost |
|---|---|---|
| 20 GB | 200 GB | $0.000111/GB/hour |
6.2 Use cases
- Temporary cache
- Build files
- Session data
- Temporary logs
7 - EFS with Fargate
7.1 Configuration
{
"volumes": [
{
"name": "efs-volume",
"efsVolumeConfiguration": {
"fileSystemId": "fs-12345678",
"rootDirectory": "/",
"transitEncryption": "ENABLED",
"transitEncryptionPort": 2049,
"authorizationConfig": {
"accessPointId": "fsap-12345678",
"iam": "ENABLED"
}
}
}
],
"containerDefinitions": [
{
"mountPoints": [
{
"sourceVolume": "efs-volume",
"containerPath": "/data",
"readOnly": false
}
]
}
]
}
7.2 EFS Access Point
EFSAccessPoint:
Type: AWS::EFS::AccessPoint
Properties:
FileSystemId: !Ref EFSFileSystem
PosixUser:
Uid: "1000"
Gid: "1000"
RootDirectory:
CreationInfo:
OwnerUid: "1000"
OwnerGid: "1000"
Permissions: "755"
Path: /app-data
8 - Pricing
8.1 Standard Fargate
| Resource | Price (eu-west-1) |
|---|---|
| vCPU | $0.04048/hour |
| Memory | $0.004445/GB/hour |
| Storage (> 20GB) | $0.000111/GB/hour |
8.2 Fargate Spot
| Resource | Price (eu-west-1) |
|---|---|
| vCPU | ~$0.012/hour (-70%) |
| Memory | ~$0.0013/GB/hour (-70%) |
8.3 Example calculation
1 task: 1 vCPU, 2GB RAM, 24h
Standard Fargate:
- vCPU: 0.97
- Memory: 0.21
- Total: $1.18/day
Fargate Spot:
- Total: ~$0.35/day (-70%)
9 - Cost optimization
9.1 Right-sizing
# Analyser l'utilisation avec Container Insights
# CloudWatch > Insights > Container Insights > Performance Monitoring
# Métriques clés :
# - CpuUtilized vs CpuReserved
# - MemoryUtilized vs MemoryReserved
9.2 Recommendations
- Use Fargate Spot for non-critical workloads
- Choose ARM64 when possible
- Adjust CPU/Memory based on actual usage
- Use scaling to adapt to the load
Summary
In this chapter, we learned:
- The advantages of Fargate vs EC2
- The configuration of Fargate Task Definitions
- Fargate Spot to save costs
- The ARM64 architecture
- Ephemeral storage and EFS
- Pricing and cost optimization
Next step
In the next chapter, we will look at Auto Scaling.