Home > Blockchain >  Terraform Output multiple subnet ids
Terraform Output multiple subnet ids

Time:02-15

I'm pretty new to terraform and I'm trying to learn and write a TF code to automate Azure VM deployment. I'm trying to cover each parts as modules (except rg) rather than keeping it in a single main.tf file.

I'm creating a single Vnet with 3 subnets inside. Please find my code for the subnet module. Please help me with below two points.

Subnet.tf

resource "azurerm_subnet" "SUBNETS" {
for_each=var.Subnetlist
name=each.value.name
address_prefixes=[each.value.address]
  resource_group_name  = var.resource_group_name
  virtual_network_name = var.virtual_network_name
}

NIC.tf

resource "azurerm_network_interface" "NETWORKINTERFACE" {
  for_each=var.niclist
  name                = each.value.name
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = 
    private_ip_address_allocation = "Dynamic"
  }
}
  1. How can I output the subnet_ids (of three created subnets).
  2. How can I pass these subnet_ids to another module (say for creating Network interface)

CodePudding user response:

  1. Since your azurerm_subnet.SUBNETS is a map due to the use of for_each:
output "subnet_ids" {
   value = values(azurerm_subnet.SUBNETS)[*].id
}
  1. You pass it as any other variable:
module "myothermodule" { 
  source = "./modulepath"
  subnets_ids = module.mysubnetmodule.subnet_ids
}
  • Related