Hands-on LabCreates billable resources

Lab 10: Containers and Serverless Baseline

Lab นี้ฝึกทั้ง container runtime และ serverless/event-driven runtime: ECR, ECS Fargate, ALB, Lambda, API Gateway, S3 Event, SQS และ DLQ

Learning objectives

  • Push Docker Image ไปยัง ECR
  • Deploy Web Application บน ECS Fargate และเชื่อมกับ ALB
  • สร้าง Lambda ที่ทำงานจาก S3 Event
  • สร้าง API ด้วย API Gateway และ Lambda
  • สร้าง SQS Queue พร้อม Dead-letter Queue

Prerequisites

  • มี VPC, public/private subnets และ IAM permissions จาก Phase ก่อนหน้า
  • ติดตั้ง Docker, AWS CLI และตั้ง AWS profile แล้ว
  • มี Budget และเข้าใจค่าใช้จ่าย ALB, ECS/Fargate, NAT Gateway, Lambda, API Gateway, SQS และ CloudWatch Logs
  • มี sample web app ขนาดเล็กสำหรับ build image

Step 1: Push image to ECR

aws ecr create-repository --repository-name learning-web
aws ecr get-login-password --region ap-southeast-1 \
  | docker login --username AWS --password-stdin ACCOUNT_ID.dkr.ecr.ap-southeast-1.amazonaws.com
docker build -t learning-web:1.0.0 .
docker tag learning-web:1.0.0 ACCOUNT_ID.dkr.ecr.ap-southeast-1.amazonaws.com/learning-web:1.0.0
docker push ACCOUNT_ID.dkr.ecr.ap-southeast-1.amazonaws.com/learning-web:1.0.0

Step 2: Deploy ECS Fargate service

  1. Create ECS Cluster
  2. Create Task Definition ที่ใช้ image จาก ECR, port 3000/80 และ CloudWatch Logs
  3. สร้าง Task Execution Role และ Task Role แยกกัน
  4. Create ECS Service แบบ Fargate ใน private subnets
  5. เชื่อม Service กับ ALB Target Group และตั้ง health check path
  6. เปิด Service Auto Scaling เช่น target tracking ที่ CPU 60%

Step 3: Create Lambda from S3 Event

  1. Create S3 bucket สำหรับ upload test files
  2. Create Lambda function และ execution role ที่เขียน logs ได้
  3. เพิ่ม S3 Event Notification สำหรับ ObjectCreated
  4. Upload file และดู CloudWatch Logs ของ Lambda

Step 4: Create API Gateway + Lambda

  1. Create HTTP API หรือ REST API
  2. เชื่อม route เช่น GET /health ไป Lambda
  3. เปิด access logs และตั้ง throttling ถ้าใช้ stage ที่รองรับ
  4. ทดสอบ endpoint ด้วย curl

Step 5: Create SQS with DLQ

  1. Create DLQ เช่น learning-orders-dlq
  2. Create main queue เช่น learning-orders
  3. ตั้ง redrive policy เช่น max receive count 3
  4. ส่ง test message และให้ Lambda/worker process
  5. ตั้ง CloudWatch Alarm เมื่อ DLQ มี visible messages

Expected results

  • ECR repository มี image tag ที่ push แล้ว
  • ECS Service มี running tasks และ ALB target healthy
  • S3 upload trigger Lambda และมี logs
  • API Gateway endpoint เรียก Lambda ได้
  • SQS main queue มี DLQ และ alarm สำหรับ failed messages

Verification steps

aws ecr describe-images --repository-name learning-web
aws ecs describe-services --cluster YOUR_CLUSTER --services YOUR_SERVICE
curl http://YOUR_ALB_DNS_NAME
curl https://YOUR_API_ID.execute-api.ap-southeast-1.amazonaws.com/health
aws sqs get-queue-attributes --queue-url YOUR_QUEUE_URL --attribute-names RedrivePolicy

Troubleshooting

อาการแนวทางแก้
ECS pull image ไม่ได้ตรวจ Task Execution Role, ECR repository URI, Region และ image tag
ALB target unhealthyตรวจ container port, health check path, Security Groups และ app startup time
S3 ไม่ trigger Lambdaตรวจ event notification, Lambda permission, bucket Region และ object prefix/suffix filter
SQS message ถูก process ซ้ำตรวจ visibility timeout, delete message logic และ idempotency

Cleanup procedures

  1. Delete ECS Service แล้วรอ tasks หยุด
  2. Delete ALB, Target Group, ECS Cluster และ CloudWatch Log Groups ที่สร้างเพื่อ Lab
  3. Delete ECR images/repository ถ้าไม่ใช้ต่อ
  4. Delete Lambda functions, API Gateway API และ S3 Event Notification
  5. Delete SQS queues และ DLQ
  6. ตรวจ Cost Explorer วันถัดไปเพื่อยืนยันว่าไม่มี resource ค้าง

Mini quiz

  1. ทำไม ECS task ควรอยู่ private subnets หลัง ALB?
  2. Lambda concurrency กระทบ downstream services อย่างไร?
  3. DLQ ช่วย operation หลัง failure อย่างไร?