how to create two vnets and some subnets in each one of those VNETs using terraform

Viewed 487

how to create two VNETs and some subnets in each one of those VNETs dynamically using terraform?

Currently, I'm creating four separate resources 2 each for VNETs and subnets.

I wanna use just one resource block for VNET and one resource block for the subnet in the tf code and have terraform pick for say VNET "test" two subnets (test1-subnet, test2-subnet) and second VNET say "dev" two separate subnets (dev1-subnet, dev2-subnet).

How to achieve this scenario using terraform for azure provider?

resource "azurerm_virtual_network" "example" {
  name                = "rest-network"
  address_space       = ["10.0.0.0/16"]
  location            = "eastus"
  resource_group_name  = "test-rg"
}

resource "azurerm_virtual_network" "example1" {
  name                = "test-network"
  address_space       = ["10.1.0.0/16"]
  location            = "eastus"
  resource_group_name  = "test-rg"
}

resource "azurerm_subnet" "example" {
  name                 = "rest-subnet"  
  resource_group_name  = "test-rg"
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefix       = "10.0.1.0/24"

  enforce_private_link_endpoint_network_policies = true
}

resource "azurerm_subnet" "example1" {
  name                 = "test-subnet"  
  resource_group_name  = "test-rg"
  virtual_network_name = azurerm_virtual_network.example1.name
  address_prefix       = "10.1.1.0/24"

  enforce_private_link_endpoint_network_policies = true
}

1 Answers

You have to parametrize your setup. One way, could be as follows (example which you may need to modify to suit your specific need):

variable "vnets" {
    default = {
        test = {
          address_space = ["10.0.0.0/16"]
          resource_group_name  = "test-rg"          
          subnets = [{
              address_prefix       = "10.0.1.0/24"
          },
          {
              address_prefix       = "10.0.2.0/24"
          }             
          ]
        },
        dev = {
          address_space = ["10.1.0.0/16"]
          resource_group_name  = "dev-rg"          
          subnets = [{
              address_prefix       = "10.1.1.0/24"
          },
          {
              address_prefix       = "10.1.2.0/24"
          }             
          ]
        }
    }
}


locals {
    # flatten the var.vnets variable
    vnet_subnet_map = merge([
           for vnet_name, vnet_details in var.vnets:
            {
                for idx, subnet in vnet_details.subnets: 
                    "${vnet_name}-${idx}" => {
                        vnet_name = vnet_name
                        subnet_name = "${vnet_name}${idx}-subnet"
                        address_space = vnet_details.address_space
                        resource_group_name = vnet_details.resource_group_name
                        subnet_address_prefix = subnet.address_prefix
                    }
            }
        ]...) # do NOT remove the dots
}

Then you create the two vnets and their subnets:

resource "azurerm_virtual_network" "vnet" {

  for_each            = var.vnets

  name                = "${each.key}-network"
  address_space       = each.value.address_space
  location            = "eastus"
  resource_group_name = each.value.resource_group_name
}



resource "azurerm_subnet" "subnet" {
  
  for_each             = local.vnet_subnet_map

  name                 = each.value.subnet_name
  resource_group_name  = each.value.resource_group_name
  virtual_network_name = azurerm_virtual_network.vnet[each.value.vnet_name].name
  address_prefix       = each.value.subnet_address_prefix

  enforce_private_link_endpoint_network_policies = true
}
Related