简体   繁体   中英

Referencing properties with for_each in modules and count in the resource

Here is my shortened code:

variable.tf :

variable "vm_avd" {
  type = map(any)
  default = {
    avd-std = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
    avd-gpu = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
  }
}

variable "vm_server" {
  type = map(any)
  default = {
    srv-std = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
  }
}

if.tf :

resource "azurerm_network_interface" "if" {
  count               = var.vm_count
  name                = var.private_ip_address_allocation == "Static" ? "if_${var.if_name}_${replace(var.private_ip_address, ".", "")}_${format("%02d", count.index)}" : "if_${var.if_name}_${format("%02d", count.index)}"
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "if_ipconfig"
    subnet_id                     = var.subnet_id
    private_ip_address_version    = var.private_ip_address_version
    private_ip_address_allocation = var.private_ip_address_allocation
    private_ip_address            = var.private_ip_address
  }
}
output "if_name" {
  value = azurerm_network_interface.if[*].name
}
output "if_id" {
  value = azurerm_network_interface.if[*].id
}

main.tf :

module "vm" {
  for_each              = merge(var.vm_server, var.vm_avd)
  vm_name               = each.key
  vm_count              = each.value["vm_count"]
  network_interface_ids = module.if[each.key].if_id
}

module "if" {
  for_each  = merge(var.vm_server, var.vm_avd)
  vm_count  = each.value["vm_count"]
  if_name   = each.key
  subnet_id = module.snet[each.value["subnet_id"]].snet_id
}

Is there a possibility to reference in a for_each loop the correct count object for dependent resources?

I'm creating Terraform modules for Azure and I want to build a structure that is highly flexible and easy to fill for everyone.

Therefore I build a root file with a single module call for each resource and I have a single main variables file which should be filled with values which are looped through.

All related resource variables are defined in the same variables object, so for a VM a variable VM is created and filled with VM details, NIC details and extension details. The single modules run through the same variables for VM, NIC and extensions which is, in general, running fine.

I can reference the su.net_id for the.network interfaces nicely but as soon as I use a count operator in the resource I don't know how the correctly get the.network interface ID of the correct interface for the correct VM.

Now I ran into the problem that I use for_each in my module call and I want to have the option to use count in my resource definition too. It is working but when I build eg virtual machines, I get the problem that I cannot reference to the correct.network interfaces. Atm all my interfaces are connected to the same VM and the 2nd VM cannot be build. Same goes for extensions and any possible multiple objects tho.

I tried several module calls but the first VM got all the interfaces every time. I tried with the Star character in the NIC module call or with the.network interface ID. If this is not possible at all, I thought of a solution with building the.network interface ID itself with its original form.

I also checked if there is a possibility to build an array with the for_each and count elements but I couldn't find any way to build arrays out of number variables in terraform, like the normal "for 1 to 5 do". If you know of any way to do this, I would be grateful too.

Maybe it is a typo when you try to simplify the question, but in your variables you have:

variable vm_avd { default = { avd-std = {...}} }
variable vm_server { default = { avd-std = {...}} }

in your main tf you have:

merge(var.vm_server, var.vm_avd)

since the key is the same, one will override the other, if you are using the default value, it might result in the behaviour you are suggesting, consider changing the key name to server-std?

I got the solution with simply providing the whole element array, eg from VM, to the variable for the ID and the reference the correct VM with the count index:

virtual_machine_id = var.vm_id[count.index]

As i run through the same Count in other ressources, I get the correct order of the elements for referencing:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM