Terraform v0.14.3 : Using count in modules

I am using Terraform version v0.14.3. I am using count in modules to create multiple Azure resources (network interface card, VM) of the same type. Below is the parent module, calling child modules NIC and VM :

module "NIC" {
  source = "./NIC"
  count  = 2

  nic_name      =  "vm-nic-${count.index + 1}" 
  nic_location  = "eastus2"
  rg_name       = "abc-test-rg"
  ipconfig_name = "vm-nic-ipconfig-${count.index + 1}" 
  subnet_id     = "/subscriptions/***********/resourceGroups/abc-test-rg/providers/Microsoft.Network/virtualNetworks/abc-test-vnet/subnets/abc-test-vnet"
output "nic_id" {
  value = module.NIC[*].nic_id
module "VM" {
  source = "./VM"
  count = 2

  vm_name        = "test-vm"
  rg_name        = "abc-test-rg"
  location       = "eastus2"
  admin_password = var.admin_password
  nic_id         = [module.NIC[*].nic_id]

I am getting below error during terraform plan :

Error: Incorrect attribute value type

  on VM\main.tf line 8, in resource "azurerm_linux_virtual_machine" "vm":
   8:   network_interface_ids           = var.nic_id
    | var.nic_id is tuple with 1 element

Inappropriate value for attribute "network_interface_ids": element 0: string

How do I loop around the two NIC ids generated and pass them to the two VMs in the VM module? Thanks in advance!

Hi @JiJo333,
here all NIC IDs are passed to both VM module deployments.
a) How was var.nic_id defined in VM module?
b) you might also want to use nic_id = module.NIC[count.index].nic_id.

Hi @tbugfinder, I made the changes you mentioned, and it working now :slight_smile: :grinning:

I had tried similar approach earlier, the difference was I put it brackets, possibly converting it in a list.

nic_id =[module.NIC[count.index].nic_id]

Below worked:

nic_id = module.NIC[count.index].nic_id

Thanks again!

Hi @JiJo333 I am also on the same boat. But still could not figure out to resolve issue.

Could you please share your VM module main.tf file as well?

Error which I am getting is

Error: Invalid value for module argument
│   on main.tf line 90, in module "winvm":
│   90:   nic_id                      =  module.nic[count.index].nic_id
│ The given value is not suitable for child module variable "nic_id" defined at .terraform\modules\winvm\variables.tf:16,1-18: string required