Lab 4: Web EC2, EBS, ALB and ASG Baseline
Lab นี้สร้าง web server บน EC2, attach EBS, วาง Application Load Balancer และสร้าง Auto Scaling Group กระจายอย่างน้อย 2 Availability Zones
Goal
รัน web workload หลัง ALB และ ASG
เริ่มจาก EC2 เดี่ยว แล้วขยายเป็น target group และ Auto Scaling Group 2 AZ
Safety
Compute lab สร้าง billable resources หลายตัว
EC2, EBS, ALB, public IPv4, NAT และ snapshots ต้องตรวจ cleanup
Output
Healthy targets และ repeatable launch config
สุดท้ายต้องอธิบายได้ว่า ALB health check และ ASG replacement ทำงานอย่างไร
Run order
- Create Security Groups สำหรับ ALB และ App
- Launch EC2 ด้วย User Data และ IAM role ที่เหมาะสม
- Attach/mount EBS แล้วตรวจ filesystem
- Create ALB, Target Group และ health check
- Create Launch Template และ ASG across 2 AZs แล้ว cleanup resource หลังทดสอบ
Learning objectives
- Launch EC2 instance ด้วย AMI, Instance Type, Security Group และ IAM Role ที่ถูกต้อง
- ใช้ User Data bootstrap web server
- Attach และ mount EBS volume
- Create ALB, Listener และ Target Group พร้อม Health Check
- Create Launch Template และ Auto Scaling Group across 2 AZs
- ทดสอบ health replacement และ cleanup resource
Prerequisites
- ผ่าน Phase 1 Budget, Phase 2 IAM และ Phase 3 VPC baseline แล้ว
- มี VPC พร้อม public subnets อย่างน้อย 2 AZ และ private/app subnets ถ้าต้องการวาง instance private
- มี IAM Role สำหรับ EC2 ที่อนุญาต SSM Session Manager และ CloudWatch logs ตามที่ต้องใช้
- เข้าใจค่าใช้จ่าย EC2, EBS, ALB และ public IPv4
Cost warning: EC2, EBS, ALB, NAT, public IPv4 และ snapshots อาจมีค่าใช้จ่าย ห้ามจบ Lab โดยไม่ cleanup
Step 1: Create Security Groups
ALB security group:
inbound TCP 80 from your IP or 0.0.0.0/0 for short lab
outbound TCP 80 to App security group
App security group:
inbound TCP 80 from ALB security group
outbound TCP 443 to 0.0.0.0/0 or VPC endpoints
Step 2: Launch one EC2 with User Data
ใช้ Amazon Linux AMI และ instance ขนาดเล็กสำหรับ Lab เช่น t3.micro ถ้าเหมาะกับ Region/Free Tier ของคุณ
#!/bin/bash
dnf update -y
dnf install -y nginx
cat > /usr/share/nginx/html/index.html <<'HTML'
AWS Compute Lab
Instance bootstrapped with User Data.
HTML
systemctl enable nginx
systemctl start nginx
Step 3: Attach and mount EBS
- Create EBS volume
gp3ใน AZ เดียวกับ instance - Attach volume ไปยัง instance
- Connect ผ่าน Session Manager
- Format และ mount volume ไปที่
/data
lsblk
sudo mkfs -t xfs /dev/xvdf
sudo mkdir -p /data
sudo mount /dev/xvdf /data
df -h
Step 4: Create ALB and Target Group
- Create Target Group protocol HTTP port 80
- Set health check path เป็น
/ - Register EC2 instance เป็น target
- Create internet-facing ALB ใน public subnets อย่างน้อย 2 AZ
- Create Listener HTTP 80 forward ไป Target Group
- เปิด ALB DNS name และตรวจว่าเห็นหน้าเว็บ
Step 5: Create Launch Template and ASG
- Create Launch Template จาก config เดิม: AMI, instance type, App Security Group, IAM Instance Profile, User Data
- Create Auto Scaling Group ใน subnets 2 AZ
- Attach ASG to Target Group
- Set min=2, desired=2, max=4 สำหรับ Lab สั้นๆ
- รอ instances healthy ใน Target Group
Expected results
Definition of done
- ALB DNS เปิดหน้า web server ได้
- Target Group มี healthy targets อย่างน้อย 2 ตัวหลังใช้ ASG
- EBS volume mount ที่
/dataบน instance ที่ทดสอบ - ASG replacement ทำงานเมื่อ instance unhealthy/terminated
- EC2, EBS, ALB, Target Group, ASG, Launch Template และ snapshots ทดลองถูก cleanup ถ้าไม่ใช้ต่อ
Verification steps
aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:...
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names learning-web-asg
curl http://your-alb-dns-name
Troubleshooting
| อาการ | แนวทางแก้ |
|---|---|
| Target unhealthy | ตรวจ health check path, nginx status, App SG inbound จาก ALB SG และ subnet route |
| ALB เข้าไม่ได้ | ตรวจ ALB SG inbound, listener, target group และ DNS name |
| User Data ไม่ทำงาน | ตรวจ /var/log/cloud-init-output.log และ package manager command ของ AMI |
| EBS mount ไม่เจอ | ตรวจ AZ ของ volume, device name, filesystem และ lsblk |
Cleanup procedures
- Set ASG desired/min/max เป็น 0 หรือ delete ASG
- Delete Launch Template versions ที่ไม่ใช้
- Deregister/delete Target Group และ delete ALB
- Terminate standalone EC2 instance
- Detach/delete EBS volumes ที่ไม่ใช้ และลบ snapshots ทดลองถ้ามี
- Release Elastic IP/public IPv4 ที่ allocate เอง
- ตรวจ Cost Explorer วันถัดไป
Mini quiz
- ทำไม Target Group health check จึงสำคัญต่อ ALB และ ASG?
- ทำไม ASG ควรวางหลาย Availability Zones?
- Resource ใดบ้างใน Lab นี้ที่ยังมีค่าใช้จ่ายหลังหยุด instance?