Udostępnij za pośrednictwem


Peer two virtual networks with an Azure CLI script sample

This script sample creates and connects two virtual networks in the same region through the Azure network. After running the script, you have a peering between two virtual networks.

If you don't have an Azure subscription, create an Azure free account before you begin.

Prerequisites

Sample script

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.

When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.

Sign in to Azure

Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing <Subscription ID> with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

For more information, see set active subscription or log in interactively

Run the script

# Peer two virtual networks

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-virtual-network-rg-$randomIdentifier"
tag="peer-two-virtual-networks"
vNet1="msdocs-vNet-$randomIdentifier"
addressPrefixVNet1="10.0.0.0/16"
vNet2="msdocs-vNet2-$randomIdentifier"
addressPrefixVNet2="10.1.0.0/16"

echo "Using resource group $resourceGroup with login: $login"

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create virtual network 1.
echo "Creating $vNet1"
az network vnet create --name $vNet1 --resource-group $resourceGroup --location "$location" --address-prefix $addressPrefixVNet1

# Create virtual network 2.
echo "Creating $vNet2"
az network vnet create --name $vNet2 --resource-group $resourceGroup --location "$location" --address-prefix $addressPrefixVNet2

# Get the id for VNet1.
echo "Getting the id for $vNet1"
VNet1Id=$(az network vnet show --resource-group $resourceGroup --name $vNet1 --query id --out tsv)

# Get the id for VNet2.
echo "Getting the id for $vNet2"
VNet2Id=$(az network vnet show --resource-group $resourceGroup --name $vNet2 --query id --out tsv)

# Peer VNet1 to VNet2.
echo "Peering $vNet1 to $vNet2"
az network vnet peering create --name "Link"$vNet1"To"$vNet2 --resource-group $resourceGroup --vnet-name $vNet1 --remote-vnet $VNet2Id --allow-vnet-access

# Peer VNet2 to VNet1.
echo "Peering $vNet2 to $vNet1"
az network vnet peering create --name "Link"$vNet2"To"$vNet1 --resource-group $resourceGroup --vnet-name $vNet2 --remote-vnet $VNet1Id --allow-vnet-access

Clean up deployment

Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.

az group delete --name $resourceGroup

Sample reference

This script uses the following commands to create a resource group, virtual machine, and all related resources. Each command in the following table links to command-specific documentation:

Command Notes
az group create Creates a resource group in which all resources are stored.
az network vnet create Creates an Azure virtual network and subnet.
az network vnet peering create Creates a peering between two virtual networks.
az group delete Deletes a resource group including all nested resources.

Next steps

For more information on the Azure CLI, see Azure CLI documentation.

Additional virtual network CLI script samples can be found in Virtual network CLI samples.