Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 57 ยท Real-World Projects

Project: Secure Three-Tier Network Architecture

Design a secure three-tier network on Azure: hub and spoke VNets, NSGs, private endpoints, Application Gateway with WAF, Bastion and a Bicep layout, plus a checker.

A network is the part of a cloud design that is easiest to get wrong and hardest to see. A single permissive rule or one forgotten public address can expose a database to the whole internet. In this project you design a classic three-tier architecture (web, application, data) and protect it in layers, so that no single mistake is enough to cause a breach.

The Azure resources need a subscription, so the commands, Bicep and configuration are read-only. What you can run is a reachability checker: a JavaScript model of the design that evaluates Network Security Group rules the way Azure does (priority order, default rules, service tags) and tests a list of "this must work" and "this must be blocked" connections. Writing the checker first, as a set of tests for your network, is a habit worth keeping. The lesson builds on Virtual Networks and Subnets, Network Security Groups and Firewalls, Load Balancer, Application Gateway and Front Door and Private Endpoints, VPN and ExpressRoute.

The design

flowchart TB
    NET["Internet"] -->|"443"| AGW
    ADM["Administrator"] -->|"HTTPS to the portal"| BAS
    subgraph HUB["Hub VNet 10.0.0.0/16"]
        BAS["AzureBastionSubnet<br/>10.0.0.0/26<br/>Bastion"]
    end
    subgraph SPOKE["Spoke VNet 10.1.0.0/16"]
        AGW["snet-agw 10.1.0.0/24<br/>App Gateway + WAF"]
        WEB["snet-web 10.1.1.0/24<br/>web tier (8080)"]
        APP["snet-app 10.1.2.0/24<br/>app tier (8080)"]
        DAT["snet-data 10.1.3.0/24<br/>private endpoint to SQL (1433)"]
        AGW --> WEB --> APP --> DAT
    end
    BAS -.->|"peering, SSH 22"| WEB
    BAS -.-> APP

Traffic flows in one direction through the tiers. Administration comes from the hub through Bastion, never from the internet.

The layers of protection, from the outside in:

  1. Application Gateway with WAF. The only public entry point. It terminates TLS and inspects HTTP traffic for common attacks (the Web Application Firewall).
  2. Subnets per tier plus NSGs. Each tier accepts traffic only from the tier in front of it, on one port.
  3. Private endpoints. The database has no public address; it is reached through a private IP inside the data subnet.
  4. Bastion. Administrators connect through the portal to a private VM, so no machine needs a public SSH or RDP port.
  5. Hub and spoke. Shared services (Bastion today; a firewall or VPN gateway later) live in a hub VNet, and workloads live in spokes connected by peering. A new application gets a new spoke without touching the others.

The ranges must not overlap, because peered networks must have distinct address spaces. Plan them with a table before you create anything (the stand-in in Virtual Networks and Subnets can check for overlaps).

Subnets and special rules

Some Azure services need a subnet of their own, and some demand exact names:

  • Azure Bastion requires a subnet named exactly AzureBastionSubnet, at least /26, and a Standard SKU static public IP.
  • Application Gateway needs a dedicated subnet with no other resource types in it (a /24 is common to leave room to scale). Its NSG must allow the management traffic Azure requires for the v2 SKU (the GatewayManager service tag on ports 65200-65535) and the Azure load balancer probes, so build it from the current documentation and do not block its outbound traffic broadly.
  • Private endpoints take one IP address from the subnet they are placed in. Many teams use a dedicated snet-pe subnet; this project puts the SQL endpoint in snet-data to keep the diagram to three tiers.

NSG rules: the part people get wrong

An NSG holds rules evaluated in priority order, lowest number first, and the first match wins. Azure adds default rules at priorities 65000 and above that you cannot delete:

Direction Priority Rule Effect
Inbound 65000 AllowVnetInBound allows all traffic from the virtual network (including peered networks)
Inbound 65001 AllowAzureLoadBalancerInBound allows load balancer probes
Inbound 65500 DenyAllInBound denies everything else
Outbound 65000 AllowVnetOutBound allows traffic to the virtual network
Outbound 65001 AllowInternetOutBound allows traffic to the internet
Outbound 65500 DenyAllOutBound denies everything else

Read the first row again. Inside a virtual network, everything is allowed by default. If you only write "allow web to app on 8080", the web tier can still reach the data subnet on any port, because AllowVnetInBound matches it. A secure design therefore adds an explicit deny for VirtualNetwork at a priority below your allow rules (for example 4000) in every tier. NSGs are stateful: replies to an allowed connection are allowed automatically, so you write rules only for the direction in which connections start.

The Bicep layout

Infrastructure as code keeps the network reviewable and repeatable (see Bicep and ARM Templates). A common layout:

TEXT
infra/
  main.bicep               (parameters and module calls)
  modules/
    hub.bicep              (hub VNet, AzureBastionSubnet, Bastion, public IP)
    spoke.bicep            (spoke VNet, subnets, NSGs)
    peering.bicep          (both directions of the peering)
    appgw.bicep            (Application Gateway, WAF policy, public IP)
    data.bicep             (SQL server, private endpoint, private DNS zone)
  parameters/
    dev.bicepparam
    prod.bicepparam

The top file only wires modules together (read-only):

BICEP
targetScope = 'resourceGroup'

param location string = resourceGroup().location
param prefix string = 'shop'

module hub 'modules/hub.bicep' = {
  name: 'hub'
  params: {
    location: location
    prefix: prefix
    addressSpace: '10.0.0.0/16'
    bastionPrefix: '10.0.0.0/26'
  }
}

module spoke 'modules/spoke.bicep' = {
  name: 'spoke'
  params: {
    location: location
    prefix: prefix
    addressSpace: '10.1.0.0/16'
    bastionSource: '10.0.0.0/26'
  }
}

module peering 'modules/peering.bicep' = {
  name: 'peering'
  params: {
    hubVnetName: hub.outputs.vnetName
    spokeVnetName: spoke.outputs.vnetName
  }
}

Inside spoke.bicep, the NSG rules are data, and a loop turns them into resources. This is the web tier (read-only):

BICEP
param location string
param bastionSource string

var webRules = [
  { name: 'allow-agw-8080', priority: 100, access: 'Allow', port: '8080', source: '10.1.0.0/24' }
  { name: 'allow-bastion-ssh', priority: 110, access: 'Allow', port: '22', source: bastionSource }
  { name: 'deny-vnet-inbound', priority: 4000, access: 'Deny', port: '*', source: 'VirtualNetwork' }
]

resource nsgWeb 'Microsoft.Network/networkSecurityGroups@2023-11-01' = {
  name: 'nsg-web'
  location: location
  properties: {
    securityRules: [for r in webRules: {
      name: r.name
      properties: {
        priority: r.priority
        direction: 'Inbound'
        access: r.access
        protocol: 'Tcp'
        sourceAddressPrefix: r.source
        sourcePortRange: '*'
        destinationAddressPrefix: '*'
        destinationPortRange: r.port
      }
    }]
  }
}

resource spoke 'Microsoft.Network/virtualNetworks@2023-11-01' = {
  name: 'vnet-spoke'
  location: location
  properties: {
    addressSpace: { addressPrefixes: [ '10.1.0.0/16' ] }
    subnets: [
      { name: 'snet-agw', properties: { addressPrefix: '10.1.0.0/24' } }
      { name: 'snet-web', properties: { addressPrefix: '10.1.1.0/24', networkSecurityGroup: { id: nsgWeb.id } } }
      // snet-app and snet-data follow the same pattern with their own NSGs
    ]
  }
}

The WAF is its own resource, referenced by the gateway. Start in Detection mode to see what it would block, tune out false positives, then switch to Prevention:

BICEP
resource wafPolicy 'Microsoft.Network/ApplicationGatewayWebApplicationFirewallPolicies@2023-11-01' = {
  name: 'waf-shop'
  location: location
  properties: {
    policySettings: { state: 'Enabled', mode: 'Prevention' }
    managedRules: {
      managedRuleSets: [
        { ruleSetType: 'OWASP', ruleSetVersion: '3.2' }
      ]
    }
  }
}

Newer managed rule set versions exist, so choose the version Microsoft currently recommends when you build this for real.

The commands for Bastion, the private endpoint and public access look like this (read-only):

Terminal
az network bastion create --name bas-hub --resource-group rg-net \
  --vnet-name vnet-hub --public-ip-address pip-bastion --sku Standard

az network private-endpoint create --name pe-sql --resource-group rg-net \
  --vnet-name vnet-spoke --subnet snet-data \
  --private-connection-resource-id $SQL_SERVER_ID --group-id sqlServer \
  --connection-name pe-sql-conn

az sql server update --name sql-shop-001 --resource-group rg-net --enable-public-network false

A private endpoint needs private DNS too. The server name sql-shop-001.database.windows.net must resolve to the private IP from inside your networks, which is done with a private DNS zone named privatelink.database.windows.net, linked to both VNets. Without it, clients keep resolving the public address and fail once public access is off (see DNS and Traffic Manager).

A reachability checker: a stand-in

Now test the design. The model below contains the two VNets, the five subnets, the hosts and their NSG rules, and it evaluates a packet as Azure would: outbound rules at the source subnet, then inbound rules at the destination subnet, each in priority order with the default rules appended. It knows three tags (Internet, VirtualNetwork, AzureLoadBalancer) and treats peered networks as one VirtualNetwork. It is a simplified model: it ignores routing tables, firewalls, DNS, the Application Gateway management rules and application-level behavior, so a pass here is necessary but not sufficient.

JS Test the three-tier network design

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first run is the instructive one. With allow rules only, four policy lines ("gateway cannot skip the web tier", "web tier cannot touch the database", "only the SQL port is open" and "admin cannot reach the data tier") fail, because the default AllowVnetInBound rule lets any host in the VNet (and in the peered hub) connect to any other. The hardened run adds one deny rule per tier and every line passes. Notice also that the internet-to-web check is stopped by the missing public IP, not by an NSG: a second layer.

What if someone adds a public IP?

Mistakes happen: a developer attaches a public IP to a VM "just for a minute". Layered defenses exist so that this is not fatal. The next stand-in repeats the check with a public IP on the web VM, in the naive and hardened designs, and then shows an NSG that allows the whole internet by accident.

JS Layers: mistakes versus defense in depth

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice three things. A subnet or network interface with no NSG at all filters nothing, so a public IP there is fully exposed. Once an NSG exists, the default DenyAllInBound blocks the internet, but only the explicit deny stops the data subnet. And an allow of port 8080 from * opens the door again. The real protection is a combination: no public IP unless needed, NSGs that name sources precisely, Azure Policy that denies public IPs on tier subnets (see Azure Policy, Locks and Tags), and tests that fail the pipeline when the design drifts.

Deploying and operating

  • Deploy in order: hub, spoke, peering, data, then the gateway, using az deployment group what-if before every change (see Bicep and ARM Templates).
  • Send NSG flow logs and gateway/WAF logs to Log Analytics, and alert on WAF blocks, spikes in denied traffic and any change to NSG rules (see Azure Monitor, Log Analytics and Application Insights).
  • Defender for Cloud highlights exposed management ports and open storage (see Security Best Practices and Defender for Cloud).
  • Egress control: to inspect or restrict outbound traffic, add an Azure Firewall in the hub and route spoke subnets through it with user-defined routes. It is the natural next step and a significant cost.
  • Cost: Application Gateway WAF v2, Bastion and Azure Firewall bill by the hour even when idle. Build the lab, test it, and delete the resource group (see Pricing, Cost Management and Budgets).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A spoke VNet NSG has only 'allow web to app on 8080'. Can the web tier connect to the data subnet on port 1433?

    The default inbound rule at priority 65000 allows VirtualNetwork to VirtualNetwork. To isolate tiers, add a higher-priority (lower number) deny rule for VirtualNetwork.
  2. 2. Which statement about NSG rule evaluation is correct?

    Priority numbers run from 100 to 4096 for your rules; the default rules sit at 65000 and above, so they are last.
  3. 3. Why is a private endpoint combined with a private DNS zone?

    Clients use the usual server name. Without the privatelink zone, the name still resolves to the public address, which fails once public access is disabled.
  4. 4. What is the main reason to use Azure Bastion?

    Bastion lives in its own subnet and proxies SSH/RDP over TLS, so the VMs keep only private addresses.
  5. 5. Where would you start the WAF before blocking traffic in production?

    Detection mode logs matches without blocking. After tuning, switch to Prevention so attacks are rejected.

Summary

  • The design is layered: WAF at the only public entry, one subnet per tier, tight NSGs, a private endpoint for the database, Bastion for administration and a hub for shared services.
  • NSG rules run in priority order with default rules at 65000 and above; the default AllowVnetInBound means tiers need an explicit deny to be isolated.
  • Bastion, Application Gateway and private endpoints have subnet requirements; plan non-overlapping address spaces for peered VNets.
  • Private endpoints need private DNS zones linked to the networks that use them.
  • Keep the network as Bicep modules, with the NSG rules as data, and test reachability with a policy list (the stand-in here, or real tests after deployment).
  • Defense in depth means one mistake, such as an accidental public IP, is not enough to cause a breach; delete expensive lab resources when you finish.

Next: Practice: Azure Exercises with Solutions.