Create a virtual network peering - Resource Manager, different subscriptions and Microsoft Entra tenants

In this tutorial, you learn to create a virtual network peering between virtual networks created through Resource Manager. The virtual networks exist in different subscriptions that might belong to different Microsoft Entra tenants. 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.

Depending on whether, the virtual networks are in the same, or different subscriptions the steps to create a virtual network peering are different. Steps to peer networks created with the classic deployment model are different. For more information about deployment models, see Azure deployment model.

Learn how to create a virtual network peering in other scenarios by selecting the scenario from the following table:

Azure deployment model Azure subscription
Both Resource Manager Same
One Resource Manager, one classic Same
One Resource Manager, one classic Different

A virtual network peering can't be created between two virtual networks deployed through the classic deployment model. If you need to connect virtual networks that were both created through the classic deployment model, you can use an Azure VPN Gateway to connect the virtual networks.

This tutorial peers virtual networks in the same region. You can also peer virtual networks in different supported regions. Familiarize yourself with the peering requirements and constraints before peering virtual networks.

Prerequisites

  • An Azure account or accounts with two active subscriptions. Create an account.

  • An Azure account with permissions in both subscriptions or an account in each subscription with the proper permissions to create a virtual network peering. For a list of permissions, see Virtual network peering permissions.

    • To separate the duty of managing the network belonging to each tenant, add the user from each tenant as a guest in the opposite tenant and assign them the Network Contributor role to the virtual network. This procedure applies if the virtual networks are in different subscriptions and Active Directory tenants.

    • To establish a network peering when you don't intend to separate the duty of managing the network belonging to each tenant, add the user from tenant A as a guest in the opposite tenant. Then, assign them the Network Contributor role to initiate and connect the network peering from each subscription. With these permissions, the user is able to establish the network peering from each subscription.

    • For more information about guest users, see Add Microsoft Entra B2B collaboration users in the Azure portal.

    • Each user must accept the guest user invitation from the opposite Microsoft Entra tenant.

  • If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

  • This how-to article requires version 2.31.0 or later of the Azure CLI.

In the following steps, learn how to peer virtual networks in different subscriptions and Microsoft Entra tenants.

You can use the same account that has permissions in both subscriptions or you can use separate accounts for each subscription to set up the peering. An account with permissions in both subscriptions can complete all of the steps without signing out and signing in to portal and assigning permissions.

The following resources and account examples are used in the steps in this article:

User account Resource group Subscription Virtual network
user-1 test-rg subscription-1 vnet-1
user-2 test-rg-2 subscription-2 vnet-2

Create virtual network - vnet-1

Note

If you are using a single account to complete the steps, you can skip the steps for logging out of the portal and assigning another user permissions to the virtual networks.

Sign in to subscription-1

Use az sign-in to sign in to subscription-1.

az login

If you're using one account for both subscriptions, sign in to that account and change the subscription context to subscription-1 with az account set.

az account set --subscription "subscription-1"

Create a resource group - test-rg

An Azure resource group is a logical container where Azure resources are deployed and managed.

Create a resource group with az group create:

az group create \
    --name test-rg \
    --location eastus2

Create the virtual network

Create a virtual network and subnet with az network vnet create. This example creates a subnet-1 virtual network named vnet-1 in the West US 3 location.

az network vnet create \
    --resource-group test-rg\
    --location eastus2 \
    --name vnet-1 \
    --address-prefixes 10.0.0.0/16 \
    --subnet-name subnet-1 \
    --subnet-prefixes 10.0.0.0/24

Assign permissions for user-2

A user account in the other subscription that you want to peer with must be added to the network you previously created. If you're using a single account for both subscriptions, you can skip this section.

Use az network vnet show to obtain the resource ID for vnet-1. Assign user-2 from subscription-2 to vnet-1 with az role assignment create.

Use az ad user list to obtain the object ID for user-2.

user-2 is used in this example for the user account. Replace this value with the display name for the user from subscription-2 that you wish to assign permissions to vnet-1. You can skip this step if you're using the same account for both subscriptions.

az ad user list --display-name user-2
[
  {
    "businessPhones": [],
    "displayName": "user-2",
    "givenName": null,
    "id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    "jobTitle": null,
    "mail": "user-2@fabrikam.com",
    "mobilePhone": null,
    "officeLocation": null,
    "preferredLanguage": null,
    "surname": null,
    "userPrincipalName": "user-2_fabrikam.com#EXT#@contoso.partner.onmschina.cn"
  }
]

Make note of the object ID of user-2 in field id. In this example, its aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb.

vnetid=$(az network vnet show \
    --name vnet-1 \
    --resource-group test-rg \
    --query id \
    --output tsv)

az role assignment create \
      --assignee aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb \
      --role "Network Contributor" \
      --scope $vnetid

Replace the example guid in --assignee with the real object ID for user-2.

Obtain resource ID of vnet-1

The resource ID of vnet-1 is required to set up the peering connection from vnet-2 to vnet-1. Use az network vnet show to obtain the resource ID for vnet-1.

vnetidA=$(az network vnet show \
    --name vnet-1 \
    --resource-group test-rg \
    --query id \
    --output tsv)

echo $vnetidA

Create virtual network - vnet-2

In this section, you sign in as user-2 and create a virtual network for the peering connection to vnet-1.

Sign in to subscription-2

Use az sign-in to sign in to subscription-1.

az login

If you're using one account for both subscriptions, sign in to that account and change the subscription context to subscription-2 with az account set.

az account set --subscription "subscription-2"

Create a resource group - test-rg-2

An Azure resource group is a logical container where Azure resources are deployed and managed.

Create a resource group with az group create:

az group create \
    --name test-rg-2 \
    --location chinaeast2

Create the virtual network

Create a virtual network and subnet with az network vnet create. This example creates a subnet-1 virtual network named vnet-2 in the China East 2 location.

az network vnet create \
    --resource-group test-rg-2\
    --location chinaeast2 \
    --name vnet-2 \
    --address-prefixes 10.1.0.0/16 \
    --subnet-name subnet-1 \
    --subnet-prefixes 10.1.0.0/24

Assign permissions for user-1

A user account in the other subscription that you want to peer with must be added to the network you previously created. If you're using a single account for both subscriptions, you can skip this section.

Use az network vnet show to obtain the resource ID for vnet-2. Assign user-1 from subscription-1 to vnet-2 with az role assignment create.

Use az ad user list to obtain the object ID for user-1.

user-1 is used in this example for the user account. Replace this value with the display name for the user from subscription-1 that you wish to assign permissions to vnet-2. You can skip this step if you're using the same account for both subscriptions.

az ad user list --display-name user-1
[
  {
    "businessPhones": [],
    "displayName": "user-1",
    "givenName": null,
    "id": "bbbbbbbb-1111-2222-3333-cccccccccccc",
    "jobTitle": null,
    "mail": "user-1@contoso.com",
    "mobilePhone": null,
    "officeLocation": null,
    "preferredLanguage": null,
    "surname": null,
    "userPrincipalName": "user-1_contoso.com#EXT#@fabrikam.partner.onmschina.cn"
  }
]

Make note of the object ID of user-1 in field id. In this example, it's bbbbbbbb-1111-2222-3333-cccccccccccc.

vnetid=$(az network vnet show \
    --name vnet-2 \
    --resource-group test-rg-2 \
    --query id \
    --output tsv)

az role assignment create \
      --assignee bbbbbbbb-1111-2222-3333-cccccccccccc \
      --role "Network Contributor" \
      --scope $vnetid

Obtain resource ID of vnet-2

The resource ID of vnet-2 is required to set up the peering connection from vnet-1 to vnet-2. Use the following steps to obtain the resource ID of vnet-2.

The resource ID of vnet-2 is required to set up the peering connection from vnet-1 to vnet-2. Use az network vnet show to obtain the resource ID for vnet-2.

vnetidB=$(az network vnet show \
    --name vnet-2 \
    --resource-group test-rg-2 \
    --query id \
    --output tsv)

echo $vnetidB

Create peering connection - vnet-1 to vnet-2

You need the Resource ID for vnet-2 from the previous steps to set up the peering connection.

Sign in to subscription-1

Use az sign-in to sign in to subscription-1.

az login

If you're using one account for both subscriptions, sign in to that account and change the subscription context to subscription-1 with az account set.

az account set --subscription "subscription-1"

Sign in to subscription-2

Authenticate to subscription-2 so that the peering can be set up.

Use az sign-in to sign in to subscription-2.

az login

Change to subscription-1 (optional)

You might have to switch back to subscription-1 to continue with the actions in subscription-1.

Change context to subscription-1.

az account set --subscription "subscription-1"

Create peering connection

Use az network vnet peering create to create a peering connection between vnet-1 and vnet-2.

az network vnet peering create \
    --name vnet-1-to-vnet-2 \
    --resource-group test-rg \
    --vnet-name vnet-1 \
    --remote-vnet /subscriptions/<subscription-2-Id>/resourceGroups/test-rg-2/providers/Microsoft.Network/VirtualNetworks/vnet-2 \
    --allow-vnet-access

Use az network vnet peering list to obtain the status of the peering connections from vnet-1 to vnet-2.

az network vnet peering list \
    --resource-group test-rg \
    --vnet-name vnet-1 \
    --output table

The peering connection shows in Peerings in a Initiated state. To complete the peer, a corresponding connection must be set up in vnet-2.

Create peering connection - vnet-2 to vnet-1

You need the Resource IDs for vnet-1 from the previous steps to set up the peering connection.

Sign in to subscription-2

Use az sign-in to sign in to subscription-2.

az login

If you're using one account for both subscriptions, sign in to that account and change the subscription context to subscription-2 with az account set.

az account set --subscription "subscription-2"

Sign in to subscription-1

Authenticate to subscription-1 so that the peering can be set up.

Use az sign-in to sign in to subscription-1.

az login

Change to subscription-2 (optional)

You might have to switch back to subscription-2 to continue with the actions in subscription-2.

Change context to subscription-2.

az account set --subscription "subscription-2"

Create peering connection

Use az network vnet peering create to create a peering connection between vnet-2 and vnet-1.

az network vnet peering create \
    --name vnet-2-to-vnet-1 \
    --resource-group test-rg-2 \
    --vnet-name vnet-2 \
    --remote-vnet /subscriptions/<subscription-1-Id>/resourceGroups/test-rg/providers/Microsoft.Network/VirtualNetworks/vnet-1 \
    --allow-vnet-access

Use az network vnet peering list to obtain the status of the peering connections from vnet-2 to vnet-1.

az network vnet peering list \
    --resource-group test-rg-2 \
    --vnet-name vnet-2 \
    --output table

The peering is successfully established after you see Connected in the Peering status column for both virtual networks in the peering. 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 Azure name resolution for the virtual networks, the resources in the virtual networks aren't 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 (Domain Name System) server or use Azure DNS.

Important

If you update the address space in one of the members of the peer, you must resync the connection to reflect the address space changes. For more information, see Update the address space for a peered virtual network using the Azure portal

For more information about using your own DNS for name resolution, see, Name resolution using your own DNS server.

For more information about Azure DNS, see What is Azure DNS?.

Next steps