My aim is to pass above NIC ids to the VM module and have 1:1 mapping between NIC id and VM ( test-nic-1 should only be attached to vm-1 , test-nic-2 to vm-2 etc.)
Error: each.value cannot be used in this context
on main.tf line 58, in module "vm":
58: nic_ids = module.nic[each.value.id].nic_ids
A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.
does your module "vm" create multiple VMs on it’s own or should that module actually get instantiated multiple times? It sounds like multiple NICs are created within the NIC module. You’d also have the option to create multiple instances of the module, though.
Your code doesn’t include any for/for_each of the module, hence each.xxx is invalid.
Based on that, wouldn’t you just pass the output of module.nics to vm?
Thanks @tbugfinder for looking into this.
Yes, my VM module also has a similar structure allowing me to create multiple VMs.
Here is my variables.tf file from the VM module:
What you mentioned below works, but that’s not exactly what I want
nic_ids = module.nic.nic_ids
It associates both the nic_ids to the same VM . . . I want first nic_id with VM-1 and second nic_id with VM-2.
Is there a way to achieve that?
Thanks in advance!
Thanks @tbugfinder for the suggestion.
I just wanted to be sure where I make these changes.
My VM module file is called VM.tf, and my parent module that calls the VM and NIC module is called Calling_modules.tf.
All the changes you mentioned above, will be in Calling_modules.tf ?
Thanks @tbugfinder for your continued support!
I see what you are suggesting with zipmap.
Using that, our local.nics_map will look something like this, creating a one to one relation with VMs and NIC ids:
However, the nic_ids in Calling_modules.tf refers to the network interfaces to be associated with the VM, which has to be a string, and that variable type cant be changed since its internal to Azure.
Blockquote
Error: Invalid function argument
on main.tf line 52, in module “vm”:
52: nic_ids = tostring(values(local.nics_map))
|----------------
| local.nics_map is object with 2 attributes
Invalid value for “v” parameter: cannot convert tuple to string.
I’m sorry then I don’t get your point. You shouldn’t have to mangle around with tosting(). Just use network_interface_ids = [var.nic_ids[each.key]].
each.key should map to VM1 and VM2.