Lesson 15 — Network Security Groups & Application Security Groups
Why You Need to Understand This
Network Security Groups are the primary traffic-filtering mechanism for IaaS workloads in Azure. Every production VM should have NSG rules controlling what can reach it and what it can reach — full stop. Misconfigured NSGs are the most common root cause of "my application can't connect" tickets and, at the other extreme, "our database was exposed to the internet" incidents.
On the AZ-104 exam, NSG questions are consistently among the highest-yield networking topics. You must understand rule evaluation order, the stateful nature of NSGs, service tags, the ASG model, and how to troubleshoot using effective security rules and IP flow verify. All of these appear regularly in scenario questions.
NSG Fundamentals
An NSG (Network Security Group) is a stateful, Layer 4 packet filter. It contains a list of inbound security rules and a list of outbound security rules. Each rule either allows or denies traffic matching the specified criteria. NSGs operate at the network layer — they see IP headers and TCP/UDP port numbers, not application-layer content.
Where NSGs can be attached
- Subnet: the NSG applies to all resources (VMs, private endpoints, etc.) in the subnet. All traffic entering or leaving the subnet is evaluated against the NSG's rules.
- NIC (Network Interface Card): the NSG applies to a single VM's network interface. Provides per-VM granularity within a shared subnet.
- Both can be applied simultaneously to the same VM — inbound traffic passes through the subnet NSG first, then the NIC NSG. Outbound traffic passes through the NIC NSG first, then the subnet NSG.
Security Rule Properties
Every NSG rule has these configurable properties:
| Property | Values | Notes |
|---|---|---|
| Priority | 100 – 4096 | Lower number = higher priority. Rules are evaluated in ascending priority order; first match wins. |
| Name | String (unique per NSG) | Descriptive names improve operational clarity: Allow-HTTPS-From-Internet |
| Source | Any, IP/CIDR, Service Tag, ASG | Where traffic originates |
| Source port range | Port number, range, or * | Usually * for inbound (client ports are ephemeral); specific ports for outbound |
| Destination | Any, IP/CIDR, Service Tag, ASG | Where traffic is going |
| Destination port range | Port number, range (80,443), or * | The service port the rule is protecting |
| Protocol | TCP, UDP, ICMP, ESP, AH, Any | Use TCP for web traffic, ICMP for ping rules |
| Action | Allow / Deny | What happens to matching traffic |
Default Rules (Always Present)
Every NSG is created with a fixed set of default rules that cannot be deleted. They sit at very high priority numbers (65000+) and represent the minimum baseline behaviour. You can override them by creating your own rules with lower priority numbers.
Inbound default rules
| Rule name | Priority | Source | Destination | Action |
|---|---|---|---|---|
| AllowVnetInBound | 65000 | VirtualNetwork | VirtualNetwork | Allow |
| AllowAzureLoadBalancerInBound | 65001 | AzureLoadBalancer | Any | Allow |
| DenyAllInBound | 65500 | Any | Any | Deny |
Outbound default rules
| Rule name | Priority | Source | Destination | Action |
|---|---|---|---|---|
| AllowVnetOutBound | 65000 | VirtualNetwork | VirtualNetwork | Allow |
| AllowInternetOutBound | 65001 | Any | Internet | Allow |
| DenyAllOutBound | 65500 | Any | Any | Deny |
AzureLoadBalancer service tag. If health probes are blocked, the backend VM appears unhealthy and the Load Balancer stops sending it traffic — even if your application is running perfectly. Always include an explicit Allow rule for AzureLoadBalancer if you are locking down a load-balanced subnet.
NSG Attachment & Rule Evaluation
Understanding the order in which NSGs are evaluated is essential for troubleshooting connectivity and passing exam scenario questions.
Inbound traffic path (e.g. internet → VM)
Outbound traffic path (e.g. VM → internet)
Effective security rules
When a VM has both a subnet NSG and a NIC NSG, seeing the actual combined ruleset can be difficult. Use Effective security rules — found in the VM's NIC blade under Support + troubleshooting → Effective security rules. This view shows the fully merged, prioritised inbound and outbound rule set that Azure is actually enforcing on that NIC.
Service Tags
Service tags are named groups of IP address prefixes managed and updated by Microsoft. Instead of maintaining static IP address lists in your NSG rules, you reference a service tag — Microsoft keeps the underlying IP ranges current as Azure services grow and change infrastructure.
Key service tags to memorise
| Service tag | Represents | Common use |
|---|---|---|
VirtualNetwork | All VNet address space + peered VNets + on-prem connected ranges | Allow intra-VNet communication (default rule) |
Internet | All public IP addresses not within VirtualNetwork | Allow/deny internet access |
AzureLoadBalancer | Azure health probe source (168.63.129.16) | Allow load balancer health probes |
Storage | Azure Storage service IP ranges | Allow outbound to Storage without enumerating IPs |
Sql | Azure SQL Database and Azure Synapse IP ranges | Allow outbound to SQL service endpoints |
AzureMonitor | Log Analytics, Application Insights endpoints | Allow diagnostic data outbound from VMs |
AppService | App Service and Function App inbound IP ranges | Restrict inbound to App Service outbound IPs |
EventHub | Azure Event Hubs service IP ranges | Allow outbound to Event Hub |
KeyVault | Azure Key Vault service IP ranges | Allow outbound to Key Vault |
Application Security Groups (ASGs)
Application Security Groups let you define security rules based on application role rather than IP address. Instead of writing rules like "allow 10.20.1.4 and 10.20.1.5 and 10.20.1.6 to reach 10.20.2.4 on port 8080", you write "allow asg-webservers to reach asg-appservers on port 8080".
How ASGs work
- Create an ASG resource (e.g.
asg-webservers,asg-appservers,asg-dbservers). - Assign VM NICs to ASGs — a NIC can belong to multiple ASGs.
- Reference ASGs as source or destination in NSG rules instead of IP addresses.
- When a VM is added to or removed from an ASG, the NSG rules automatically apply or cease to apply — no rule editing required.
Example NSG rules using ASGs
Rule 100 Inbound Allow:
Source: asg-webservers Destination: asg-appservers Port: 8080 Protocol: TCP
Rule 110 Inbound Allow:
Source: asg-appservers Destination: asg-dbservers Port: 1433 Protocol: TCP
Rule 4000 Inbound Deny:
Source: * Destination: asg-dbservers Port: * Protocol: *
With these rules, web servers can reach app servers, app servers can reach the database — and nothing else can reach the database directly. When you scale out by adding new web VMs, you assign their NICs to asg-webservers and rule 100 immediately applies. Zero rule changes required.
ASG vs IP-based rules — when to use each
| Scenario | Use ASGs | Use IP/CIDR |
|---|---|---|
| VM-to-VM within same VNet | Yes — role-based, scale-friendly | Works but brittle to IP changes |
| On-premises to Azure | Not applicable | Yes — use on-prem CIDR ranges |
| Cross-VNet (peered) | Not supported across VNets | Yes — use the remote VNet CIDR |
| Azure service (Storage, SQL) | Not applicable | Use Service Tags instead |
| Dynamic VM fleets (VMSS, auto-scale) | Yes — new instances auto-inherit rules | Impractical — IPs change constantly |
NSG Flow Logs & Traffic Analytics
NSG Flow Logs capture metadata about every accepted and denied flow through an NSG. This is not packet capture — it is flow-level telemetry (source IP, destination IP, source port, destination port, protocol, direction, action).
Flow log versions
- Version 1: basic flow information — 5-tuple + direction + action.
- Version 2: adds bytes transferred and packets per flow, enabling bandwidth analysis and anomaly detection.
Storage and processing
- Flow logs are written as JSON to a Storage Account you specify. Each log entry represents a flow within a 1-minute aggregation window.
- Traffic Analytics processes flow logs through a Log Analytics Workspace to produce visual dashboards, geographic traffic maps, threat detection, and top-talker analysis. It ingests and aggregates flow data at configurable intervals (every 10 minutes or every 60 minutes).
Troubleshooting NSG Issues
NSG troubleshooting has a standard toolkit in Azure. Know all three tools for the exam.
Tool 1 — IP Flow Verify (Network Watcher)
Tests whether a hypothetical flow would be allowed or denied by the NSGs on a VM's NIC. You specify: VM, NIC, direction (inbound/outbound), source IP, destination IP, protocol, and port. Azure evaluates all NSGs and returns the first matching rule and its action (allow/deny).
Use when: "I need to know if TCP 443 from 1.2.3.4 to my VM would be allowed right now, without actually sending traffic."
Tool 2 — Effective Security Rules
Shows the merged, sorted list of all NSG rules actually applied to a specific NIC — combining rules from both the subnet NSG and the NIC NSG in a single view, sorted by priority. Available in the NIC blade under Support + troubleshooting.
Use when: "Traffic is being blocked and I can't figure out which rule is doing it."
Tool 3 — NSG Flow Logs + Traffic Analytics
Provides historical analysis of what traffic actually flowed (and was denied) through an NSG. Unlike IP Flow Verify (which is hypothetical), flow logs show real traffic from the past.
Use when: "An incident occurred 3 hours ago and I need to know what traffic was allowed or denied."
AZ-104 Exam Traps
AzureLoadBalancer service tag. Health probes come from 168.63.129.16 — if this is blocked, the VM appears down to the Load Balancer. The fix is to add an Allow rule for source AzureLoadBalancer above any DenyAll rule.
Check Your Understanding
Click any option to see immediate feedback. Answers reflect real Azure behaviour.
1. An NSG allows inbound TCP on port 443 from the Internet. There is no outbound rule for TCP 443. Will HTTPS responses from the VM reach external clients?
2. VMs behind a Standard Load Balancer are marked unhealthy in the Load Balancer's backend health view, even though the application is responding normally. What is the most likely NSG-related cause?
AzureLoadBalancer service tag. If an NSG on the subnet or NIC has a DenyAll rule or any deny rule that blocks this source IP, health probes cannot reach the VMs. The Load Balancer marks the backend as unhealthy and stops sending real traffic to it. The fix is to add an explicit inbound Allow rule for the AzureLoadBalancer service tag at a priority lower than the blocking deny rule.3. A VM has a subnet NSG (allows TCP 80) and a NIC NSG (has no rule for TCP 80, only a DenyAllInBound at priority 4000). Inbound HTTP traffic arrives. What happens?
4. Your team manages a fleet of web VMs and app VMs. You want NSG rules to allow web VMs to reach app VMs on port 8080, with no rule changes required when VMs are added or replaced. What is the optimal approach?
asg-webservers and asg-appservers, then write one NSG rule: Allow source=asg-webservers, destination=asg-appservers, port=8080, TCP. When new web VMs are provisioned, assign their NICs to asg-webservers — the rule immediately applies without any modification. Option B works but lacks precision — it allows any VM in the /24 range to reach the app tier, not just web servers.5. You need to audit which traffic was blocked by an NSG on a production VM during a security incident that occurred 2 hours ago. Which tool provides this information?
6. You want to allow outbound traffic from VMs in a subnet to Azure Storage without maintaining a static list of Azure Storage IP ranges that might change. What should you use as the destination in the NSG rule?
Storage service tag represents all Azure Storage service IP ranges and is automatically updated by Microsoft when those ranges change. Using it means your NSG rule remains correct even as Microsoft adds new Storage infrastructure. Hard-coding IP ranges (option A) creates operational burden and fails silently when Microsoft adds new IPs that aren't in your list. The Internet tag (option D) is too broad — it would also allow access to non-Azure internet destinations.Read the full overview article and the linked Application Security Groups article. The Microsoft documentation on effective security rules and how both subnet and NIC NSGs are evaluated is particularly important for exam scenarios.
This lesson covered NSG fundamentals, default rules, ASGs, flow logs, and troubleshooting. Go deeper on:
- Walk me through the complete NSG rule evaluation sequence when a packet arrives at a VM with both a subnet NSG and a NIC NSG — include the exact order of evaluation for inbound vs outbound.
- How do I design NSG rules for a three-tier application (web, app, data) using ASGs, minimising the number of rules while maximising security?
- Explain how Traffic Analytics works internally — what does it do with flow log JSON blobs and what queries does it run in Log Analytics?
- When should I use NSG service endpoints vs Private Endpoints for securing access to Azure Storage, and how does the NSG rule change?