Planning and Implementing NSGs
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Module: Secure Networking
Section: Security for Virtual Networks
Lesson Title: Planning and Implementing Network Security Groups (NSGs)
Introduction: The Foundation of Virtual Perimeter Defense
In the early days of cloud computing, many organizations treated their virtual networks like the Wild West. They assumed that because their assets were hosted in a data center managed by a cloud provider, the infrastructure was inherently secure. We have since learned that the "Shared Responsibility Model" dictates that while the provider secures the physical hardware, the customer is responsible for securing the traffic flowing within their virtual networks. This is where Network Security Groups (NSGs) come into play.
An NSG acts as a virtual firewall for your cloud resources. It is a set of filtering rules that allow or deny network traffic to and from resources connected to a virtual network. Without an NSG, your virtual machines (VMs) or database instances might be exposed to the public internet or, worse, to unauthorized internal traffic. Planning and implementing these groups is not just a "nice-to-have" security measure; it is the fundamental building block of a zero-trust architecture. By mastering NSGs, you ensure that only the traffic necessary for your application to function can reach your critical assets, effectively shrinking your attack surface.
Understanding the Anatomy of an NSG
To effectively plan your network security, you must first understand how an NSG processes traffic. An NSG contains a list of security rules that are evaluated by priority. When traffic enters or leaves a subnet or a specific network interface, the NSG inspects the packet against these rules. If a rule matches the traffic properties—such as source IP, destination IP, port, and protocol—the action specified (Allow or Deny) is taken.
The Rule Processing Logic
The most important thing to remember about NSG rules is that they are processed in order of priority, starting from the lowest number (e.g., 100) to the highest number (e.g., 4096). Once a match is found, processing stops. This means that if you have a rule at priority 100 that allows traffic and a rule at priority 200 that denies traffic, the traffic will be allowed. If you accidentally place a "Deny All" rule at priority 100, you will effectively block all traffic, regardless of any "Allow" rules you have at higher priority numbers.
Callout: The "Default" Rules Reality Every NSG comes with a set of default rules that cannot be deleted, only overridden. These rules allow traffic between resources within the same virtual network, allow traffic from a load balancer, and deny all other inbound traffic. Understanding these defaults is critical because they provide a "deny by default" posture for the internet while maintaining utility for internal communication.
Strategic Planning: Designing Your NSG Architecture
Before you start clicking through a cloud console to create rules, you need a strategy. A poorly planned NSG configuration is often more dangerous than no configuration at all because it provides a false sense of security while creating complex management overhead.
1. Define Traffic Flows
Start by mapping your application architecture. Identify exactly which components need to talk to each other. For example, does your Web Server need to talk to the Database? Yes. Does the Database need to talk to the Internet? Absolutely not. Does the Web Server need to receive traffic from the internet? Only on ports 80 and 443. Documenting these flows in a spreadsheet or a diagram will save you hours of troubleshooting later.
2. Adopt the Principle of Least Privilege
Apply the principle of least privilege to your network rules. If a service only needs to communicate over port 5432 for PostgreSQL, do not open a broad range of ports. If a service only needs to receive traffic from a specific application subnet, restrict the source IP range to that subnet's CIDR block rather than using "Any."
3. Group by Role, Not by Instance
Organize your NSGs based on the role of the resource. Instead of creating an NSG for "VM-01," create an NSG for "Web-Tier" or "Database-Tier." This allows you to scale your environment by simply attaching new VMs to the appropriate NSG without having to rewrite firewall rules every time you add or remove an instance.
Implementing NSGs: Step-by-Step
Let us walk through the implementation of a standard three-tier architecture (Web, App, and Database) using a hypothetical cloud environment.
Step 1: Create the NSG
You should create separate NSGs for each tier. This provides granular control.
- Create
NSG-Web: Configure rules to allow inbound traffic on 80/443 from the Internet. - Create
NSG-App: Configure rules to allow inbound traffic on specific ports from theNSG-Websubnet. - Create
NSG-DB: Configure rules to allow inbound traffic on the database port only from theNSG-Appsubnet.
Step 2: Defining the Rules (Code Example)
While consoles are great for learning, infrastructure as code (IaC) is the industry standard for production environments. Below is a conceptual example of how you might define these rules in a JSON-based configuration file.
{
"name": "Allow-Web-To-DB",
"properties": {
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "5432",
"sourceAddressPrefix": "10.0.2.0/24",
"destinationAddressPrefix": "10.0.3.0/24",
"access": "Allow",
"priority": 110,
"direction": "Inbound"
}
}
Explanation:
protocol: We specify "Tcp" because database traffic typically uses TCP.sourceAddressPrefix: This is the CIDR block of our Application subnet. We are restricting access so that only servers in that specific subnet can reach the database.destinationAddressPrefix: This is the CIDR block of our Database subnet.priority: By setting this to 110, we ensure it is processed before any catch-all deny rules that might exist at higher priorities.
Note: Always use specific CIDR blocks rather than "Any" (0.0.0.0/0). Using "Any" is the leading cause of unauthorized access in cloud environments. If you must allow internet traffic, narrow it down to the smallest possible range of source IPs.
Best Practices and Industry Standards
Implementing NSGs effectively requires a disciplined approach. Here are the standards used by security-conscious organizations:
- Centralized Logging: Enable flow logs for your NSGs. Flow logs capture information about the IP traffic flowing through your NSG. This data is invaluable for auditing and for troubleshooting blocked connections. If an application stops working, you can check the logs to see if a specific rule is dropping the packets.
- Avoid "Rule Bloat": Regularly audit your NSG rules. Over time, administrators often add "temporary" rules to fix connection issues and forget to remove them. Schedule a quarterly review to identify and delete unused rules.
- Use Tags for Metadata: Tag your NSGs with clear names and descriptions. If you have 50 different NSGs, a name like "NSG-Production-Web-Tier" is far more useful than "NSG-1."
- Test in Staging: Never apply a restrictive NSG rule directly to production without testing it in a staging environment. It is very easy to accidentally lock yourself out of your own servers by blocking SSH or RDP traffic.
Common Pitfalls and Troubleshooting
Even experienced engineers run into issues with NSGs. Here is how to navigate the most common traps.
1. The "Lockout" Scenario
A common mistake is creating a rule that blocks all traffic without accounting for management protocols like SSH (port 22) or RDP (port 3389). If you apply a "Deny All" rule, you will immediately lose administrative access to your VMs.
- The Fix: Always keep an "Allow" rule for administrative management ports at a very high priority, or use a Bastion host/Jump box that has specific access, and block direct public access to your VMs.
2. The "Order of Operations" Trap
As mentioned earlier, NSGs process rules in priority order. If you have a rule that allows HTTP traffic at priority 500, but a later rule denies all web traffic at priority 400, your HTTP traffic will be blocked.
- The Fix: Use increments of 100 for your priorities (100, 200, 300). This leaves you "room" to insert new rules in between existing ones if your requirements change, without having to renumber your entire rule set.
3. Overlooking Asymmetric Routing
Sometimes traffic flows into a subnet but cannot get back out. Remember that NSGs are stateful. This means if you allow an inbound request, the return traffic is automatically allowed. However, if you have complex routing (like a virtual appliance or a firewall between subnets), the return traffic might take a different path.
- The Fix: Ensure that your NSG rules are applied to both the inbound and outbound traffic if your network topology involves complex routing, though in most standard cloud setups, stateful tracking handles this for you.
| Feature | NSG (Network Security Group) | ASG (Application Security Group) |
|---|---|---|
| Scope | Subnet or Network Interface | Network Interface only |
| Identification | Based on IP addresses/CIDR | Based on tags/logical groupings |
| Complexity | Higher (requires IP management) | Lower (easier to manage at scale) |
| Use Case | Perimeter and Subnet defense | Fine-grained application-level rules |
Advanced Configuration: Integrating ASGs
As your environment grows, managing IP addresses in NSG rules becomes tedious. This is where Application Security Groups (ASGs) become essential. ASGs allow you to group your VMs into logical categories (e.g., "WebServers," "DatabaseServers").
Instead of writing a rule that says "Allow traffic from 10.0.2.0/24 to 10.0.3.0/24," you can write a rule that says "Allow traffic from ASG-WebServers to ASG-DatabaseServers." This is much easier to read and maintain. When you add a new VM to the "WebServers" ASG, it automatically inherits the security rules associated with that group.
Callout: Why Logic Beats IPs Using IP addresses in rules is fragile. IPs change, subnets are re-architected, and documentation often falls behind. Using ASGs allows you to build security policies based on what the server does rather than where it lives. This is the difference between a static, brittle network and a dynamic, resilient one.
Auditing and Compliance
Security is not a "set it and forget it" task. You must treat your NSG configuration as a living document. Many compliance frameworks (such as SOC2, HIPAA, or PCI-DSS) require regular reviews of firewall rules.
- Automated Auditing: Use built-in cloud security tools to scan your NSGs for "overly permissive rules." Many cloud platforms have a "Security Advisor" or "Compliance Center" that will flag rules like "Allow SSH from 0.0.0.0/0."
- Version Control: If you use IaC (Terraform, Bicep, CloudFormation), keep your NSG definitions in a Git repository. This provides a clear audit trail of who changed a rule, when they changed it, and why.
- Drift Detection: If someone manually changes an NSG rule in the portal, your IaC tool should be able to detect "drift" and revert the change to match your approved code-based configuration.
Scenario: Troubleshooting a Blocked Connection
Imagine a user reports that they cannot connect to the internal web application. Here is the step-by-step process you should use to diagnose the NSG issue:
- Verify the Network Path: Is the user hitting the right IP address? Is there a Load Balancer in front of the VM?
- Check the NSG Rules: Look at the specific NSG attached to the VM's network interface. Are there any "Deny" rules that might be catching the traffic?
- Use "IP Flow Verify": Most cloud providers have a tool called "IP Flow Verify" or "Network Watcher." This tool allows you to simulate a packet flow from a source IP/Port to a destination IP/Port. It will tell you exactly which rule is allowing or denying the traffic.
- Check Flow Logs: If the simulation says "Allowed" but the user still cannot connect, check the Flow Logs to see if the traffic is actually reaching the interface. If you don't see any entries for that IP, the traffic is being blocked somewhere else, perhaps by a route table or an upstream firewall.
The Evolution of Cloud Security: Moving Beyond NSGs
While NSGs are the backbone of virtual network security, they are just one layer. In a mature cloud security posture, you should also consider:
- Application Gateways / WAFs: NSGs operate at the network layer (Layer 3/4). They cannot inspect the contents of an HTTP request. A Web Application Firewall (WAF) is required to protect against SQL injection or Cross-Site Scripting (XSS).
- Private Links: Instead of exposing services to the VNet, use Private Link services to make resources accessible via a private IP address only, completely removing them from the public routing table.
- Identity-Based Networking: The future of networking is moving away from IP-based rules entirely and toward identity-based access. In this model, a user or a service must prove their identity (via tokens or certificates) to gain access, regardless of what network they are on.
Common Questions (FAQ)
Q: Can I have multiple NSGs on a single VM? A: Yes, you can associate an NSG with a subnet and another NSG with a specific network interface. The traffic is evaluated by both. The rule of thumb is that the most restrictive rule wins across the chain.
Q: Does an NSG block traffic between VMs in the same subnet? A: Yes, if you have rules that deny internal traffic. However, the default rules usually allow communication within the same subnet. You must explicitly add a "Deny" rule if you want to isolate VMs within the same subnet.
Q: Is there a limit to the number of rules I can have in an NSG? A: Yes, every provider has a limit (often 1000-4000 rules). If you find yourself hitting this limit, you likely have an architecture problem. Consider consolidating rules or using ASGs to simplify.
Q: What happens if I delete an NSG? A: You cannot delete an NSG if it is currently associated with a subnet or a network interface. You must disassociate it first. Always be careful when doing this, as disassociating an NSG could leave your resources wide open to the default network settings.
Key Takeaways for Success
Implementing Network Security Groups is a critical skill for any cloud practitioner. By following these core principles, you will ensure a secure and manageable environment:
- Start with "Deny All" and Punch Holes: Always begin with a restrictive posture. Only open the specific ports and protocols necessary for your application to function.
- Prioritize Your Rules: Use the priority system effectively. Leave gaps in your numbering (e.g., 100, 200, 300) to allow for future rule insertions without needing to reorder the entire list.
- Leverage ASGs: Move away from IP-based management as soon as possible. Using Application Security Groups makes your security policy more readable, scalable, and resilient to infrastructure changes.
- Automate and Audit: Use Infrastructure as Code to manage your NSGs. This provides a single source of truth, allows for version control, and makes auditing compliance much easier.
- Use Diagnostic Tools: Don't guess why traffic is being blocked. Utilize built-in simulation tools like "IP Flow Verify" to get definitive answers on how your rules are impacting traffic.
- Don't Forget Management Access: Always ensure that your administrative access (SSH/RDP) is secured and not blocked by your own rules. Use Bastion hosts or VPNs to limit exposure to management ports.
- Monitor with Flow Logs: Enable logging for all production NSGs. This is your primary source of truth when investigating security incidents or performance issues.
By treating your NSGs as a core part of your application’s infrastructure rather than an afterthought, you create a robust perimeter that protects your data and services. Security is a continuous process of refinement, and your NSG configuration should evolve alongside your application. Always stay curious, keep your rules clean, and never stop auditing your network perimeter.
Reach the last section to complete this lesson and earn points — you're on section 1 of 12.
- Understanding Azure Built-in Role Assignments
- Understanding Azure Built-in Role Assignments5q
- Managing Custom Azure Roles
- Managing Custom Azure Roles5q
- Creating Custom Microsoft Entra Roles
- Creating Custom Microsoft Entra Roles5q
- Planning Privileged Identity Management
- Planning Privileged Identity Management5q
- Configuring PIM Settings and Assignments
- Configuring PIM Settings and Assignments5q
- Implementing Multi-Factor Authentication
- Implementing Multi-Factor Authentication5q
- Implementing Conditional Access Policies
- Implementing Conditional Access Policies5q
- Advanced Conditional Access Scenarios
- Advanced Conditional Access Scenarios5q
- Managing Enterprise Application Access
- Managing Enterprise Application Access5q
- Managing OAuth Permission Grants
- Managing OAuth Permission Grants5q
- Configuring App Registration Permission Scopes
- Configuring App Registration Permission Scopes5q
- Managing Service Principals
- Managing Service Principals5q
- Managing Managed Identities
- Managing Managed Identities5q
- Planning and Implementing NSGs
- Planning and Implementing NSGs5q
- Implementing Application Security Groups
- Implementing Application Security Groups5q
- Managing VNets with Virtual Network Manager
- Managing VNets with Virtual Network Manager5q
- Implementing User-Defined Routes
- Implementing User-Defined Routes5q
- Configuring VNet Peering and VPN Gateway
- Configuring VNet Peering and VPN Gateway5q
- Implementing Virtual WAN and Secured Hub
- Implementing Virtual WAN and Secured Hub5q
- Securing VPN and ExpressRoute Connectivity
- Securing VPN and ExpressRoute Connectivity5q
- Monitoring with Network Watcher
- Monitoring with Network Watcher5q
- Implementing Service Endpoints
- Implementing Service Endpoints5q
- Implementing Private Endpoints
- Implementing Private Endpoints5q
- Implementing Private Link Services
- Implementing Private Link Services5q
- Network Integration for App Service and Functions
- Network Integration for App Service and Functions5q
- Network Security for ASE and SQL Managed Instance
- Network Security for ASE and SQL Managed Instance5q
- Implementing TLS for Applications
- Implementing TLS for Applications5q
- Planning and Implementing Azure Firewall
- Planning and Implementing Azure Firewall5q
- Managing Firewall Policies with Azure Firewall Manager
- Managing Firewall Policies with Azure Firewall Manager5q
- Implementing Azure Application Gateway
- Implementing Azure Application Gateway5q
- Implementing Azure Front Door and CDN
- Implementing Azure Front Door and CDN5q
- Implementing Web Application Firewall
- Implementing Web Application Firewall5q
- Implementing Azure DDoS Protection
- Implementing Azure DDoS Protection5q
- Remote Access with Azure Bastion
- Remote Access with Azure Bastion5q
- Implementing Just-in-Time VM Access
- Implementing Just-in-Time VM Access5q
- Configuring AKS Network Isolation
- Configuring AKS Network Isolation5q
- Securing and Monitoring AKS
- Securing and Monitoring AKS5q
- AKS Authentication Configuration
- AKS Authentication Configuration5q
- Security Monitoring for Container Instances and Apps
- Security Monitoring for Container Instances and Apps5q
- Managing Access to Azure Container Registry
- Managing Access to Azure Container Registry5q
- Configuring Disk Encryption Options
- Configuring Disk Encryption Options5q
- Security for Azure API Management
- Security for Azure API Management5q
- Configuring Storage Account Access Control
- Configuring Storage Account Access Control5q
- Managing Storage Account Access Keys
- Managing Storage Account Access Keys5q
- Configuring Access to Azure Files
- Configuring Access to Azure Files5q
- Configuring Access to Azure Blob Storage
- Configuring Access to Azure Blob Storage5q
- Data Protection with Soft Delete and Versioning
- Data Protection with Soft Delete and Versioning5q
- Configuring BYOK and Infrastructure Encryption
- Configuring BYOK and Infrastructure Encryption5q
- Enabling Microsoft Entra Database Authentication
- Enabling Microsoft Entra Database Authentication5q
- Enabling Database Auditing
- Enabling Database Auditing5q
- Implementing Dynamic Data Masking
- Implementing Dynamic Data Masking5q
- Implementing Transparent Data Encryption
- Implementing Transparent Data Encryption5q
- Using Azure SQL Always Encrypted
- Using Azure SQL Always Encrypted5q
- Creating and Assigning Azure Policies
- Creating and Assigning Azure Policies5q
- Interpreting Azure Policy Initiatives
- Interpreting Azure Policy Initiatives5q
- Configuring Key Vault Network Settings
- Configuring Key Vault Network Settings5q
- Configuring Key Vault Access Policies and RBAC
- Configuring Key Vault Access Policies and RBAC5q
- Managing Certificates Secrets and Keys
- Managing Certificates Secrets and Keys5q
- Configuring Key Rotation and Backup
- Configuring Key Rotation and Backup5q
- Implementing Backup Security Controls
- Implementing Backup Security Controls5q
- Using Secure Score and Inventory
- Using Secure Score and Inventory5q
- Managing Compliance Standards
- Managing Compliance Standards5q
- Adding Custom Standards to Defender
- Adding Custom Standards to Defender5q
- Connecting Multi-Cloud Environments
- Connecting Multi-Cloud Environments5q
- Using External Attack Surface Management
- Using External Attack Surface Management5q
- Enabling Cloud Workload Protection Plans
- Enabling Cloud Workload Protection Plans5q
- Configuring Defender for Servers
- Configuring Defender for Servers5q
- Configuring Defender for Databases and Storage
- Configuring Defender for Databases and Storage5q
- Implementing Agentless VM Scanning
- Implementing Agentless VM Scanning5q
- Managing Vulnerability Management for VMs
- Managing Vulnerability Management for VMs5q
- Configuring DevOps Security
- Configuring DevOps Security5q
- Managing Security Alerts
- Managing Security Alerts5q
- Configuring Workflow Automation
- Configuring Workflow Automation5q
- Configuring Data Collection Rules
- Configuring Data Collection Rules5q
- Configuring Sentinel Data Connectors
- Configuring Sentinel Data Connectors5q
- Enabling Analytics Rules in Sentinel
- Enabling Analytics Rules in Sentinel5q
- Configuring Automation in Sentinel
- Configuring Automation in Sentinel5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons