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
- Create ECS Cluster
- Create Task Definition ที่ใช้ image จาก ECR, port 3000/80 และ CloudWatch Logs
- สร้าง Task Execution Role และ Task Role แยกกัน
- Create ECS Service แบบ Fargate ใน private subnets
- เชื่อม Service กับ ALB Target Group และตั้ง health check path
- เปิด Service Auto Scaling เช่น target tracking ที่ CPU 60%
Step 3: Create Lambda from S3 Event
- Create S3 bucket สำหรับ upload test files
- Create Lambda function และ execution role ที่เขียน logs ได้
- เพิ่ม S3 Event Notification สำหรับ ObjectCreated
- Upload file และดู CloudWatch Logs ของ Lambda
Step 4: Create API Gateway + Lambda
- Create HTTP API หรือ REST API
- เชื่อม route เช่น
GET /healthไป Lambda - เปิด access logs และตั้ง throttling ถ้าใช้ stage ที่รองรับ
- ทดสอบ endpoint ด้วย
curl
Step 5: Create SQS with DLQ
- Create DLQ เช่น
learning-orders-dlq - Create main queue เช่น
learning-orders - ตั้ง redrive policy เช่น max receive count 3
- ส่ง test message และให้ Lambda/worker process
- ตั้ง 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
- Delete ECS Service แล้วรอ tasks หยุด
- Delete ALB, Target Group, ECS Cluster และ CloudWatch Log Groups ที่สร้างเพื่อ Lab
- Delete ECR images/repository ถ้าไม่ใช้ต่อ
- Delete Lambda functions, API Gateway API และ S3 Event Notification
- Delete SQS queues และ DLQ
- ตรวจ Cost Explorer วันถัดไปเพื่อยืนยันว่าไม่มี resource ค้าง
Mini quiz
- ทำไม ECS task ควรอยู่ private subnets หลัง ALB?
- Lambda concurrency กระทบ downstream services อย่างไร?
- DLQ ช่วย operation หลัง failure อย่างไร?