Hands-on LabCreates billable resources

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
  1. Create Security Groups สำหรับ ALB และ App
  2. Launch EC2 ด้วย User Data และ IAM role ที่เหมาะสม
  3. Attach/mount EBS แล้วตรวจ filesystem
  4. Create ALB, Target Group และ health check
  5. 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

  1. Create EBS volume gp3 ใน AZ เดียวกับ instance
  2. Attach volume ไปยัง instance
  3. Connect ผ่าน Session Manager
  4. 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

  1. Create Target Group protocol HTTP port 80
  2. Set health check path เป็น /
  3. Register EC2 instance เป็น target
  4. Create internet-facing ALB ใน public subnets อย่างน้อย 2 AZ
  5. Create Listener HTTP 80 forward ไป Target Group
  6. เปิด ALB DNS name และตรวจว่าเห็นหน้าเว็บ

Step 5: Create Launch Template and ASG

  1. Create Launch Template จาก config เดิม: AMI, instance type, App Security Group, IAM Instance Profile, User Data
  2. Create Auto Scaling Group ใน subnets 2 AZ
  3. Attach ASG to Target Group
  4. Set min=2, desired=2, max=4 สำหรับ Lab สั้นๆ
  5. รอ instances healthy ใน Target Group

Expected results

Definition of done
  1. ALB DNS เปิดหน้า web server ได้
  2. Target Group มี healthy targets อย่างน้อย 2 ตัวหลังใช้ ASG
  3. EBS volume mount ที่ /data บน instance ที่ทดสอบ
  4. ASG replacement ทำงานเมื่อ instance unhealthy/terminated
  5. 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

  1. Set ASG desired/min/max เป็น 0 หรือ delete ASG
  2. Delete Launch Template versions ที่ไม่ใช้
  3. Deregister/delete Target Group และ delete ALB
  4. Terminate standalone EC2 instance
  5. Detach/delete EBS volumes ที่ไม่ใช้ และลบ snapshots ทดลองถ้ามี
  6. Release Elastic IP/public IPv4 ที่ allocate เอง
  7. ตรวจ Cost Explorer วันถัดไป

Mini quiz

  1. ทำไม Target Group health check จึงสำคัญต่อ ALB และ ASG?
  2. ทำไม ASG ควรวางหลาย Availability Zones?
  3. Resource ใดบ้างใน Lab นี้ที่ยังมีค่าใช้จ่ายหลังหยุด instance?