AWS Fargate: Serverless Containers for Batch Jobs
We started using AWS Fargate for batch jobs. No more managing servers.
What is Fargate?
Fargate runs containers without managing servers:
- No EC2 instances to manage
- Pay only for what you use
- Automatic scaling
- Integrates with ECS and EKS
Use Case: Batch Jobs
We have batch jobs that run:
- Data processing
- Report generation
- Database backups
- Image processing
Before: Dedicated EC2 instances (running 24/7, mostly idle)
After: Fargate (pay only when jobs run)
ECS Task Definition
{
"family": "data-processor",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "1024",
"memory": "2048",
"containerDefinitions": [
{
"name": "processor",
"image": "registry.example.com/data-processor:latest",
"environment": [
{
"name": "S3_BUCKET",
"value": "my-data-bucket"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/data-processor",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
Running Tasks
# Run task
aws ecs run-task \
--cluster my-cluster \
--launch-type FARGATE \
--task-definition data-processor \
--network-configuration "awsvpcConfiguration={subnets=[subnet-12345],securityGroups=[sg-12345]}"
Scheduled Tasks
# CloudWatch Events rule
aws events put-rule \
--name daily-backup \
--schedule-expression "cron(0 2 * * ? *)"
# Target
aws events put-targets \
--rule daily-backup \
--targets "Id"="1","Arn"="arn:aws:ecs:us-east-1:123456789:cluster/my-cluster","RoleArn"="arn:aws:iam::123456789:role/ecsEventsRole","EcsParameters"="{TaskDefinitionArn=arn:aws:ecs:us-east-1:123456789:task-definition/backup:1,TaskCount=1,LaunchType=FARGATE,NetworkConfiguration={awsvpcConfiguration={Subnets=[subnet-12345],SecurityGroups=[sg-12345],AssignPublicIp=ENABLED}}}"
Cost Comparison
Before (EC2):
- 2 m5.large instances (24/7)
- Cost: $140/month
- Utilization: 10%
After (Fargate):
- Pay per task execution
- Cost: $45/month
- Utilization: 100% (when running)
Savings: $95/month (68%)
Pros
- No server management
- Pay only for what you use
- Automatic scaling
- Easy to use
Cons
- More expensive than EC2 for long-running tasks
- Cold start time (~30 seconds)
- Limited to 4 vCPU, 30GB memory
The Verdict
Fargate is perfect for batch jobs and sporadic workloads. Not for long-running services (use EKS/ECS on EC2 instead).
We’re using Fargate for all batch jobs now. Significant cost savings.
Questions? Let me know!