{"id":3619,"date":"2026-01-14T15:29:49","date_gmt":"2026-01-14T21:29:49","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3619"},"modified":"2026-01-14T15:29:49","modified_gmt":"2026-01-14T21:29:49","slug":"how-to-create-an-azure-vnet-in-azure-using-terraform","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3619","title":{"rendered":"How to create an Azure VNet in azure using terraform"},"content":{"rendered":"\n<p>In azure vnets allow you to separate out parts of your cloud infrastructure into different network segments. This structure is almost the equivalent of building or setting up a new office location except the cloud has made it a lot faster. Although creating vnet\u2019s in azure can be completed really easy, automating this deployment can make sure that there is a consistent deployment setup across networks.<\/p>\n\n\n\n<p>To create a vnet in azure using terraform, we will be building the following resources in terraform:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An Azure resource group<\/li>\n\n\n\n<li>Then a virtual ntework<\/li>\n\n\n\n<li>Allocate a CIDR range for the virtual network<\/li>\n\n\n\n<li>A default subnet<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Building your resource group in terraform<\/h2>\n\n\n\n<p>The resource group is what holds all the resources that share a common lifecycle together. This makes it easier to view everything in a logical unit. Create a new folder in the directory of your choice and create a file called spoke-network.tf. Inside the file place the following code in their.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    azurerm = {\n      source  = \"hashicorp\/azurerm\"\n      version = \"=3.0.0\"\n    }\n  }\n}\n\n# Configure the Microsoft Azure Provider\nprovider \"azurerm\" {\n  features {}\n}\n\n#build azure resource group\nresource \"azurerm_resource_group\" \"main\" {\n  name = \"mainnetwork\"\n  location = \"eastus\"\n  \n}<\/code><\/pre>\n\n\n\n<p>Here we declared our required providers for this code. The resource group is being deployed in the \u201ceastus\u201d region and called \u201cmainnetwork\u201d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a vnet in azure<\/h2>\n\n\n\n<p>The vnet is dependent on the resource group being available. Now lets add the resource block for the virtual network below our resource group.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Resource \"azurerm_virtual_network\" \"mainnetwork\" {\n  name = \"mainnetwork\"\n  location = azurerm_resource_group.main.location\n  resource_group_name = azurerm_resource_group.main.name\n  address_space = &#91;\"10.0.0.0\/16\"]\n}<\/code><\/pre>\n\n\n\n<p>This deploys a virtual network called \u201cmainnetwork\u201d inside the \u201cmainnetwork\u201d resource group with an address space of 10.0.0.0\/8. This will then allow you to deploy multiple subnets in that range. There are additional options that can be declared in the virtual network resource block such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ddos_protection_plan<\/li>\n\n\n\n<li>dns_servers<\/li>\n\n\n\n<li>edge_zone<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Building your subnets in separate terraform blocks<\/h2>\n\n\n\n<p>We could add our subnets to the current azurerm_virtual_network block, but separating them out makes it easier when needing to reference them for other things if the terraform project grew.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Resource \"azurerm_subnet\" \"devsubnet\" {\n  name = \"dev-subnet\"\n  resource_group_name = azurerm_resource_group.main.name\n  address_prefixes = &#91;\"10.0.1.0\/24\"]\n  virtual_network_name = azurerm_virtual_network.mainnetwork.name\n}\n\nResource \"azurerm_subnet\" \"testsubnet\" {\n  name = \"test-subnet\"\n  resource_group_name = azurerm_resource_group.main.name\n  address_prefixes = &#91;\"10.0.2.0\/24\"]\n  virtual_network_name = azurerm_virtual_network.mainnetwork.name\n}<\/code><\/pre>\n\n\n\n<p>To create the completion of the vnet in azure using terraform we built 2 subnets. The dev and test subnet will both be linked to the \u201cmainnetwork\u201d virtual network. When finished we will have a simple vnet with 2 subnets pictured below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/letmetechyou.com\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-23-at-5.36.37-PM-1024x635.png\" alt=\"azure vnet deployed using terraform\" class=\"wp-image-1813\"\/><figcaption class=\"wp-element-caption\">azure vnet deployed using terraform<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Once finished the final deployment of terraform code should not take too long. The great thing is this template will be a great starting point to continue with building out an entire azure network from scratch and peering along with other virtual networks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    azurerm = {\n      source  = \"hashicorp\/azurerm\"\n      version = \"=3.0.0\"\n    }\n  }\n}\n\n# Configure the Microsoft Azure Provider\nprovider \"azurerm\" {\n  features {}\n}\n\n#build azure resource group\nresource \"azurerm_resource_group\" \"main\" {\n  name = \"mainnetwork\"\n  location = \"eastus\"\n  \n}\n\nResource \"azurerm_virtual_network\" \"mainnetwork\" {\n  name = \"mainnetwork\"\n  location = azurerm_resource_group.main.location\n  resource_group_name = azurerm_resource_group.main.name\n  address_space = &#91;\"10.0.0.0\/16\"]\n}\n\n#build devsubnet and link to mainnetwork virtual network\nResource \"azurerm_subnet\" \"devsubnet\" {\n  name = \"dev-subnet\"\n  resource_group_name = azurerm_resource_group.main.name\n  address_prefixes = &#91;\"10.0.1.0\/24\"]\n  virtual_network_name = azurerm_virtual_network.mainnetwork.name\n}\n\n#build testsubnet and link to mainnetwork virtual network\nResource \"azurerm_subnet\" \"testsubnet\" {\n  name = \"test-subnet\"\n  resource_group_name = azurerm_resource_group.main.name\n  address_prefixes = &#91;\"10.0.2.0\/24\"]\n  virtual_network_name = azurerm_virtual_network.mainnetwork.name\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In azure vnets allow you to separate out parts of your cloud infrastructure into different network segments. This structure is almost the equivalent of building or setting up a new office location except the cloud has made it a lot faster. Although creating vnet\u2019s in azure can be completed really easy, automating this deployment can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,35,77,85,84],"tags":[],"class_list":["post-3619","post","type-post","status-publish","format-standard","hentry","category-azure","category-cloud-computing","category-devops","category-iac","category-terraform"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3619"}],"version-history":[{"count":1,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3619\/revisions"}],"predecessor-version":[{"id":3620,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3619\/revisions\/3620"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}