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
- Create DB Subnet Group จาก private subnets อย่างน้อย 2 AZ
- Create RDS PostgreSQL หรือ MySQL ขนาดเล็กสำหรับ Lab
- Set Public access เป็น No
- Enable storage encryption
- Set backup retention เช่น 1-7 วันสำหรับ Lab
- 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
- Create manual snapshot ของ DB
- Restore snapshot เป็น DB instance ใหม่ใน private subnet
- ตรวจว่า restored DB ไม่ public และ SG ถูกต้อง
- 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
- Delete restored RDS DB
- Delete primary RDS DB ถ้าไม่ใช้ต่อ และเลือกว่าจะเก็บ final snapshot หรือไม่
- Delete manual snapshots ที่ไม่ต้องการ
- Delete DB Subnet Group และ DB Security Group ที่สร้างเฉพาะ Lab
- Delete DynamoDB table
- ตรวจ Cost Explorer วันถัดไป
Mini quiz
- ทำไม RDS production ควรอยู่ private subnet?
- Query ใน DynamoDB ต้องใช้ key อะไร?
- ทำไม restore test จึงสำคัญกว่าแค่เปิด backup?