Azure Network Security Group (NSG) คืออะไร?
NSG เป็น Virtual Firewall แบบ Layer 3/4 (L3/L4) ควบคุม Traffics เข้า/ออก Azure Resources (VM, Subnet, NIC, App Gateway, API Management, AKS, VNet) ผ่าน Security Rules (Allow/Deny) ตาม 5-tuple: Source IP/Port, Destination IP/Port, Protocol (TCP/UDP/ICMP/*)
🏗️ โครงสร้าง NSG
- Security Rules: Priority (100-4096), Name, Port, Protocol, Source/Dest (IP/CIDR/Service Tag/Application Security Group), Action (Allow/Deny), Direction (Inbound/Outbound)
- Default Rules: มี Default Rules คงที่ (Priority 65000+) Allow VNet Inbound/Outbound, Allow Azure Load Balancer, Deny All Inbound/Outbound
- Association: ผูกกับ Subnet (แนะนำ) หรือ NIC (VM เดียว) ได้ พร้อมกันได้ (Subnet + NIC) → Rule ทั้งสองชุดทำงาน (Effective Rules)
- Service Tags: กลุ่ม IP ของ Azure Services (AzureCloud, AzureLoadBalancer, Storage, Sql, ApiManagement, etc.) อัปเดตอัตโนมัติโดย Microsoft
- Application Security Groups (ASG): จัดกลุ่ม VM ตาม Application Logic (เช่น Web, App, DB) ใช้ใน Source/Destination แทน IP
⚙️ สร้างและจัดการ NSG (Portal / CLI / IaC)
Portal
- Create a Resource → Networking → Network Security Group
- Subscription, Resource Group, Name, Region
- Create → ไปที่ NSG → Settings → Inbound/Outbound Security Rules → Add
- Associate: Subnets → + Associate → เลือก VNet + Subnet
Azure CLI
# Create NSG
az network nsg create -g MyRG -n MyNSG -l eastus
# Add Rule (Allow HTTPS Inbound)
az network nsg rule create -g MyRG --nsg-name MyNSG -n Allow-HTTPS-Inbound \
--priority 100 --direction Inbound --access Allow --protocol Tcp \
--source-address-prefixes Internet --source-port-ranges '*' \
--destination-address-prefixes '*' --destination-port-ranges 443
# Associate to Subnet
az network vnet subnet update -g MyRG --vnet-name MyVNet -n MySubnet \
--network-security-group MyNSG
Terraform (IaC แนะนำ)
resource "azurerm_network_security_group" "example" {
name = "example-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
security_rule {
name = "AllowHTTPSInbound"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.example.id
network_security_group_id = azurerm_network_security_group.example.id
}
🔄 NSG vs Azure Firewall vs ASG
| Feature | NSG | Azure Firewall | ASG |
|---|---|---|---|
| Layer | L3/L4 | L3/L4/L7 (App ID, FQDN, TLS Inspection) | Logical Grouping (Object in NSG) |
| Stateful | Yes | Yes | N/A |
| Threat Intelligence | No | Yes (Microsoft/3rd party) | No |
| SNAT/DNAT | No | Yes | No |
| Cost | Free (Rules only) | ~$1.25/hr + Data | Free |
| Use Case | Micro-segmentation, Basic Filtering | Centralized Egress/Ingress, L7 Inspection | Simplify NSG Rules (App-tier logic) |
🏆 Best Practices
- Associate at Subnet Level (ไม่ใช่ NIC) เพื่อ Consistency และ Manageability
- ใช้ Service Tags แทน Hard-coded IP (เช่น AzureCloud, Storage.Region)
- ใช้ ASG สำหรับ Application Tier (Web, App, DB) ลด Complexity ของ Rules
- Priority Spacing: เว้น 100 (100, 200, 300...) เพื่อ Insert Rule ง่าย
- Explicit Deny All Inbound ท้ายสุด (Priority 4000) ยกเว้นที่จำเป็น
- Log to Log Analytics / Storage Account → Diagnostic Settings → NSG Flow Logs
- Test ก่อน Apply:ใช้ IP Flow Verify (Network Watcher) และ Effective Security Rules
- Automate with IaC (Terraform, Bicep, ARM) + Policy (Azure Policy: Deny NSG w/o Flow Logs, Deny RDP/SSH from Internet)
🔍 Troubleshooting
- Effective Security Rules: ดู Rule ที่奏效จริงบน NIC/Subnet (รวม Default Rules)
- IP Flow Verify: ทดสอบ Source/Dest IP/Port/Protocol ว่า Allow/Deny
- NSG Flow Logs: ส่งไป Log Analytics → KQL Query / Workbook
- Connection Troubleshoot: Network Watcher → Connection Troubleshoot
❌ ไม่สามารถ Rename NSG ได้
ต้องสร้าง NSG ใหม่ → Copy Rules → Associate ใหม่ → ลบเก่า (ใช้ IaC จะง่ายกว่า)
สรุป
NSG = L3/L4 Firewall ฟรีใน Azure ควบคุม Traffic ด้วย Rules (5-tuple) ผูกกับ Subnet (Best Practice) ใช้ Service Tags + ASG ลด Complexity Logging ด้วย Flow Logs ทดสอบด้วย IP Flow Verify ไม่ Rename ได้ ต้องสร้างใหม่ เปรียบเทียบ: NSG (Micro-seg) vs Azure Firewall (Central L7) vs ASG (Logical Grouping)