Network Security Groups Design

Watch the video to deepen your understanding.
SubscribeComplete 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
Lesson: Network Security Group (NSG) Design
Introduction: What is an NSG?
In cloud infrastructure, a Network Security Group (NSG) acts as a virtual firewall for your cloud resources. It contains a list of security rules that allow or deny inbound and outbound network traffic to resources connected to virtual networks (VNets).
Why do we need them? Without NSGs, your cloud resources would be exposed to the public internet or internal lateral movement without any filtering. NSGs provide the "Zero Trust" foundation required to secure cloud environments, ensuring that only authorized traffic reaches your applications, databases, and management interfaces.
How NSGs Work: The Mechanics
An NSG works by evaluating traffic based on a 5-tuple:
- Source/Destination IP address
- Source/Destination Port
- Protocol (TCP, UDP, ICMP, etc.)
- Direction (Inbound or Outbound)
- Priority (Rules are processed in order from lowest number to highest number)
The Priority System
Rules are processed in priority order. Once traffic matches a rule, processing stops. If you have a rule with priority 100 that allows traffic and another with priority 200 that denies it, the traffic will be allowed. Always remember: Lower numbers = Higher priority.
Default Rules
Every NSG comes with default rules that cannot be removed but can be overridden:
- AllowVnetInBound: Allows communication between resources within the same VNet.
- AllowAzureLoadBalancerInBound: Allows health probes from the Azure Load Balancer.
- DenyAllInBound: The final "fail-safe" that blocks everything not explicitly allowed.
Practical Example: Designing a 3-Tier Architecture
Imagine a standard web application architecture consisting of a Web Tier, an App Tier, and a Database Tier. You should not use one single NSG for everything. Instead, apply granular NSGs to each subnet.
1. Web Tier NSG (Public Facing)
- Inbound: Allow Port 80/443 from
Internet(Source). - Outbound: Allow traffic to the App Tier subnet.
2. App Tier NSG (Internal)
- Inbound: Allow Port 8080 from the Web Tier subnet only.
- Outbound: Allow traffic to the Database Tier subnet.
3. Database Tier NSG (Locked Down)
- Inbound: Allow Port 5432 (PostgreSQL) from the App Tier subnet only.
- Outbound: Deny all (or restrict to necessary updates).
Infrastructure as Code (IaC) Implementation
Using Terraform is the industry standard for managing NSGs to ensure consistency and version control.
# Example: Defining an NSG Rule for a Web Subnet
resource "azurerm_network_security_rule" "allow_https" {
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_group_name = azurerm_resource_group.main.name
network_security_group_name = azurerm_network_security_group.web_nsg.name
}
Pro Tip: When using IaC, define your rules in a variable file or a module to avoid hard-coding IP addresses, which can lead to configuration drift.
Best Practices for NSG Design
- Principle of Least Privilege: Never use
Anyor*as a source or destination if you can avoid it. Specify exact subnets or IP ranges. - Use Application Security Groups (ASGs): Instead of managing IP addresses, use ASGs to group virtual machines by function (e.g.,
ASG-Web,ASG-DB). Rules can then reference the ASG name instead of specific IPs. - Log Everything: Enable NSG Flow Logs. This allows you to monitor traffic patterns and perform forensic analysis if a security incident occurs.
- Keep it Simple: Avoid creating hundreds of rules in a single NSG. If an NSG becomes too complex, your architecture is likely too complex; consider segmenting your network into smaller subnets.
- Test with Network Watcher: Use tools like "IP Flow Verify" in Azure Network Watcher to test if a packet is allowed or denied without needing to manually inspect every rule.
Common Pitfalls to Avoid
- The "Deny All" Trap: Creating a custom rule that denies everything at priority 100, effectively blocking your own management access (SSH/RDP) or internal communication.
- Ignoring Outbound Traffic: Many architects focus entirely on Inbound rules. However, egress filtering is critical to prevent compromised servers from "phoning home" to a Command & Control (C2) server.
- Overlapping Rules: Creating conflicting rules where an "Allow" rule is unintentionally overridden by a higher-priority "Deny" rule. Always use the "Effective Security Rules" view in your cloud portal to visualize the final result.
- Hard-coding IPs: Using hard-coded public IPs in rules makes your infrastructure fragile and difficult to migrate. Use Service Tags (e.g.,
Sql,Storage,AzureLoadBalancer) provided by the cloud provider instead.
Key Takeaways
- NSGs are stateful: If you allow inbound traffic, the return traffic is automatically allowed. You do not need to create a matching outbound rule for the return trip.
- Granularity is key: Apply NSGs at the subnet level rather than the individual NIC level whenever possible to maintain easier management and visibility.
- Prioritize security: Always adopt a "Deny by Default" posture. Explicitly allow only the traffic required for the application to function.
- Leverage Automation: Use IaC (Terraform, Bicep, ARM) to deploy NSGs. This prevents human error and ensures that your security posture is reproducible across development, staging, and production environments.
By mastering NSG design, you move from simply "connecting" resources to "securing" them, which is the cornerstone of professional cloud infrastructure engineering.
Reach the last section to complete this lesson and earn points — you're on section 1 of 3.
- Introduction to Azure Monitor
- Azure Monitor Architecture and Data Sources
- Configuring Log Analytics Workspaces
- Designing Log Routing Solutions
- Configuring Diagnostic Settings
- Application Insights for Solution Architects
- Network Watcher and Network Monitoring
- Azure Monitor Alerts and Action Groups
- Workbooks and Custom Dashboards
- Designing a Comprehensive Monitoring Strategy
- Logging and Monitoring Quiz5q
- Microsoft Entra ID for Solution Architects
- Designing Identity Solutions: B2B Collaboration
- Designing Identity Solutions: B2C Scenarios
- Conditional Access Policy Design
- Designing for Multi-Factor Authentication
- Managed Identities for Azure Resources
- Service Principals and App Registrations
- Role-Based Access Control Design
- Privileged Identity Management
- Microsoft Entra ID Protection
- Zero Trust Architecture with Microsoft Entra
- Authentication and Authorization Quiz5q
- Introduction to Azure Governance
- Designing Management Group Hierarchies
- Subscription Strategy Design
- Resource Group Organization Patterns
- Azure Policy Design and Assignment
- Custom Policy Definitions and Initiatives
- Resource Locks and Tagging Strategies
- Azure Blueprints and Landing Zones
- Cost Management and Budget Design
- Cloud Adoption Framework for Governance
- Governance Solutions Quiz5q
- Introduction to Azure Storage
- Storage Account Types and Replication
- Blob Storage Tiers and Lifecycle Management
- Azure Files and Azure NetApp Files
- Azure Managed Disks Design
- Azure Data Lake Storage Gen2
- Cosmos DB Consistency Models
- Cosmos DB Partitioning and Throughput Design
- Cosmos DB API Selection Guide
- Table Storage and Queue Storage Design
- Storage Security and Encryption
- Non-Relational Storage Quiz5q
- Azure SQL Database Service Tiers
- Azure SQL Managed Instance Design
- Azure Database for MySQL and PostgreSQL
- Database Scaling: Vertical and Horizontal
- Read Replicas and Geo-Replication
- Database Security and Auditing Design
- Transparent Data Encryption and Always Encrypted
- Caching with Azure Cache for Redis
- Azure SQL Elastic Pools Design
- Relational Storage Quiz5q
- Azure Data Factory Design Patterns
- Data Integration Pipeline Architecture
- Azure Synapse Analytics Design
- Azure Databricks Integration Patterns
- Azure Stream Analytics for Real-Time Data
- Azure Event Hubs for Data Ingestion
- Data Migration Strategies and Tools
- Azure Purview for Data Governance
- Data Integration Quiz5q
- Introduction to High Availability in Azure
- Availability Zones and Availability Sets
- Azure Load Balancer Design
- Application Gateway and WAF Design
- Azure Front Door and Global Load Balancing
- Azure Traffic Manager Routing Methods
- Multi-Region Architecture Design
- SLA Design and Composite SLAs
- Health Probes and Failover Configuration
- Azure Service Fabric for Stateful HA
- High Availability Quiz5q
- Azure Backup Architecture and Vaults
- Backup Policies for VMs and Databases
- Azure Site Recovery Design
- RTO and RPO Planning Strategies
- Geo-Redundant and Cross-Region Recovery
- Hybrid and On-Premises Backup Solutions
- Resiliency Patterns and Chaos Engineering
- Disaster Recovery Testing and Drills
- Azure Immutable Backup and Soft Delete
- Backup and Disaster Recovery Quiz5q
- Introduction to Azure Compute Options
- Virtual Machine Design and Sizing
- VM Scale Sets and Autoscaling Strategies
- Azure Batch for Large-Scale Workloads
- Azure App Service Plans and Design
- App Service Environments and Isolation
- Azure Container Instances
- Azure Kubernetes Service Architecture
- AKS Networking and Storage Design
- Azure Functions and Serverless Design
- Durable Functions and Orchestration
- Compute Decision Framework
- Azure Virtual Desktop Design
- Compute Solutions Quiz5q
- Microservices Architecture Patterns
- Azure API Management Design
- Azure Service Bus Messaging Design
- Azure Event Grid and Event-Driven Architecture
- Azure Event Hubs for Streaming
- Azure Logic Apps and Integration Workflows
- Azure SignalR and Web PubSub
- Caching Strategies and Azure CDN
- App Configuration and Feature Flags
- Designing for Scalability and Performance
- Azure Container Apps Design
- Application Architecture Quiz5q
- Virtual Network Design and Address Planning
- Subnet Design and Network Segmentation
- Hub-Spoke Network Topology
- Azure Virtual WAN Design
- VPN Gateway Design and Configuration
- ExpressRoute Circuit Design
- Network Security Groups Design
- Azure Firewall and Firewall Manager
- Azure DDoS Protection Design
- Private Endpoints and Private Link
- Azure DNS and DNS Architecture
- Network Performance and Traffic Routing
- Azure Bastion and Secure Access
- Network Solutions Quiz5q
- Azure Migrate Overview and Assessment
- Migration Assessment and Discovery
- Azure Cloud Adoption Framework for Migration
- VM Migration with Azure Migrate
- Database Migration with Azure DMS
- Application Migration to App Service
- Containerizing Applications for Migration
- Migration Cost Planning and Optimization
- Data Box and Offline Migration Methods
- Migrations Quiz5q
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