Configuring VNet (virtual network) peering with the Azure CLI in Azure

MVP

 

Hi Azure friends,

 

Start the CloudShell in the Azure portal or go to the following URL: https://shell.azure.com/

Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#Here you can find out which subscription you are working with
az account show

 

#View all subscriptions
az account list --all --output table

 

#change the subscription (if necessary)
az account set --subscription "Name of the Subscription"

 

#Create a resource group
az group create --name myResourceGroup --location westeurope

 

#Create virtual networks
az network vnet create --name myVirtualNetwork1 --resource-group myResourceGroup --address-prefixes 10.0.0.0/16 --subnet-name Subnet1 --subnet-prefix 10.0.0.0/24

 

az network vnet create --name myVirtualNetwork2 --resource-group myResourceGroup --address-prefixes 10.1.0.0/16 --subnet-name Subnet1 --subnet-prefix 10.1.0.0/24

 

#Peer virtual networks

#Get the id for myVirtualNetwork1.
vNet1Id=$(az network vnet show --resource-group myResourceGroup --name myVirtualNetwork1 --query id --out tsv)

 

# Get the id for myVirtualNetwork2.
vNet2Id=$(az network vnet show --resource-group myResourceGroup --name myVirtualNetwork2 --query id --out tsv)

 

#Create a peering from myVirtualNetwork1 to myVirtualNetwork2
az network vnet peering create --name myVirtualNetwork1-myVirtualNetwork2 --resource-group myResourceGroup --vnet-name myVirtualNetwork1 --remote-vnet $vNet2Id --allow-vnet-access

 

#Create a peering from myVirtualNetwork2 to myVirtualNetwork1
az network vnet peering create --name myVirtualNetwork2-myVirtualNetwork1 --resource-group myResourceGroup --vnet-name myVirtualNetwork2 --remote-vnet $vNet1Id --allow-vnet-access

 

#Confirm the peering state
az network vnet peering show --name myVirtualNetwork1-myVirtualNetwork2 --resource-group myResourceGroup --vnet-name myVirtualNetwork1 --query peeringState

 

Now you have configured a VNet peering with the Azure CLI! Congratulations!

If you don't need the resource group (also to save costs) don't forget the clean up.

 

#Clean Up (if necessary)
az group delete --name myResourceGroup --yes

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, @azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

0 Replies