Home > Back-end >  subnet ID from an inline virtual network resource
subnet ID from an inline virtual network resource

Time:10-01

I am creating a virtual network using Terraform and the subnets are created within the virtual network resource block (instead of using its own resource block).

I am trying to associate some route tables to the subnets, however I'm not sure how to obtain the subnet resource ID when the subnets are created within virtual network, could someone help?

main.tf

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.24.0"
    }
  }
}

provider "azurerm" {
    skip_provider_registration = true
    features {}
}

###########################
# RESOURCE GROUP CREATION #
###########################
resource "azurerm_resource_group" "rg" {
    name = var.rg.name
    location = var.rg.location
}

############################
# VIRTUAL NETWORK CREATION #
############################
resource "azurerm_virtual_network" "vnet" {
    name = var.vnet.name
    resource_group_name = azurerm_resource_group.rg.name
    location = azurerm_resource_group.rg.location
    address_space = var.vnet.address_space
    dns_servers = var.vnet.dns_servers

    subnet {
        name = var.vnet.subnet1_name
        address_prefix = var.vnet.subnet1_address_prefix
    }

    subnet {
        name = var.vnet.subnet2_name
        address_prefix = var.vnet.subnet2_address_prefix
    }

    subnet {
        name = var.vnet.subnet3_name
        address_prefix = var.vnet.subnet3_address_prefix
    }

    subnet {
        name = var.vnet.subnet4_name
        address_prefix = var.vnet.subnet4_address_prefix
    }

    subnet {
        name = var.vnet.subnet5_name
        address_prefix = var.vnet.subnet5_address_prefix
    }
}

########################
# ROUTE TABLE CREATION #
########################
resource "azurerm_route_table" "fslogix_rt" {
  name = var.rt.fslogix.name
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  disable_bgp_route_propagation = false
}

resource "azurerm_route_table" "back_office_rt" {
  name = var.rt.back_office.name
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  disable_bgp_route_propagation = false
}

resource "azurerm_route_table" "dev_dev_rt" {
  name = var.rt.dev_dev.name
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  disable_bgp_route_propagation = false
}

resource "azurerm_route_table" "dev_prod_rt" {
  name = var.rt.dev_prod.name
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  disable_bgp_route_propagation = false
}

resource "azurerm_route_table" "front_office_rt" {
  name = var.rt.front_office.name
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  disable_bgp_route_propagation = false
}

##################
# ROUTE CREATION #
##################
resource "azurerm_route" "fslogix_routes" {
  for_each = var.fslogix_routes
  name = each.value.name
  resource_group_name = azurerm_resource_group.rg.name
  route_table_name = azurerm_route_table.fslogix_rt.name
  address_prefix = each.value.address_prefix
  next_hop_type = each.value.next_hop_type
  next_hop_in_ip_address = each.value.next_hop_in_ip_address
}

resource "azurerm_route" "back_office_routes" {
  for_each = var.back_office_routes
  name = each.value.name
  resource_group_name = azurerm_resource_group.rg.name
  route_table_name = azurerm_route_table.back_office_rt.name
  address_prefix = each.value.address_prefix
  next_hop_type = each.value.next_hop_type
  next_hop_in_ip_address = each.value.next_hop_in_ip_address
}

resource "azurerm_route" "dev_dev_routes" {
  for_each = var.developer_dev_routes
  name = each.value.name
  resource_group_name = azurerm_resource_group.rg.name
  route_table_name = azurerm_route_table.dev_dev_rt.name
  address_prefix = each.value.address_prefix
  next_hop_type = each.value.next_hop_type
  next_hop_in_ip_address = each.value.next_hop_in_ip_address
}

resource "azurerm_route" "dev_prod_routes" {
  for_each = var.developer_prod_routes
  name = each.value.name
  resource_group_name = azurerm_resource_group.rg.name
  route_table_name = azurerm_route_table.dev_prod_rt.name
  address_prefix = each.value.address_prefix
  next_hop_type = each.value.next_hop_type
  next_hop_in_ip_address = each.value.next_hop_in_ip_address
}

resource "azurerm_route" "front_office_routes" {
  for_each = var.front_office_routes
  name = each.value.name
  resource_group_name = azurerm_resource_group.rg.name
  route_table_name = azurerm_route_table.front_office_rt.name
  address_prefix = each.value.address_prefix
  next_hop_type = each.value.next_hop_type
  next_hop_in_ip_address = each.value.next_hop_in_ip_address
}

###########################
# ROUTE TABLE ASSOICATION #
###########################
resource "azurerm_subnet_route_table_association" "fslogix_rta" {
  subnet_id = ???????????????????????????????????????
  route_table_id = azurerm_route_table.fslogix_rt.id
}

CodePudding user response:

You can get all the subnet_id with:

azurerm_virtual_network.vnet.subnet.*.id

You can get the first subnet_id with:

azurerm_virtual_network.vnet.subnet.*.id[0]

And the second subnet_id with:

azurerm_virtual_network.vnet.subnet.*.id[1]

And go on...

Hope this helps!

  • Related