Lesson 3Security

Network Security

AWS Network Security ใช้หลายชั้นร่วมกัน: Security Group สำหรับ resource-level firewall, Network ACL สำหรับ subnet-level guardrail และ VPC Flow Logs สำหรับ visibility

Primary control Security Group คุมระดับ resource ใช้ SG reference เพื่อเปิดเฉพาะ ALB → App → DB แทนการเปิด IP กว้าง
Subnet guardrail NACL เป็น stateless ต้องคิดทั้ง request และ response path โดยเฉพาะ ephemeral ports
Visibility Flow Logs ช่วยบอก ACCEPT/REJECT ใช้ดู source, destination, port และ action แต่ต้องตั้ง retention/cost control

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 GroupNetwork ACL
ScopeResource / ENISubnet
StateStatefulStateless
Rule typeAllow rules onlyAllow และ 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

Before next lesson
  1. เปรียบเทียบ Security Group stateful กับ NACL stateless ได้
  2. ออกแบบ SG chain แบบ Internet → ALB → App → DB ได้โดยไม่เปิด database public
  3. อ่าน Flow Logs field พื้นฐานเพื่อ debug traffic ได้
  1. Stateful ต่างจาก stateless อย่างไร?
  2. เมื่อใดควรใช้ NACL แทนหรือเสริม Security Group?
  3. VPC Flow Logs ช่วย debug ปัญหา network อย่างไร?