Deploying a Virtual Network
In this guide, we'll walk through the steps to deploy a virtual network with subnets in Azure using Bicep.
Azure Virtual Network is a service that provides the fundamental building block for your private network in Azure.
An instance of the service (a virtual network) enables many types of Azure resources to securely communicate with each other, the internet, and on-premises networks. These Azure resources include virtual machines (VMs).
A virtual network is similar to a traditional network that you'd operate in your own datacenter.
But it brings extra benefits of the Azure infrastructure, such as scale, availability, and isolation.
Create a bicep file
Open Visual Studio Code in the C:\DTX-IaC-Demo
folder and create a new file with the name virtualnetwork.bicep
.
Create code
Write the Bicep code to define the virtual network and save the file. Below is a sample code snippet:
targetScope = 'resourceGroup'
param location string = 'westeurope'
param name string = 'vnet-dtx-demo-001'
param addressPrefixes string = '10.0.0.0/16'
resource resVirtualNetwork 'Microsoft.Network/virtualNetworks@2022-07-01' = {
name: name
location: location
properties: {
addressSpace: {
addressPrefixes: [
addressPrefixes
]
}
enableDdosProtection: false
ddosProtectionPlan: null
dhcpOptions: null
}
}
Deploy the resource
az deployment group create --template-file .\virtualnetwork.bicep --resource-group 'rg-networking'
Please note that the resource group rg-networking
should already exist in the subscription.
And please note that the az deployment command is different from the previous guide as this is a resource group deployment.
Subnets
Currently the Virtual Network does not have any subnets.
Edit the virtualnetwork.bicep file and add the following code to create a subnet.
Under param addressPrefixes add the following code:
param subnets array = [
{
name: 'snet-001'
ipAddressRange: '10.0.0.0/24'
}
]
Under dhcpOptions: null
add the following code:
subnets: [for subnet in subnets: {
name: subnet.name
properties: {
addressPrefix: subnet.ipAddressRange
}
}]
Redeploy the resource group with the following command:
az deployment group create --template-file .\virtualnetwork.bicep --resource-group 'rg-networking'
If you want more subnets you can add snet-002 to the array the code will look like this:
param subnets array = [
{
name: 'snet-001'
ipAddressRange: '10.0.0.0/24'
}
{
name: 'snet-002'
ipAddressRange: '10.0.1.0/24'
}
]
Template
Your bicep deploy file virtualnetwork.bicep
should look like this:
targetScope = 'resourceGroup'
param location string = 'westeurope'
param name string = 'vnet-dtx-demo-001'
param addressPrefixes string = '10.0.0.0/16'
param subnets array = [
{
name: 'snet-001'
ipAddressRange: '10.0.0.0/24'
}
{
name: 'snet-002'
ipAddressRange: '10.0.1.0/24'
}
]
resource resVirtualNetwork 'Microsoft.Network/virtualNetworks@2022-07-01' = {
name: name
location: location
properties: {
addressSpace: {
addressPrefixes: [
addressPrefixes
]
}
enableDdosProtection: false
ddosProtectionPlan: null
dhcpOptions: null
subnets: [for subnet in subnets: {
name: subnet.name
properties: {
addressPrefix: subnet.ipAddressRange
}
}]
}
}
Speedy
Are you deploying resources like speedy gonzales, maybe you can try to deploy a second virtual network with a different address space and subnets with a parameter file.
Version overview
This document has the following versions:
Version | Date | Overview of changes |
---|---|---|
1.2 | 2024-03-22 | Typo in the name virtualnetwork.bicep |
1.1 | 2024-03-18 | Added comments and improvements from test run. |
1.0 | 2024-02-28 | Initial version. |