Amazon ECS and AWS Fargate
Amazon ECS เป็น managed container orchestration service สำหรับ deploy, run และ scale containerized applications โดยไม่ต้องดูแล control plane เอง
ECS concepts
- Cluster: logical grouping ของ capacity และ services
- Task Definition: blueprint ของ container เช่น image, CPU, memory, ports, env vars, log config และ IAM roles
- Task: running copy ของ Task Definition เหมาะกับงาน batch หรือ one-off
- Service: ดูแล long-running tasks ตาม desired count และช่วย rolling deployment
Fargate vs EC2 launch type
| หัวข้อ | Fargate | EC2 launch type |
|---|---|---|
| Capacity | AWS จัดการ server ให้ | ทีมต้องดูแล EC2 capacity |
| เหมาะกับ | เริ่มเร็ว, ลด ops, workload ทั่วไป | ต้องการ control instance, GPU, daemon, cost optimization เฉพาะทาง |
| Scaling | scale tasks โดยไม่จัดการ hosts | ต้อง scale ทั้ง tasks และ cluster capacity |
ECS Service กับ ALB
สำหรับ web application, ECS Service มักเชื่อมกับ Application Load Balancer ผ่าน Target Group ALB รับ traffic จากผู้ใช้และส่งไป tasks ที่ healthy ใน private subnets
IAM roles
Task Execution Role ให้ ECS agent pull image จาก ECR และส่ง logs ไป CloudWatch ส่วน Task Role คือ permission ที่ application code ใช้เรียก AWS services เช่น S3, DynamoDB หรือ SQS
Service Auto Scaling
ECS Service Auto Scaling ปรับ desired count ตาม metric เช่น CPUUtilization, MemoryUtilization หรือ ALB RequestCountPerTarget ต้องตั้ง minimum/maximum capacity และ health check ให้สอดคล้องกับ startup time ของ app
Common mistakes
- ใช้ Task Execution Role เป็น Task Role ทำให้ permission ของ app กว้างเกินไป
- ใส่ secrets เป็น plaintext env vars แทนใช้ Secrets Manager/Parameter Store integration
- ตั้ง health check interval/timeout ไม่เหมาะ ทำให้ deployment fail ทั้งที่ app แค่ start ช้า
Review questions
- Task Definition ต่างจาก Task อย่างไร?
- Fargate ลดงาน operations เรื่องใด?
- Task Role และ Task Execution Role ต่างกันอย่างไร?