Lesson 2Orchestration

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

หัวข้อFargateEC2 launch type
CapacityAWS จัดการ server ให้ทีมต้องดูแล EC2 capacity
เหมาะกับเริ่มเร็ว, ลด ops, workload ทั่วไปต้องการ control instance, GPU, daemon, cost optimization เฉพาะทาง
Scalingscale tasks โดยไม่จัดการ hostsต้อง scale ทั้ง tasks และ cluster capacity

ECS Service กับ ALB

สำหรับ web application, ECS Service มักเชื่อมกับ Application Load Balancer ผ่าน Target Group ALB รับ traffic จากผู้ใช้และส่งไป tasks ที่ healthy ใน private subnets

ClientHTTPS
ALBListener + Target Group
ECS ServiceDesired tasks
Fargate TasksContainer app

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

  1. Task Definition ต่างจาก Task อย่างไร?
  2. Fargate ลดงาน operations เรื่องใด?
  3. Task Role และ Task Execution Role ต่างกันอย่างไร?