Hands-on LabCreates billable resources

Lab 6: Private RDS and DynamoDB Baseline

Lab นี้สร้าง RDS ใน private subnet, จำกัด access จาก application server เท่านั้น, สร้าง snapshot/restore และสร้าง DynamoDB table เพื่อทดลอง CRUD, Query และ Scan

Learning objectives

  • สร้าง DB Subnet Group จาก private subnets อย่างน้อย 2 AZ
  • สร้าง RDS แบบ private access และเปิด encryption
  • จำกัด Security Group ให้ application server เข้าถึง database เท่านั้น
  • สร้าง Snapshot และทดลอง Restore
  • สร้าง DynamoDB table พร้อม Partition Key / Sort Key และทดลอง Query vs Scan

Prerequisites

  • ผ่าน Phase 3 VPC และ Phase 4 Compute baseline
  • มี private subnets อย่างน้อย 2 AZ สำหรับ DB Subnet Group
  • มี application EC2 หรือ bastion/testing host ที่เชื่อมผ่าน Session Manager ได้
  • ตั้ง Budget แล้ว เพราะ RDS คิดค่าใช้จ่ายต่อชั่วโมงและ storage
Cost warning: RDS, storage, snapshots, NAT path และ DynamoDB usage อาจมีค่าใช้จ่าย Cleanup ทุกครั้งหลัง Lab

Step 1: Create DB Security Group

Database SG inbound:
  TCP 5432 from App Security Group only
Outbound:
  default or restricted according to your org standard

สำหรับ MySQL ใช้ port 3306 แทน 5432

Step 2: Create DB Subnet Group and RDS

  1. Create DB Subnet Group จาก private subnets อย่างน้อย 2 AZ
  2. Create RDS PostgreSQL หรือ MySQL ขนาดเล็กสำหรับ Lab
  3. Set Public access เป็น No
  4. Enable storage encryption
  5. Set backup retention เช่น 1-7 วันสำหรับ Lab
  6. Attach Database Security Group

Step 3: Connect from application server only

เข้า EC2 ผ่าน Session Manager แล้วทดสอบ connection ไป RDS endpoint:

# PostgreSQL example
psql "host=your-rds-endpoint port=5432 dbname=postgres user=admin sslmode=require"

# MySQL example
mysql -h your-rds-endpoint -u admin -p

Step 4: Snapshot and restore

  1. Create manual snapshot ของ DB
  2. Restore snapshot เป็น DB instance ใหม่ใน private subnet
  3. ตรวจว่า restored DB ไม่ public และ SG ถูกต้อง
  4. Delete restored DB หลังตรวจเสร็จ

Step 5: DynamoDB CRUD, Query and Scan

aws dynamodb create-table \
  --table-name LearningOrders \
  --attribute-definitions AttributeName=customerId,AttributeType=S AttributeName=orderDate,AttributeType=S \
  --key-schema AttributeName=customerId,KeyType=HASH AttributeName=orderDate,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST

aws dynamodb put-item --table-name LearningOrders --item '{
  "customerId": {"S": "C123"},
  "orderDate": {"S": "2026-06-16"},
  "amount": {"N": "1200"}
}'

ทดลอง query ด้วย customerId แล้วเปรียบเทียบกับ scan เพื่อเห็นความต่างด้าน access pattern

Expected results

  • RDS อยู่ใน private subnets และไม่มี public access
  • Application SG เท่านั้นที่ connect database port ได้
  • Snapshot restore สำเร็จและยังคง private baseline
  • DynamoDB table รองรับ Query ด้วย partition/sort key

Troubleshooting

อาการแนวทางแก้
Connect RDS ไม่ได้ตรวจ SG source, subnet route, public access setting, DB status และ endpoint/port
Auth failedตรวจ username/password และ Secrets Manager/Parameter Store value
Restore snapshot แล้ว publicตรวจ subnet group, public access และ SG ของ restored DB
DynamoDB query ไม่ได้ตรวจ key schema และ KeyConditionExpression ว่าตรง partition/sort key

Cleanup procedures

  1. Delete restored RDS DB
  2. Delete primary RDS DB ถ้าไม่ใช้ต่อ และเลือกว่าจะเก็บ final snapshot หรือไม่
  3. Delete manual snapshots ที่ไม่ต้องการ
  4. Delete DB Subnet Group และ DB Security Group ที่สร้างเฉพาะ Lab
  5. Delete DynamoDB table
  6. ตรวจ Cost Explorer วันถัดไป

Mini quiz

  1. ทำไม RDS production ควรอยู่ private subnet?
  2. Query ใน DynamoDB ต้องใช้ key อะไร?
  3. ทำไม restore test จึงสำคัญกว่าแค่เปิด backup?