Create a virtual network peering - different deployment models and subscriptions
In this tutorial, you learn to create a virtual network peering between virtual networks created through different deployment models. The virtual networks exist in different subscriptions. Peering two virtual networks enables resources in different virtual networks to communicate with each other with the same bandwidth and latency as though the resources were in the same virtual network. Learn more about Virtual network peering.
The steps to create a virtual network peering are different, depending on whether the virtual networks are in the same, or different, subscriptions, and which Azure deployment model the virtual networks are created through. Learn how to create a virtual network peering in other scenarios by clicking the scenario from the following table:
Azure deployment model | Azure subscription |
---|---|
Both Resource Manager | Same |
Both Resource Manager | Different |
One Resource Manager, one classic | Same |
A virtual network peering cannot be created between two virtual networks deployed through the classic deployment model. This tutorial peers virtual networks in the same region. You can also peer virtual networks in different supported regions. It's recommended that you familiarize yourself with the peering requirements and constraints before peering virtual networks.
When creating a virtual network peering between virtual networks that exist in different subscriptions, the subscriptions can be associated to the same Microsoft Entra tenant. If you don't already have a Microsoft Entra tenant, you can quickly create one.
You can use the Azure portal, the Azure CLI, or Azure PowerShell to create a virtual network peering. Click any of the previous tool links to go directly to the steps for creating a virtual network peering using your tool of choice.
Create peering - Azure portal
This tutorial uses different accounts for each subscription. If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of the portal, and skip the steps for assigning another user permissions to the virtual networks.
Log in to the Azure portal as UserA. The account you log in with must have the necessary permissions to create a virtual network peering. For a list of permissions, see Virtual network peering permissions.
Click + New, click Networking, then click Virtual network.
In the Create virtual network blade, enter, or select values for the following settings, then click Create:
- Name: myVnetA
- Address space: 10.0.0.0/16
- Subnet name: default
- Subnet address range: 10.0.0.0/24
- Subscription: Select subscription A.
- Resource group: Select Create new and enter myResourceGroupA
- Location: East US
In the Search resources box at the top of the portal, type myVnetA. Click myVnetA when it appears in the search results. A blade appears for the myVnetA virtual network.
In the myVnetA blade that appears, click Access control (IAM) from the vertical list of options on the left side of the blade.
In the myVnetA - Access control (IAM) blade that appears, click + Add role assignment.
In the Add role assignment blade that appears, select Network contributor in the Role box.
In the Select box, select UserB, or type UserB's email address to search for it. The list of users shown is from the same Microsoft Entra tenant as the virtual network you're setting up the peering for. Click UserB when it appears in the list.
Click Save.
Log out of the portal as UserA, then log in as UserB.
Click + New, type Virtual network in the Search the Marketplace box, then click Virtual network in the search results.
In the Virtual Network blade that appears, select Classic in the Select a deployment model box, then click Create.
In the Create virtual network (classic) box that appears, enter the following values:
- Name: myVnetB
- Address space: 10.1.0.0/16
- Subnet name: default
- Subnet address range: 10.1.0.0/24
- Subscription: Select subscription B.
- Resource group: Select Create new and enter myResourceGroupB
- Location: East US
In the Search resources box at the top of the portal, type myVnetB. Click myVnetB when it appears in the search results. A blade appears for the myVnetB virtual network.
In the myVnetB blade that appears, click Properties from the vertical list of options on the left side of the blade. Copy the RESOURCE ID, which is used in a later step. The resource ID is similar to the following example:
/subscriptions/<Subscription ID>/resourceGroups/myResourceGroupB/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
Complete steps 5-9 for myVnetB, entering UserA in step 8.
Log out of the portal as UserB and log in as UserA.
In the Search resources box at the top of the portal, type myVnetA. Click myVnetA when it appears in the search results. A blade appears for the myVnet virtual network.
Click myVnetA.
In the myVnetA blade that appears, click Peerings from the vertical list of options on the left side of the blade.
In the myVnetA - Peerings blade that appeared, click + Add
In the Add peering blade that appears, enter, or select the following options, then click OK:
- Name: myVnetAToMyVnetB
- Virtual network deployment model: Select Classic.
- I know my resource ID: Check this box.
- Resource ID: Enter the resource ID of myVnetB from step 15.
- Allow virtual network access: Ensure that Enabled is selected. No other settings are used in this tutorial. To learn about all peering settings, read Manage virtual network peerings.
After clicking OK in the previous step, the Add peering blade closes and you see the myVnetA - Peerings blade again. After a few seconds, the peering you created appears in the blade. Connected is listed in the PEERING STATUS column for the myVnetAToMyVnetB peering you created. The peering is now established. There is no need to peer the virtual network (classic) to the virtual network (Resource Manager).
Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. If you want to resolve names across virtual networks in a peering, you must create your own DNS server. Learn how to set up Name resolution using your own DNS server.
Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.
Optional: To delete the resources that you create in this tutorial, complete the steps in the Delete resources section of this article.
Create peering - Azure CLI
This tutorial uses different accounts for each subscription. If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of Azure, and remove the lines of script that create user role assignments. Replace [email protected] and [email protected] in all of the following scripts with the usernames you're using for UserA and UserB. Complete the following steps using the Azure classic CLI and the Azure CLI. You can complete the steps from the Azure Cloud Shell, by just selecting the Try it button in any of the following steps, or by installing the classic CLI and CLI and running the commands on your local computer.
If using the Cloud Shell, skip to step 2, because the Cloud Shell automatically signs you in to Azure. Open a command session and sign in to Azure using the
azure login
command.Run the classic CLI in Service Management mode by entering the
azure config mode asm
command.Enter the following classic CLI command to create the virtual network (classic):
azure network vnet create --vnet myVnetB --address-space 10.1.0.0 --cidr 16 --location "East US"
The remaining steps must be completed using a bash shell with the Azure CLI (not the classic CLI).
Copy the following script to a text editor on your PC. Replace
<SubscriptionB-Id>
with your subscription ID. If you don't know your subscription Id, enter theaz account show
command. The value for id in the output is your subscription Id. Copy the modified script, paste it in to your CLI session, and then pressEnter
.az role assignment create \ --assignee [email protected] \ --role "Classic Network Contributor" \ --scope /subscriptions/<SubscriptionB-Id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
When you created the virtual network (classic) in step 4, Azure created the virtual network in the Default-Networking resource group.
Log UserB out of Azure and log in as UserA in the CLI.
Create a resource group and a virtual network (Resource Manager). Copy the following script, paste it in to your CLI session, and then press
Enter
.#!/bin/bash # Variables for common values used throughout the script. rgName="myResourceGroupA" location="eastus" # Create a resource group. az group create \ --name $rgName \ --location $location # Create virtual network A (Resource Manager). az network vnet create \ --name myVnetA \ --resource-group $rgName \ --location $location \ --address-prefix 10.0.0.0/16 # Get the id for myVnetA. vNetAId=$(az network vnet show \ --resource-group $rgName \ --name myVnetA \ --query id --out tsv) # Assign UserB permissions to myVnetA. az role assignment create \ --assignee [email protected] \ --role "Network Contributor" \ --scope $vNetAId
Create a virtual network peering between the two virtual networks created through the different deployment models. Copy the following script to a text editor on your PC. Replace
<SubscriptionB-id>
with your subscription Id. If you don't know your subscription Id, enter theaz account show
command. The value for id in the output is your subscription Id. Azure created the virtual network (classic) you created in step 4 in a resource group named Default-Networking. Paste the modified script in your CLI session, and then pressEnter
.# Peer VNet1 to VNet2. az network vnet peering create \ --name myVnetAToMyVnetB \ --resource-group $rgName \ --vnet-name myVnetA \ --remote-vnet /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB \ --allow-vnet-access
After the script executes, review the peering for the virtual network (Resource Manager). Copy the following script, and then paste it in your CLI session:
az network vnet peering list \ --resource-group $rgName \ --vnet-name myVnetA \ --output table
The output shows Connected in the PeeringState column.
Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. If you want to resolve names across virtual networks in a peering, you must create your own DNS server. Learn how to set up Name resolution using your own DNS server.
Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.
Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.
Create peering - PowerShell
This tutorial uses different accounts for each subscription. If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of Azure, and remove the lines of script that create user role assignments. Replace [email protected] and [email protected] in all of the following scripts with the usernames you're using for UserA and UserB.
Install the latest version of the PowerShell Azure and Az modules. If you're new to Azure PowerShell, see Azure PowerShell overview.
Start a PowerShell session.
In PowerShell, log in to UserB's subscription as UserB by entering the
Add-AzureAccount
command. The account you log in with must have the necessary permissions to create a virtual network peering. For a list of permissions, see Virtual network peering permissions.To create a virtual network (classic) with PowerShell, you must create a new, or modify an existing, network configuration file. Learn how to export, update, and import network configuration files. The file should include the following VirtualNetworkSite element for the virtual network used in this tutorial:
<VirtualNetworkSite name="myVnetB" Location="East US"> <AddressSpace> <AddressPrefix>10.1.0.0/16</AddressPrefix> </AddressSpace> <Subnets> <Subnet name="default"> <AddressPrefix>10.1.0.0/24</AddressPrefix> </Subnet> </Subnets> </VirtualNetworkSite>
Warning
Importing a changed network configuration file can cause changes to existing virtual networks (classic) in your subscription. Ensure you only add the previous virtual network and that you don't change or remove any existing virtual networks from your subscription.
Log in to UserB's subscription as UserB to use Resource Manager commands by entering the
Connect-AzAccount
command.Assign UserA permissions to virtual network B. Copy the following script to a text editor on your PC and replace
<SubscriptionB-id>
with the ID of subscription B. If you don't know the subscription Id, enter theGet-AzSubscription
command to view it. The value for Id in the returned output is your subscription ID. Azure created the virtual network (classic) you created in step 4 in a resource group named Default-Networking. To execute the script, copy the modified script, paste it in to PowerShell, and then pressEnter
.New-AzRoleAssignment ` -SignInName [email protected] ` -RoleDefinitionName "Classic Network Contributor" ` -Scope /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
Log out of Azure as UserB and log in to UserA's subscription as UserA by entering the
Connect-AzAccount
command. The account you log in with must have the necessary permissions to create a virtual network peering. For a list of permissions, see Virtual network peering permissions.Create the virtual network (Resource Manager) by copying the following script, pasting it in to PowerShell, and then pressing
Enter
:# Variables for common values $rgName='MyResourceGroupA' $location='eastus' # Create a resource group. New-AzResourceGroup ` -Name $rgName ` -Location $location # Create virtual network A. $vnetA = New-AzVirtualNetwork ` -ResourceGroupName $rgName ` -Name 'myVnetA' ` -AddressPrefix '10.0.0.0/16' ` -Location $location
Assign UserB permissions to myVnetA. Copy the following script to a text editor on your PC and replace
<SubscriptionA-Id>
with the ID of subscription A. If you don't know the subscription Id, enter theGet-AzSubscription
command to view it. The value for Id in the returned output is your subscription ID. Paste the modified version of the script in PowerShell, and then pressEnter
to execute it.New-AzRoleAssignment ` -SignInName [email protected] ` -RoleDefinitionName "Network Contributor" ` -Scope /subscriptions/<SubscriptionA-Id>/resourceGroups/myResourceGroupA/providers/Microsoft.Network/VirtualNetworks/myVnetA
Copy the following script to a text editor on your PC, and replace
<SubscriptionB-id>
with the ID of subscription B. To peer myVnetA to myVNetB, copy the modified script, paste it in to PowerShell, and then pressEnter
.Add-AzVirtualNetworkPeering ` -Name 'myVnetAToMyVnetB' ` -VirtualNetwork $vnetA ` -RemoteVirtualNetworkId /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
View the peering state of myVnetA by copying the following script, pasting it into PowerShell, and pressing
Enter
.Get-AzVirtualNetworkPeering ` -ResourceGroupName $rgName ` -VirtualNetworkName myVnetA ` | Format-Table VirtualNetworkName, PeeringState
The state is Connected. It changes to Connected once you set up the peering to myVnetA from myVnetB.
Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. If you want to resolve names across virtual networks in a peering, you must create your own DNS server. Learn how to set up Name resolution using your own DNS server.
Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.
Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.
Delete resources
When you've finished this tutorial, you might want to delete the resources you created in the tutorial, so you don't incur usage charges. Deleting a resource group also deletes all resources that are in the resource group.
Azure portal
- In the portal search box, enter myResourceGroupA. In the search results, click myResourceGroupA.
- On the myResourceGroupA blade, click the Delete icon.
- To confirm the deletion, in the TYPE THE RESOURCE GROUP NAME box, enter myResourceGroupA, and then click Delete.
- In the Search resources box at the top of the portal, type myVnetB. Click myVnetB when it appears in the search results. A blade appears for the myVnetB virtual network.
- In the myVnetB blade, click Delete.
- To confirm the deletion, click Yes in the Delete virtual network box.
Azure CLI
Log in to Azure using the CLI to delete the virtual network (Resource Manager) with the following command:
az group delete --name myResourceGroupA --yes
Sign in to Azure using the classic CLI to delete the virtual network (classic) with the following commands:
azure config mode asm azure network vnet delete --vnet myVnetB --quiet
PowerShell
At the PowerShell command prompt, enter the following command to delete the virtual network (Resource Manager):
Remove-AzResourceGroup -Name myResourceGroupA -Force
To delete the virtual network (classic) with PowerShell, you must modify an existing network configuration file. Learn how to export, update, and import network configuration files. Remove the following VirtualNetworkSite element for the virtual network used in this tutorial:
<VirtualNetworkSite name="myVnetB" Location="East US"> <AddressSpace> <AddressPrefix>10.1.0.0/16</AddressPrefix> </AddressSpace> <Subnets> <Subnet name="default"> <AddressPrefix>10.1.0.0/24</AddressPrefix> </Subnet> </Subnets> </VirtualNetworkSite>
Warning
Importing a changed network configuration file can cause changes to existing virtual networks (classic) in your subscription. Ensure you only remove the previous virtual network and that you don't change or remove any other existing virtual networks from your subscription.
Next steps
- Thoroughly familiarize yourself with important virtual network peering constraints and behaviors before creating a virtual network peering for production use.
- Learn about all virtual network peering settings.
- Learn how to create a hub and spoke network topology with virtual network peering.