Task 06: Network Security Groups
DOCUMENT CATEGORY: Runbook
SCOPE: NSG deployment and association
PURPOSE: Secure management and bastion subnets with NSG rules
MASTER REFERENCE: Microsoft Learn - NSG Overview
Status: Active
Overview
This task creates two Network Security Groups (NSGs) and associates them with the management subnet and AzureBastionSubnet. These NSGs enforce least-privilege network access for Bastion traffic, CI/CD runner access, and on-premises connectivity.
Execution Target: Azure-Only (control-plane API operation) Tab Profile: 3 tabs — Azure Portal · PowerShell · Azure CLI
Module: azurelocal-toolkit
File: nsg.tf
Mode: Management
Components Created
| Resource | Name Pattern | Purpose |
|---|---|---|
| NSG (Management) | nsg-azrl-{env}-{region}-01 | Management subnet security |
| NSG (Bastion) | nsg-bastion-{env}-{region}-01 | AzureBastionSubnet security |
| Subnet Association (2x) | — | Bind NSGs to subnets |
Management NSG Rules
| Priority | Name | Direction | Source | Destination Ports | Action |
|---|---|---|---|---|---|
| 110 | Bastion-SSH-RDP-Inbound | Inbound | AzureBastionSubnet CIDR | 22, 3389 | Allow |
| 120 | Azure Local Cloud-Runners-SSH-Inbound | Inbound | CI/CD runner subnet | 22 | Allow |
| 130 | Azure-Local-OnPrem-Inbound | Inbound | On-prem mgmt + OOB CIDRs | * | Allow |
Bastion NSG Rules (Microsoft Required)
| Priority | Name | Direction | Source | Destination Ports | Action |
|---|---|---|---|---|---|
| 120 | AllowHttpsInbound | Inbound | Internet | 443 | Allow |
| 130 | AllowGatewayManagerInbound | Inbound | GatewayManager | 443 | Allow |
| 140 | AllowAzureLoadBalancerInbound | Inbound | AzureLoadBalancer | 443 | Allow |
| 150 | AllowBastionHostCommunicationInbound | Inbound | VirtualNetwork | 80, 5701 | Allow |
| 120 | AllowSSHRDPOutbound | Outbound | * | 22, 3389 | Allow |
| 130 | AllowAzureCloudOutbound | Outbound | * | 443 | Allow |
| 140 | AllowBastionCommunicationOutbound | Outbound | VirtualNetwork | 5701, 8080 | Allow |
| 150 | AllowSessionOutbound | Outbound | * | 80 | Allow |
| 4090 | DenyAllOutbound | Outbound | * | * | Deny |
Prerequisites
- Task 01: Virtual Network completed — Subnets exist
- On-premises management and OOB CIDRs confirmed
- CI/CD runner subnet CIDR confirmed (if applicable)
Values from infrastructure.yml
The script reads these keys from infrastructure.yml (schema 5). Each key, with its type, default and example, is in the variables reference.
azure.tenant_id, azure.location, tags
landing_zone.subscriptions[].name, .id the subscription of each network
landing_zone.networks[].name, .subscription, .resource_group, .location
landing_zone.networks[].subnets[].name, .network_security_group which networks use which group
landing_zone.network_security_groups[].name
landing_zone.network_security_groups[].rules[].name, .priority, .direction, .access, .protocol, .source_address_prefixes,
.source_port_ranges, .destination_address_prefixes, .destination_port_ranges, .description
Single Subscription Model
Landing Zone Placement
| Field | Value | Config Path |
|---|---|---|
| Subscription | Customer subscription | landing_zone.subscriptions[].id |
| Resource Group | rg-azrlmgmt-{env}-{region}-01 | landing_zone.networks[].resource_group |
| Management NSG | nsg-azrl-{env}-{region}-01 | Derived from naming convention |
| Bastion NSG | nsg-bastion-{env}-{region}-01 | Derived from naming convention |
Execution Options
- Azure Portal
- Orchestrated Script
- Azure CLI
Azure Portal
When to use: Learning Azure Local, single deployment, prefer visual interface
Procedure — Management NSG
- Create NSG:
- Search for Network security groups → + Create
| Field | Value | Source |
|-------|-------|--------|
| Name |
nsg-azrl-{env}-{region}-01| Naming convention | | Region | Your region |azure.location| | Resource Group | Management RG |landing_zone.networks[].resource_group|
- Add Inbound Rules — Navigate to Inbound security rules → + Add for each:
Rule 1: Bastion SSH/RDP
| Field | Value |
|---|---|
| Source | IP Addresses |
| Source IP | AzureBastionSubnet CIDR |
| Destination port ranges | 22, 3389 |
| Protocol | Any |
| Action | Allow |
| Priority | 110 |
| Name | Bastion-SSH-RDP-Inbound |
Rule 2: CI/CD Runners SSH
| Field | Value |
|---|---|
| Source | IP Addresses |
| Source IP | CI/CD runner subnet CIDR |
| Destination port ranges | 22 |
| Priority | 120 |
| Name | Azure Local Cloud-Runners-SSH-Inbound |
Rule 3: On-Prem Inbound
| Field | Value |
|---|---|
| Source | IP Addresses |
| Source IPs | On-prem mgmt CIDR, OOB CIDR |
| Destination port ranges | * |
| Priority | 130 |
| Name | Azure-Local-OnPrem-Inbound |
- Associate NSG to Management Subnet:
- NSG → Subnets → + Associate → Select VNet and management subnet
Procedure — Bastion NSG
- Create Bastion NSG:
- Create second NSG named
nsg-bastion-{env}-{region}-01
-
Add Required Bastion Rules: Add all inbound/outbound rules per the Bastion NSG Rules table above. These are Microsoft-required for Bastion to function.
-
Associate NSG to AzureBastionSubnet:
- NSG → Subnets → + Associate → Select AzureBastionSubnet
Validation
- Both NSGs created with correct rules
- Management NSG associated with management subnet
- Bastion NSG associated with AzureBastionSubnet
- Bastion connectivity still works (Task 05)
Links
PowerShell
When to use: Run the task from a management workstation or a pipeline. The script reads
infrastructure.ymland carries no values of its own.
Script
| Script | What it does |
|---|---|
scripts/deploy/02-azure-foundation/phase-04-azure-management-infrastructure/02-manual-deployment/task-06-network-security-groups/powershell/New-LandingZoneNsg.ps1 | Creates the Azure network security groups that landing_zone.network_security_groups of infrastructure.yml lists, with their rules. Supports -WhatIf. |
Requires: PowerShell 7.
Run
./New-LandingZoneNsg.ps1 -InfrastructureFile ./infrastructure.yml -WhatIf
Azure CLI
When to use: Run the task from a host that has the Azure CLI and not the Az PowerShell modules. The script is PowerShell 7 that calls
az; it readsinfrastructure.ymland carries no values of its own.
Script
| Script | What it does |
|---|---|
scripts/deploy/02-azure-foundation/phase-04-azure-management-infrastructure/02-manual-deployment/task-06-network-security-groups/azurecli/New-LandingZoneNsg.azcli.ps1 | Creates the Azure network security groups that landing_zone.network_security_groups of infrastructure.yml lists, with their rules, with the Azure CLI. Supports -WhatIf. |
Requires: PowerShell 7 and the Azure CLI, signed in with az login --tenant <azure.tenant_id>.
Run it after the resource groups exist and before the virtual network script of Task 01, which attaches each group to its subnets. It is additive: it adds the rules a group lacks, reports a rule that differs, and never removes or changes a rule or attaches a group to a subnet. A group that no subnet names is reported as NotUsed and not created.
Run
./New-LandingZoneNsg.azcli.ps1 -InfrastructureFile ./infrastructure.yml -WhatIf
./New-LandingZoneNsg.azcli.ps1 -InfrastructureFile ./infrastructure.yml
Validation
- Management NSG has 3 inbound rules
- Bastion NSG has all required rules (9 rules)
- Both NSGs associated with correct subnets
- Bastion connectivity still works
CAF/WAF Landing Zone Model
In the CAF/WAF model, NSGs follow the same pattern but are deployed in the Connectivity subscription where the VNet resides.
Landing Zone Placement
| Field | Value | Config Path |
|---|---|---|
| Subscription | Connectivity subscription | landing_zone.subscriptions[].id |
| Resource Group | rg-azrlconn-{env}-{region}-01 | landing_zone.networks[].resource_group |
Execution Options
- Azure Portal
- Orchestrated Script
- Azure CLI
Azure Portal
Follow the same procedure as Single Subscription → Azure Portal, targeting the Connectivity subscription.
Validation
- NSGs in Connectivity subscription
- Associated with correct subnets
PowerShell
When to use: Run the task from a management workstation or a pipeline. The script reads
infrastructure.ymland carries no values of its own.
Script
| Script | What it does |
|---|---|
scripts/deploy/02-azure-foundation/phase-04-azure-management-infrastructure/02-manual-deployment/task-06-network-security-groups/powershell/New-LandingZoneNsg.ps1 | Creates the Azure network security groups that landing_zone.network_security_groups of infrastructure.yml lists, with their rules. Supports -WhatIf. |
Requires: PowerShell 7.
Run
./New-LandingZoneNsg.ps1 -InfrastructureFile ./infrastructure.yml -WhatIf
Azure CLI
When to use: Run the task from a host that has the Azure CLI and not the Az PowerShell modules. The script is PowerShell 7 that calls
az; it readsinfrastructure.ymland carries no values of its own.
Script
| Script | What it does |
|---|---|
scripts/deploy/02-azure-foundation/phase-04-azure-management-infrastructure/02-manual-deployment/task-06-network-security-groups/azurecli/New-LandingZoneNsg.azcli.ps1 | Creates the Azure network security groups that landing_zone.network_security_groups of infrastructure.yml lists, with their rules, with the Azure CLI. Supports -WhatIf. |
Requires: PowerShell 7 and the Azure CLI, signed in with az login --tenant <azure.tenant_id>.
Run it after the resource groups exist and before the virtual network script of Task 01, which attaches each group to its subnets. It is additive: it adds the rules a group lacks, reports a rule that differs, and never removes or changes a rule or attaches a group to a subnet. A group that no subnet names is reported as NotUsed and not created.
Run
./New-LandingZoneNsg.azcli.ps1 -InfrastructureFile ./infrastructure.yml -WhatIf
./New-LandingZoneNsg.azcli.ps1 -InfrastructureFile ./infrastructure.yml
Validation
- NSGs deployed in Connectivity subscription
- Subnet associations correct
Troubleshooting
| Issue | Root Cause | Remediation |
|---|---|---|
| Bastion stops working after NSG | Missing required Bastion rules | Add all Microsoft-required Bastion inbound/outbound rules |
| SSH from runner fails | Priority or CIDR mismatch | Verify CI/CD subnet CIDR in rule 120 |
| On-prem cannot reach Azure VMs | NSG blocking on-prem to mgmt | Verify on-prem CIDRs in rule 130 |
| DenyAllOutbound blocks Bastion | Bastion outbound rules missing | Ensure outbound rules 120-150 exist before DenyAll at 4090 |
Alternatives
The procedures in this task use the methods shown in the tabs above. The toolkit is PowerShell 7 only and has no Bash scripts. A few tasks that act on Azure also have an Azure CLI script written in PowerShell. The script status page of the toolkit lists which task has which script.
Navigation
| Previous | Up | Next |
|---|---|---|
| Task 05: Azure Bastion | Manual Deployment Index | Task 07: NAT Gateway |
Version Control
- Created: 2025-09-15 by Hybrid Cloud Solutions
- Last Updated: 2026-03-03 by Hybrid Cloud Solutions
- Version: 4.0.0
- Tags: azure-local, nsg, networking, security
- Keywords: NSG, network security group, firewall rules, subnet security, Bastion NSG
- Author: Hybrid Cloud Solutions
Version Control
| Version | Date | Author | Changes |
|---|---|---|---|
| 1.0.0 | 2025-03-25 | Azure Local Cloud | Initial release |