VNet peering for connectivity in Azure - Commands & Configuration
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vnet1Name": {
"type": "string",
"defaultValue": "vnet-eastus"
},
"vnet2Name": {
"type": "string",
"defaultValue": "vnet-westus"
},
"resourceGroup": {
"type": "string",
"defaultValue": "example-rg"
}
},
"resources": [
{
"type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
"apiVersion": "2021-05-01",
"name": "[concat(parameters('vnet1Name'), '/peer-to-vnet2')]",
"properties": {
"remoteVirtualNetwork": {
"id": "[resourceId(parameters('resourceGroup'), 'Microsoft.Network/virtualNetworks', parameters('vnet2Name'))]"
},
"allowVirtualNetworkAccess": true,
"allowForwardedTraffic": false,
"allowGatewayTransit": false,
"useRemoteGateways": false
}
},
{
"type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
"apiVersion": "2021-05-01",
"name": "[concat(parameters('vnet2Name'), '/peer-to-vnet1')]",
"properties": {
"remoteVirtualNetwork": {
"id": "[resourceId(parameters('resourceGroup'), 'Microsoft.Network/virtualNetworks', parameters('vnet1Name'))]"
},
"allowVirtualNetworkAccess": true,
"allowForwardedTraffic": false,
"allowGatewayTransit": false,
"useRemoteGateways": false
}
}
]
}This JSON template creates a peering connection between two Azure virtual networks named vnet-eastus and vnet-westus in the resource group example-rg.
Each virtualNetworkPeerings resource defines one side of the peering, allowing both VNets to communicate.
The allowVirtualNetworkAccess set to true enables traffic between the VNets.
Other flags like allowForwardedTraffic, allowGatewayTransit, and useRemoteGateways are set to false for simple peering without gateway sharing or forwarded traffic.
az network vnet peering create --name peer-to-vnet2 --resource-group example-rg --vnet-name vnet-eastus --remote-vnet vnet-westus --allow-vnet-access
--allow-vnet-access - Allows traffic between the two virtual networks.az network vnet peering create --name peer-to-vnet1 --resource-group example-rg --vnet-name vnet-westus --remote-vnet vnet-eastus --allow-vnet-access
--allow-vnet-access - Allows traffic between the two virtual networks.az network vnet peering list --resource-group example-rg --vnet-name vnet-eastus
az network vnet peering list --resource-group example-rg --vnet-name vnet-westus
If you remember nothing else from this pattern, remember: VNet peering connects two virtual networks so they can communicate privately and directly without using the internet.
Practice
Solution
Step 1: Understand VNet peering concept
VNet peering allows two virtual networks to communicate directly without going through the internet.Step 2: Identify the main use case
This direct communication is the primary purpose, enabling resource sharing securely within Azure.Final Answer:
To connect two virtual networks for direct communication -> Option AQuick Check:
VNet peering = direct network connection [OK]
- Confusing peering with VM backup
- Thinking peering speeds up app deployment
- Assuming peering is for traffic monitoring
Solution
Step 1: Review peering setup requirements
VNet peering requires configuration from both virtual networks to allow full communication.Step 2: Identify correct procedure
Creating peering from both VNets ensures bidirectional connectivity and proper routing.Final Answer:
Create peering from both VNets involved -> Option CQuick Check:
Peering setup = both sides configured [OK]
- Configuring peering only on one VNet
- Assuming peering is automatic on new VNets
- Looking for peering in subscription settings
Solution
Step 1: Understand VNet peering behavior
Peering connects VNets so resources can communicate directly, including ping (ICMP) traffic if allowed by NSGs.Step 2: Analyze the ping scenario
If no network security group blocks ICMP, ping from VM in VNetA to VM in VNetB will succeed due to peering.Final Answer:
The ping succeeds because peering allows direct communication -> Option DQuick Check:
Peering enables direct VM communication [OK]
- Assuming peering blocks ping by default
- Thinking VMs must be in same subnet to communicate
- Believing peering does not enable VNet communication
Solution
Step 1: Understand peering directionality
Peering must be created on both VNets for full bidirectional communication.Step 2: Analyze partial peering effect
With peering only from VNetA to VNetB, traffic flows from A to B but not from B to A.Final Answer:
Unidirectional communication from VNetA to VNetB only -> Option BQuick Check:
One-sided peering = one-way traffic [OK]
- Assuming peering is automatic both ways
- Expecting no communication at all
- Thinking peering creates full connection from one side
Solution
Step 1: Identify peering types and their use cases
Local VNet peering works within the same region; global VNet peering connects VNets across regions.Step 2: Choose peering for cross-region communication
Global VNet peering enables secure, low-latency communication between VNets in different Azure regions.Final Answer:
Global VNet peering -> Option AQuick Check:
Cross-region VNet peering = global peering [OK]
- Choosing local peering for different regions
- Confusing VPN Gateway with peering
- Selecting ExpressRoute which is for on-premises
Ready to prove it?
Pass the test and this pattern moves unknown
