Terraform-Defined Azure Virtual Networks With VNet Manager and ExpressRoute

Azure Virtual Network is a network that provides various network-related services in Azure. It connects groups of resources and isolates them from outside access in azure cloud. The reference implementation material describes how Terraform can be used to declare, preview and deploy that isolation boundary as code. The interaction between Terraform, the Azure Resource Manager provider, and Azure Virtual Network Manager creates a reproducible topology where three virtual networks can be provisioned with a mesh connectivity policy and where an ExpressRoute circuit can be bound to a virtual network for private peering.

Terraform is described as Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code. The description emphasizes Infrastructure as a Code allows to representation of cloud infrastructure in the form of code. Azure Virtual Network in Microsoft Azure is an isolated network that protects a group of resources. Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed.

Azure Virtual Networks are the foundation of private networking in Azure. They can isolate your resources from the public internet, let you control traffic flow with subnets and network security groups, and connect to on-premises networks or other VNets through peering and VPN gateways. Getting your network architecture right at the start is critical because changing it later can require readdressing or migrating workloads. Terraform makes it straightforward to define, version, and replicate your network topology.

Primary Terminologies And Core Concepts

Azure Vnet also called Azure Virtual Network is a network that provides various network-related services in Azure. It connects groups of resources and isolates them from outside access in azure cloud.

Terraform is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.

Azure Virtual Network in Microsoft Azure is an isolated network that protects a group of resources.

IaaC is Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.

The impact of these definitions is that the network boundary is no longer a manual portal operation but a declarative artifact. The contextual link is that Terraform's HCL files become the source of truth for both the VNet address space and the subnet prefixes that enforce isolation.

| Term | Description from reference facts |
| Terraform | Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code |
| Azure Virtual Network | A network that provides various network-related services in Azure. It connects groups of resources and isolates them from outside access |
| IaaC | Infrastructure as a Code allows to representation of cloud infrastructure in the form of code |
| Azure Virtual Network Manager | Used to provision connectivity for all virtual networks and create a mesh network topology |

Terraform Installation Prerequisites For Azure VNet Workloads

Step 1 is Set Up Terraform.

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
  • For MacOs install the terraform using HomeBrew.

The download step establishes the binary that will interpret HCL. Extracting and adding to PATH makes the terraform command available in terminal sessions. On macOS the Homebrew path reduces manual extraction and ensures version updates are repeatable.

The impact for operators is that without a correctly pathed binary, terraform init and terraform apply will fail with command not found errors, blocking the entire VNet provisioning pipeline. Contextually, this prerequisite precedes Azure CLI authentication because Terraform will later use Azure credentials supplied by the CLI login session.

Azure CLI Installation And Authentication Flow

Step 2 is Set Up Azure CLI.

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.
  • For MacOs install the Azure CLI using below HomeBrew Command.

brew update && brew install azure-cli

Step 3 is Configure Azure CLI.

  • Open terminal and run below command.

az login

  • A browser window will open for login

The Homebrew command updates package metadata and installs the CLI. az login initiates device code authentication and opens a browser window for login. The session token is stored locally and used by the azurerm provider for subsequent API calls.

The real-world consequence is that a failed login results in provider authentication errors during terraform plan. The contextual link is that the Azure CLI token is the bridge between Terraform's provider configuration and the Azure Resource Manager API that creates the resource group and virtual network.

Provider Configuration And Version Constraints

The reference material shows two provider configuration styles.

First style for a single VNet:

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } } } provider "azurerm" { features {} }

Second style for Virtual Network Manager with random naming:

terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.56.0, < 4.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }

Version pinning for azurerm at ~>3.0 versus ~> 3.56.0, < 4.0 controls API compatibility. The random provider is used to generate non-deterministic names for resource groups and virtual networks.

The impact is that an unpinned provider can introduce breaking changes to resource schemas, causing plan failures. Contextually, the provider block is the first evaluated node in the dependency graph and determines which Azure API versions are available for azurerm_virtual_network and azurerm_subnet.

| Provider file | required_version | azurerm version | additional providers |
| providers.tf for VNet Manager | >=1.0 | ~> 3.56.0, < 4.0 | random ~>3.0 |
| providers.tf for single VNet | not specified | ~>3.0 | none |

Single Virtual Network Definition With Inline Subnet

The step-by-step guide specifies we have specified the location and resource group where virtual network should be created. We have added a subnet with /24 prefix in the virtual network.

The complete code will look like below.

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } } } provider "azurerm" { features {} } resource "azurerm_virtual_network" "samplevnet" { name = "samplevnet" resource_group_name = "DeepsLab" location = "eastus" address_space = [ "10.0.0.0/16"] subnet { name = "subnet-A" address_prefix = "10.0.1.0/24" } }

name = "samplevnet" sets the VNet name. resource_group_name = "DeepsLab" binds the VNet to an existing resource group. location = "eastus" places the VNet in the East US region. address_space = [ "10.0.0.0/16"] defines the supernet. The inline subnet block creates subnet-A with address_prefix = "10.0.1.0/24".

The impact is that the /16 supernet allows 65,536 addresses while the /24 subnet allocates 256 usable addresses for workloads. Contextually, the inline subnet definition is simpler for one-off networks but does not allow separate lifecycle management of subnets.

| Attribute | Value | Purpose |
| name | samplevnet | VNet identifier in Azure |
| resourcegroupname | DeepsLab | Containing resource group |
| location | eastus | Azure region |
| addressspace | 10.0.0.0/16 | VNet supernet |
| subnet name | subnet-A | Subnet identifier |
| address
prefix | 10.0.1.0/24 | Subnet CIDR |

Apply The Terraform Code And Verify Deployment

Step 5 is Apply The Terraform Code.

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.

terraform init

  • After successful output of terraform apply the changes using below command.

terraform apply

  • After verifying type "yes" to confirm and apply.
  • Terraform will start creating network.
  • You can also verify deployment by visiting Virtual Networks page of Azure.

terraform init downloads provider plugins. terraform apply creates an execution plan and applies it after user confirmation. The plan preview allows verification before changes are made to actual resources.

The impact is that init failures block plugin availability, while apply failures leave partial resources in Azure that may require manual cleanup. Contextually, verification in the Azure portal closes the loop between declarative code and actual resource state.

Multi-VNet Mesh Topology With Virtual Network Manager

Get started with Azure Virtual Network Manager by using Terraform to provision connectivity for all your virtual networks.

In this quickstart, you deploy three virtual networks and use Azure Virtual Network Manager to create a mesh network topology. Then, you verify that the connectivity configuration was applied. You can choose from a deployment with a Subscription scope or a management group scope. Learn more about network manager scopes.

The main.tf for this pattern creates a resource group, three virtual networks, and a subnet per virtual network.

```

Create the Resource Group

resource "randompet" "rgname" {
prefix = var.resourcegroupnameprefix
}
resource "azurerm
resourcegroup" "rg" {
location = var.resource
grouplocation
name = random
pet.rg_name.id
}

Create three virtual networks

resource "randomstring" "prefix" {
length = 4
special = false
upper = false
}
resource "random
pet" "virtualnetworkname" {
prefix = "vnet-${randomstring.prefix.result}"
}
resource "azurerm
virtualnetwork" "vnet" {
count = 3
name = "${random
pet.virtualnetworkname.id}-0${count.index}"
resourcegroupname = azurermresourcegroup.rg.name
location = azurermresourcegroup.rg.location
address_space = ["10.${count.index}.0.0/16"]
}

Add a subnet to each virtual network

resource "azurermsubnet" "subnetvnet" {
count = 3
name = "default"
virtualnetworkname = azurermvirtualnetwork.vnet[count.index].name
resourcegroupname = azurermresourcegroup.rg.name
addressprefixes = ["10.${count.index}.0.0/24"]
}
data "azurerm
subscription" "current" {
}

Create a Management Group

resource "randompet" "managementgroup_name" {
prefix =
```

The count = 3 meta-argument replicates the VNet resource three times. The address space uses 10.${count.index}.0.0/16 for VNet 0,1,2 and 10.${count.index}.0.0/24 for each subnet.

The impact is that mesh connectivity can be applied centrally via Virtual Network Manager instead of per peering. The contextual link is that the random naming resources prevent name collisions across repeated applies and enable versioning of the topology.

Resource Group Naming With Random Providers

random_pet and random_string are used to generate names.

random_string with length = 4, special = false, upper = false creates a lowercase alphanumeric prefix. random_pet with prefix = "vnet-${random_string.prefix.result}" builds a pet name for each VNet.

The impact is that deterministic naming is avoided, reducing risk of naming conflicts in shared subscriptions. Contextually, these names flow into azurerm_resource_group and azurerm_virtual_network name arguments, making the entire topology uniquely identifiable.

Subnet Provisioning Patterns Per Virtual Network

The azurerm_subnet resource uses count = 3, name = "default", virtual_network_name = azurerm_virtual_network.vnet[count.index].name, resource_group_name = azurerm_resource_group.rg.name, address_prefixes = ["10.${count.index}.0.0/24"].

Each VNet receives a default subnet in the first /24 of its /16. The address prefixes are non-overlapping across VNets.

The impact is that workloads in different VNets can communicate via Virtual Network Manager mesh without overlapping CIDRs. Contextually, the subnet prefix choice determines the size of the host pool per VNet and must remain within the VNet address_space.

Verification Commands For Effective Connectivity

To verify connectivity configuration:

az network manager list-effective-connectivity-config \ --resource-group $resource_group_name \ --vnet-name <virtual_network_name>

Replace the <virtual_network_name> placeholder with the virtual network name.

The command queries the effective connectivity configuration applied by Virtual Network Manager. The impact is immediate visibility into whether the mesh policy is enforced. Contextually, this CLI verification complements Terraform's plan output.

Destroy Plan Workflow And Cleanup

When you no longer need the resources created via Terraform, do the following steps:

Run terraform plan and specify the destroy flag.

terraform plan -destroy -out main.destroy.tfplan

Key points:
- The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
- The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.

Run terraform apply to apply the execution plan.

terraform apply main.destroy.tfplan

The destroy plan creates a file that captures the exact set of resources to delete. Applying the plan removes the resource group, virtual networks, subnets, and associated Virtual Network Manager objects.

The impact is safe teardown without accidental deletion of unrelated resources. Contextually, the same plan-apply workflow used for creation is reused for destruction, maintaining consistency.

ExpressRoute Circuit And Virtual Network Integration

In this quickstart, you use Terraform to create an Azure ExpressRoute circuit and its associated infrastructure. The Terraform template creates a complete ExpressRoute setup including a virtual network, ExpressRoute gateway, circuit configuration, and private peering. All resources are deployed with configurable parameters that allow you to customize the deployment for your specific requirements.

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed.

ExpressRoute provides private connectivity from on-premises networks to Azure. The Terraform template binds the ExpressRoute gateway to the virtual network and configures private peering.

The impact is that hybrid connectivity is codified alongside VNet definition, enabling version control of both private and public address spaces. Contextually, this extends the VNet Manager mesh to on-premises networks.

Network Architecture Implications And Change Costs

Azure Virtual Networks are the foundation of private networking in Azure. They can isolate your resources from the public internet, let you control traffic flow with subnets and network security groups, and connect to on-premises networks or other VNets through peering and VPN gateways.

Getting your network architecture right at the start is critical because changing it later can require readdressing or migrating workloads. Terraform makes it straightforward to define, version, and replicate your network topology.

The address space choice 10.0.0.0/16 locks the VNet to a specific private range. Expanding later requires a new VNet or complex peering. Subnet prefixes like 10.0.1.0/24 determine the maximum number of IP addresses available per workload tier.

The impact is that mis-sized address spaces lead to renumbering, which is expensive operationally. The contextual link is that Terraform's versioned configuration allows teams to replicate the correct sizing across dev, test, and prod environments.

Conclusion

The reference material demonstrates that Terraform virtual network provisioning in Azure is a layered process that begins with local tool installation and ends with verifiable connectivity policies. The initial setup of Terraform and Azure CLI creates the execution environment where az login supplies credentials to the azurerm provider. Provider version constraints such as ~>3.0 and ~> 3.56.0, < 4.0 anchor the API surface that defines azurerm_virtual_network and azurerm_subnet resources.

Single VNet examples show an inline subnet definition with explicit name, resource_group_name, location, address_space, and address_prefix. Multi-VNet mesh examples introduce count, random_pet, and random_string to generate unique names and non-overlapping 10.${count.index}.0.0/16 address spaces with matching /24 subnets. Virtual Network Manager then applies a mesh topology across those VNets, and verification is performed with az network manager list-effective-connectivity-config.

ExpressRoute integration extends the same HCL pattern to private peering and gateway resources, making hybrid connectivity repeatable. Cleanup follows the same plan-apply discipline using terraform plan -destroy -out main.destroy.tfplan and terraform apply main.destroy.tfplan, ensuring that destruction is previewed before execution.

The dense interconnection between provider configuration, resource naming, address planning, and verification commands forms a reproducible system where network isolation, traffic control, and on-premises connectivity are all expressed as code and can be versioned, replicated, and audited over time.

Sources

  1. GeeksforGeeks Create VNet In Azure Using Terraform
  2. Microsoft Learn Azure Virtual Network Manager With Terraform
  3. OneUptime How To Create Azure Virtual Networks And Subnets In Terraform
  4. Microsoft Learn Quickstart Create ExpressRoute VNet Terraform

Related Posts