Overview and What You Will Learn
Azure VPN Gateway connects an on-premises network to Azure through an encrypted IPsec tunnel over the public internet, while ExpressRoute connects through a dedicated private circuit from a connectivity provider. In this lab, you will build a working VPN connection, using a second VNet to stand in for your on-premises network so the tunnel really comes up, and then walk through how ExpressRoute differs and why production networks often run both.
Why This Matters in Production
The wrong hybrid link shows up as latency problems or an outage you cannot easily fix. An enterprise bank migrating a core banking workload to Azure needs predictable latency between its datacenter and Azure. A VPN over the shared internet cannot promise that, while a dedicated circuit can, at a higher cost and a much longer lead time. Choosing late means missing the migration deadline, because circuits take weeks to provision.
Core Principles
Both services land on a gateway inside a dedicated GatewaySubnet in your VNet, but they travel different paths. A common resilient pattern uses ExpressRoute as the primary path and a VPN as automatic backup.
192.168.0.0/16] -->|ExpressRoute primary| G O -->|IPsec VPN backup| G subgraph AZ[Azure vnet-azure 10.0.0.0/16] G[GatewaySubnet
ER and VPN gateways] --> V[subnet-vms] end classDef onprem fill:#475569,color:#FFFFFF classDef gw fill:#326CE5,color:#FFFFFF classDef vm fill:#10B981,color:#FFFFFF class O onprem class G gw class V vm
| Aspect | VPN Gateway | ExpressRoute |
|---|---|---|
| Path | Encrypted tunnel over the internet | Dedicated private circuit |
| Bandwidth | Hundreds of Mbps to a few Gbps by SKU | 50 Mbps to 10 Gbps per circuit |
| Setup time | Under an hour | Weeks, via a provider |
RememberExpressRoute is private, not automatically encrypted. If you need encryption on the circuit, add MACsec on ExpressRoute Direct or run an IPsec VPN over it.
Detailed Step-by-Step Practical Lab
This lab creates two VPN gateways, which bill by the hour and take 30 to 45 minutes to deploy. Delete everything when you finish.
How to Create the Azure VNet and a Simulated On-Premises VNet
The second VNet uses 192.168.0.0/16 to play the role of your office network. Each VNet needs a subnet named exactly GatewaySubnet, and a /27 is the minimum recommended size.
export RG="rg-vpn-lab-mumbai"export PSK=$(openssl rand -hex 24) az group create --name $RG --location centralindia az network vnet create \ --resource-group $RG \ --name vnet-azure \ --address-prefixes 10.0.0.0/16 \ --subnet-name subnet-vms \ --subnet-prefixes 10.0.1.0/24 az network vnet subnet create \ --resource-group $RG \ --vnet-name vnet-azure \ --name GatewaySubnet \ --address-prefixes 10.0.255.0/27 az network vnet create \ --resource-group $RG \ --name vnet-onprem-sim \ --address-prefixes 192.168.0.0/16 \ --subnet-name subnet-office \ --subnet-prefixes 192.168.1.0/24 az network vnet subnet create \ --resource-group $RG \ --vnet-name vnet-onprem-sim \ --name GatewaySubnet \ --address-prefixes 192.168.255.0/27RememberThe two networks must not overlap. A VPN cannot route between identical ranges, which is why on-premises planning matters before you build anything.
How to Create Public IPs and VPN Gateways
Use a zone-redundant AZ gateway SKU, because non-zone SKUs can no longer be created for new gateways. Start both gateways with --no-wait so they deploy in parallel, then wait for them.
for n in azure onprem; do az network public-ip create \ --resource-group $RG \ --name pip-gw-$n \ --sku Standard \ --allocation-method Static \ --zone 1 2 3done az network vnet-gateway create \ --resource-group $RG \ --name gw-azure \ --public-ip-addresses pip-gw-azure \ --vnet vnet-azure \ --gateway-type Vpn \ --vpn-type RouteBased \ --sku VpnGw1AZ \ --no-wait az network vnet-gateway create \ --resource-group $RG \ --name gw-onprem-sim \ --public-ip-addresses pip-gw-onprem \ --vnet vnet-onprem-sim \ --gateway-type Vpn \ --vpn-type RouteBased \ --sku VpnGw1AZ \ --no-wait az network vnet-gateway wait --resource-group $RG --name gw-azure --createdaz network vnet-gateway wait --resource-group $RG --name gw-onprem-sim --createdHow to Connect the Two Gateways With a Shared Key
A tunnel needs a connection on each side, and both must use the identical pre-shared key.
az network vpn-connection create \ --resource-group $RG \ --name conn-azure-to-onprem \ --vnet-gateway1 gw-azure \ --vnet-gateway2 gw-onprem-sim \ --shared-key "$PSK" az network vpn-connection create \ --resource-group $RG \ --name conn-onprem-to-azure \ --vnet-gateway1 gw-onprem-sim \ --vnet-gateway2 gw-azure \ --shared-key "$PSK"How to Check VPN Connection Status
Wait a few minutes for the IPsec negotiation, then check both sides.
for c in conn-azure-to-onprem conn-onprem-to-azure; do az network vpn-connection show \ --resource-group $RG \ --name $c \ --query "connectionStatus" --output tsvdoneHow to Connect a Real On-Premises Device
With a real firewall or router, the Azure side uses a Local Network Gateway instead of a second Azure gateway. Do not run these in the lab, because 203.0.113.50 is a documentation address that will never connect.
# Give this public IP to your on-premises network teamaz network public-ip show \ --resource-group $RG --name pip-gw-azure \ --query ipAddress --output tsv az network local-gateway create \ --resource-group $RG \ --name onprem-gateway \ --gateway-ip-address 203.0.113.50 \ --local-address-prefixes 192.168.0.0/16 az network vpn-connection create \ --resource-group $RG \ --name conn-real-site \ --vnet-gateway1 gw-azure \ --local-gateway2 onprem-gateway \ --shared-key "$PSK"A mismatched key, or mismatched IPsec settings on the device, leaves the status at NotConnected or Connecting.
How ExpressRoute Is Provisioned
ExpressRoute cannot be built in a lab, because it needs a physical circuit from a connectivity provider. The workflow is:
- Choose a provider and a peering location near your datacenter.
- Create the circuit resource in Azure and give the provider its service key.
- Wait for the provider to provision the circuit, which usually takes weeks.
- Configure private peering, with BGP, VLAN, and address ranges agreed with the provider.
- Create an ExpressRoute gateway in the
GatewaySubnetand link the circuit to it.
# Illustrative only: billing starts when the circuit is created,# even before the provider has provisioned itaz network express-route create \ --resource-group $RG \ --name er-circuit-lab \ --location centralindia \ --bandwidth 200 \ --provider "Example Provider" \ --peering-location "Chennai" \ --sku-tier Standard \ --sku-family MeteredDataHow to Clean Up
Deleting the group removes the gateways, which can take a while.
az group delete --name $RG --yes --no-waitProduction Best Practices & Common Pitfalls
Common MistakeUsing VPN Gateway as the only link for a workload that needs low, steady latency, such as a real-time trading system, then finding that internet-path variation causes problems under load.
TipBoth services can use BGP to exchange routes dynamically, so your network and Azure learn each other's changes without anyone editing static routes. When both paths advertise the same prefix, Azure prefers ExpressRoute and falls back to the VPN if the circuit fails.
SecurityTreat the pre-shared key like a password. Generate it randomly instead of typing a guessable phrase, store it in Azure Key Vault, and never commit it to a repository or paste it into tickets.
- Plan for lead times. A VPN Gateway deploys in under an hour, but ExpressRoute takes weeks, so put the circuit order on the migration timeline early.
- Make the pre-shared key identical on both sides. A mismatch is one of the most common reasons a new connection shows as not connected even though every other setting looks right.
- Use zone-redundant gateway SKUs. Pick an
AZSKU such asVpnGw1AZ, because the older non-zone SKUs are retired for new deployments. - Keep the GatewaySubnet clean. Use a /27 or larger, put nothing else in it, and do not attach an NSG to it.
- Design ExpressRoute for redundancy. A circuit has two connections, and critical workloads should use circuits in two peering locations.
- Do not overlap address ranges. The on-premises and Azure ranges must be unique, or no route between them can exist.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az network vnet-gateway create --sku VpnGw1AZ |
Create a zone-redundant VPN Gateway |
az network local-gateway create |
Define the on-premises side of a VPN |
az network vpn-connection create |
Create the connection between two gateways |
az network vpn-connection show --query connectionStatus |
Check whether a tunnel is up |
az network vnet-gateway wait --created |
Wait for a gateway deployment to finish |
az network express-route create |
Create an ExpressRoute circuit (billing starts) |