Three subnets, web, app and data, and a rule that sounds obvious: the web tier may talk to the app tier, the app tier may talk to the database, and nothing skips a tier. Azure does not do that for you. Out of the box, every subnet in a virtual network can reach every other one. This recipe builds the network in Bicep, adds one network security group (NSG) per subnet, and puts a small Linux VM in each subnet so that five connection tests prove the rules do what they say.

The five-minute video walks through the same steps: the Bicep file, the deployment, the NSG rules in the portal and the five tests.

Why

A VNet is a private network with a private address range. Subnets split it into parts, and an NSG is a list of allow and deny rules that you attach to a subnet or a network card. Each rule has a priority: Azure checks them from the lowest number up and stops at the first match.

Every NSG also comes with default rules that you cannot delete. One of them, AllowVnetInBound at priority 65000, allows everything from anywhere inside the VNet. So an NSG that only says “allow 8080 from web” still lets the web tier reach SSH on the app servers, and lets it reach the database directly. The one rule that turns subnets into real tiers is a deny for the VNet, placed above the default.

Before you start

  • A subscription where you are Owner or Contributor, and PowerShell 7 with the Az module and Bicep, or the Azure CLI. GOV-02 installs them.
  • 6 vCPUs of quota in the VM family you use. The default size is Standard_D2als_v6, and new subscriptions usually get 10 vCPUs of that family. See Troubleshooting if the deployment stops with QuotaExceeded.
  • The code is in cloudtales-gr/azure-recipes: main.bicep, deploy, test and cleanup, each as .ps1 and .sh.

What the recipe builds, and which connections the tests expect to pass:

NET-01 architecture: vnet-net01 with snet-web, snet-app and snet-data, one NSG each; web to app on 8080 and app to data on 5432 allowed; web to app on 22 and web to data blocked by DenyVnetInbound

Steps

Plan the address space

One VNet, 10.10.0.0/16, and one /24 per tier. Each VM gets a fixed address and runs one small TCP listener on its tier’s port, so that a blocked test always means the NSG and never a missing service.

SubnetRangeTest VMListens onWho may connect
snet-web10.10.1.0/2410.10.1.480the internet, on 80 and 443
snet-app10.10.2.0/2410.10.2.48080snet-web only
snet-data10.10.3.0/2410.10.3.45432snet-app only

A /24 gives 251 usable addresses, not 256. Azure keeps five in every subnet: the network address, the broadcast address and three of its own. The portal shows 250 available in each subnet here, because the test VM uses one.

The subnets are private: defaultOutboundAccess: false. VMs in them get no implicit internet access, which is where Azure is heading for new networks anyway. When a tier needs to reach the internet, give it a NAT gateway on purpose; that is NET-05.

Write the NSG rules

Each tier gets its own allow rules, and every NSG ends with the same deny:

{
  // The default rule AllowVnetInBound (65000) lets every subnet reach every other one. This one wins first.
  name: 'DenyVnetInbound'
  properties: {
    priority: 4000
    direction: 'Inbound'
    access: 'Deny'
    protocol: '*'
    sourceAddressPrefix: 'VirtualNetwork'
    sourcePortRange: '*'
    destinationAddressPrefix: '*'
    destinationPortRange: '*'
  }
}

Priority 4000 leaves plenty of room above it, 100 to 3999, for the allow rules you will add later. The allow rule for the app tier names the web subnet as its only source:

{ name: 'AllowAppFromWeb', priority: 100, source: '10.10.1.0/24', port: '8080' }

The NSG is attached to the subnet, not to each VM’s network card. One NSG then covers every VM you ever put in that subnet, and nobody can forget it on the next one.

Deploy

PowerShell 7 with the Az module:

./deploy.ps1

Bash with the Azure CLI:

./deploy.sh

Both create rg-recipe-net-01 in Sweden Central, tagged recipe=NET-01 and expires=<tomorrow>. They register the Microsoft.Network and Microsoft.Compute resource providers if the subscription does not have them yet, create a throwaway SSH key that nobody ever uses, and deploy main.bicep. It takes about five minutes.

Subscription: Cloudtales
vnet   vnet-net01
web    10.10.1.4:80
app    10.10.2.4:8080
data   10.10.3.4:5432

Thirteen resources: the VNet, three NSGs, three network cards, three VMs and their three disks. None of them has a public IP address.

rg-recipe-net-01: three NICs, three NSGs, three VMs with their OS disks, and vnet-net01

Look at it in the portal

vnet-net01 > Subnets shows the three ranges, each with its own NSG:

Subnets: snet-web, snet-app and snet-data, 250 available IPs each, with nsg-web, nsg-app and nsg-data

nsg-app shows the two rules from the Bicep file above the three defaults that every NSG has:

nsg-app inbound rules: AllowAppFromWeb 100, DenyVnetInbound 4000, then the defaults AllowVnetInBound 65000, AllowAzureLoadBalancerInBound 65001 and DenyAllInBound 65500

Read the inbound rules from the top. Port 8080 from 10.10.1.0/24 matches rule 100 and is allowed. Anything else from inside the VNet matches rule 4000 and is denied, so the default at 65000 is never reached. Traffic from the internet matches neither and falls through to DenyAllInBound.

The outbound defaults include AllowInternetOutBound. That rule only permits traffic; it does not create a route. In a private subnet these VMs still have no way out.

On vm-app > Networking > Network settings, the network card itself has no NSG (“Network security group: -”). The rules come from the subnet:

vm-app network settings: nic-app with private IP 10.10.2.4 and no NSG of its own; nsg-app attached to snet-app applies

Verify

The VMs have no public IPs and no open SSH, so the test does not sign in to them. It uses Run Command, which sends a script through the Azure VM agent and returns the output. On vm-web and vm-app it opens TCP connections with a three-second timeout:

probe() {  # from to ip port expected
  if timeout 3 bash -c "</dev/tcp/$3/$4" 2>/dev/null; then got=allowed; else got=blocked; fi
  if [ "$got" = "$5" ]; then ok=OK; else ok=NO; fi
  printf '%-3s %-4s -> %-4s %-15s expected %-7s got %s\n' "$ok" "$1" "$2" "$3:$4" "$5" "$got"
}
./test.ps1      # or ./test.sh
OK  web  -> app  10.10.2.4:8080  expected allowed got allowed
OK  web  -> app  10.10.2.4:22    expected blocked got blocked
OK  web  -> data 10.10.3.4:5432  expected blocked got blocked
OK  app  -> data 10.10.3.4:5432  expected allowed got allowed
OK  app  -> web  10.10.1.4:22    expected blocked got blocked

Every VM runs SSH on port 22, so the two blocked SSH tests are refused by the NSG, not by a closed port. Without DenyVnetInbound, lines two, three and five would say allowed: the web tier could open a connection straight to the database.

Clean up

./cleanup.ps1      # or ./cleanup.sh

It deletes the resource group, which takes everything with it, and the throwaway SSH key. If you forget, tools/Remove-ExpiredLabs.ps1 in the repository deletes every recipe resource group whose expires tag has passed.

Troubleshooting

  • InvalidTemplateDeployment … Microsoft.Compute/virtualMachines reported preflight validation errors. The message hides the real reason. To read it, run the same deployment as a test: Test-AzResourceGroupDeployment -ResourceGroupName rg-recipe-net-01 -TemplateFile ./main.bicep -sshPublicKey (Get-Content <key>.pub -Raw) and look at .Details.
  • QuotaExceeded … standardBasv2Family … Current Limit: 0. This is what I hit first with the cheaper Standard_B2ats_v2 size. In a new subscription, Microsoft.Compute may not even be registered, and until it is, every VM family shows a quota of 0; the deploy scripts now register it. After registration, most families showed 10 vCPUs, but B-series v2 stayed at 0, and the portal’s automatic increase was refused. Use a family that has quota (./deploy.ps1 -VmSize Standard_D2als_v6 is the default), or open a free quota support request. Get-AzVMUsage -Location swedencentral | Where-Object Limit -gt 0 lists what you have.
  • NotAvailableForSubscription for a size, for example the older Standard_B1s: that size is closed to your subscription in that region. Pick another.
  • A test says expected allowed got blocked. The listener on the target VM starts from cloud-init a minute or two after the deployment finishes. Wait two minutes and run the test again.
  • ssh-keygen is not recognised. On Windows it comes with the OpenSSH client: Settings > System > Optional features > OpenSSH Client.

Going further

NET-02 connects networks like this one in a hub and spoke with VNet peering. NET-04 adds Azure Bastion, so that you can reach these VMs without public IPs; Bastion needs one more allow rule, from AzureBastionSubnet, above DenyVnetInbound. NET-05 gives a private subnet a single, predictable way out with a NAT gateway, and NET-14 turns on flow logs, so that you can see which rule allowed or denied each connection.