adaptive.run TECH BLOG

Cloud can be tricky sometimes. Find out what scenarios we've ran into that are worth being mentioned and explained.

Deploying VNets with ARM Templates and Establishing VNet Peering with PowerShell

Level: 200
Publishing date: 28-Nov-2023
Author: Catalin Popa


Introduction:

In the dynamic landscape of cloud computing, the ability to create a well-connected and secure network infrastructure is crucial. Microsoft Azure provides a powerful combination of Azure Resource Manager (ARM) templates and PowerShell commands to simplify the deployment of Virtual Networks (VNets) and establish seamless communication between them through VNet peering. This article will guide you through the process of deploying two VNets using ARM templates and establishing VNet peering with PowerShell.

Creating the ARM Template for Dual VNets Deployment

Azure Resource Manager (ARM) templates serve as the foundation for deploying and managing Azure resources. To deploy two VNets and establish VNet peering, we'll create an ARM template.


{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2021-02-01",
"name": "VNet1",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": ["10.1.0.0/16"]
},
"subnets": [
{
"name": "Subnet1",
"properties": {
"addressPrefix": "10.1.1.0/24"
}
}
]
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2021-02-01",
"name": "VNet2",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": ["10.2.0.0/16"]
},
"subnets": [
{
"name": "Subnet2",
"properties": {
"addressPrefix": "10.2.1.0/24"
}
}
]
}
}
]

This ARM template deploys two VNets, each with a subnet, providing a foundation for secure network architecture.

The Script: PowerShell Commands for VNet Peering

Now, let's use PowerShell commands to establish VNet peering between the deployed VNets.


# Set your Azure subscription and resource group details
$resourceGroupName = ""
$location = ""

# Authenticate to Azure
Connect-AzAccount

# Deploy the ARM template
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile "path/to/your/template.json"

# Get the VNet objects
$vnet1 = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name "VNet1"
$vnet2 = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name "VNet2"

# Establish VNet peering

Add-AzVirtualNetworkPeering -Name "VNet1-to-VNet2" -VirtualNetwork $vnet1 -RemoteVirtualNetworkId $vnet2.Id
Add-AzVirtualNetworkPeering -Name "VNet2-to-VNet1" -VirtualNetwork $vnet2 -RemoteVirtualNetworkId $vnet1.Id 

This PowerShell script authenticates to your Azure account, deploys the ARM template, retrieves VNet objects, and establishes bidirectional VNet peering between the two VNets.

Conclusion:

With the combined power of ARM templates and PowerShell commands, Azure empowers you to build a robust and interconnected network infrastructure. Deploying dual VNets and establishing VNet peering enhances the security and flexibility of your Azure network, creating a foundation for scalable and resilient cloud solutions. As you embark on your journey to architect a secure Azure network, the synergy between ARM templates and PowerShell commands ensures a streamlined and repeatable process.


adaptive.run

Transform your business.
Run adaptive.

Contact

Phone: +40 73 523 0005
Email: hello@adaptive.run

© Copyright  2019-2024 adaptive.run- All Rights Reserved