Network Security
AWS Network Security ใช้หลายชั้นร่วมกัน: Security Group สำหรับ resource-level firewall, Network ACL สำหรับ subnet-level guardrail และ VPC Flow Logs สำหรับ visibility
Security Group
Security Group เป็น virtual firewall ที่ผูกกับ resource เช่น EC2 network interface, Load Balancer หรือ RDS Rule ระบุ source/destination, protocol และ port Security Group เป็น stateful: ถ้าอนุญาต request แล้ว response traffic จะกลับได้โดยไม่ต้องสร้าง inbound/outbound rule ย้อนกลับแยกเอง
Example application tier security group
Inbound:
TCP 8080 from sg-alb
Outbound:
TCP 5432 to sg-database
Network ACL
Network ACL หรือ NACL ทำงานระดับ subnet และมีทั้ง inbound/outbound rule ที่เป็น allow หรือ deny NACL เป็น stateless จึงต้องอนุญาตทั้ง request และ response path โดยเฉพาะ ephemeral ports
| หัวข้อ | Security Group | Network ACL |
|---|---|---|
| Scope | Resource / ENI | Subnet |
| State | Stateful | Stateless |
| Rule type | Allow rules only | Allow และ Deny |
| Evaluation | ทุก rule ที่ match รวมกัน | เลข rule ต่ำกว่าถูกประเมินก่อน |
| Default use | หลักในการควบคุม access | เพิ่ม guardrail ระดับ subnet |
VPC Flow Logs
VPC Flow Logs เก็บข้อมูล IP traffic ที่เข้าออก network interface, subnet หรือ VPC และ publish ไป CloudWatch Logs, S3 หรือ Data Firehose ใช้ช่วยวิเคราะห์ว่า traffic ถูก ACCEPT หรือ REJECT, source/destination คืออะไร และ port ใดเกี่ยวข้อง
# Fields commonly inspected
srcaddr dstaddr srcport dstport protocol action log-status
Best practices
- เปิด inbound เฉพาะ source ที่จำเป็น เช่น ALB SG ไป App SG ไม่ใช้
0.0.0.0/0 - ใช้ Security Group reference แทน hard-code IP เมื่อ resource อยู่ใน VPC เดียวกัน
- ใช้ NACL อย่างระมัดระวัง เพราะ stateless rules ทำให้ troubleshooting ซับซ้อนได้
- เปิด Flow Logs สำหรับ subnet/VPC สำคัญ และตั้ง retention/cost control
Common mistakes
- เปิด SSH, RDP หรือ Database port จาก
0.0.0.0/0 - ใช้ NACL ปิด traffic แล้วลืม ephemeral return path
- ดูแค่ Security Group แต่ลืม route table หรือ NACL
- เปิด Flow Logs แต่ไม่กำหนด retention ทำให้ค่า log โตเรื่อยๆ
Review questions
- เปรียบเทียบ Security Group stateful กับ NACL stateless ได้
- ออกแบบ SG chain แบบ Internet → ALB → App → DB ได้โดยไม่เปิด database public
- อ่าน Flow Logs field พื้นฐานเพื่อ debug traffic ได้
- Stateful ต่างจาก stateless อย่างไร?
- เมื่อใดควรใช้ NACL แทนหรือเสริม Security Group?
- VPC Flow Logs ช่วย debug ปัญหา network อย่างไร?