I have a nic module and vm module. When i give nic count as 2 and try to access index 0 and 1 of that nic module from vm module it doesn’t work. It doesn’t throw error on plan and apply but it attched only one nic where as two nics were created.
Terraform Version
Terraform v0.12.7
Terraform Configuration Files
# Virtual Machine module main.tf
resource "azurerm_virtual_machine" "vm" {
count = var.vm_instances_count
name = "${var.vm_name}${format(var.count_format, var.count_offset + count.index + 1)}"
location = var.location
availability_set_id = var.avset_id
primary_network_interface_id = var.multi_nic == "true" ? var.primary_nic_id : null
resource_group_name = var.resource_group_name
network_interface_ids = [element(var.nic, count.index)]
vm_size = var.vm_size
}
# Virtual Machine module vars.tf
variable "nic" {
default = [""]
description = "Network Interface ID"
}
# environments/dev/main.tf (calling nic module)
module "fw_nic" {
source = "../../nic"
vm_name = var.fw_vm_name
location = var.location
resource_group_name = module.resource_group.name
vm_instances_count = var.fw_vm_nic_count
vnet_subnet_id = module.fw_subnet.subnet_id[0]
nsg_id = module.fw_nsg.nsg_id[0]
lb_enable = "no"
enable_ip_forwarding = "true"
}
# environments/dev/vars.tf
variable "fw_vm_nic_count" {
description = "Firewall VM nic count"
}
# environments/dev/dev.tfvars
fw_vm_nic_count = "2"
# environments/dev/main.tf (calling vm module)
module "fw_vm" {
source = "../../virtual_machines"
vm_name = var.fw_vm_name
location = var.location
resource_group_name = module.resource_group.name
vm_instances_count = var.fw_vm_instances_count
vm_size = var.fw_vm_size
admin_username = var.fw_vm_admin_username
admin_password = var.fw_vm_admin_password
nic = [module.fw_nic.nic_id]
primary_nic_id = module.fw_nic.nic_id[0]
avset_id = module.fw_avset.id
disk_size_gb = var.fw_vm_disk_size_gb
os_disk_type = var.fw_vm_os_disk_type
data_disk_type = var.fw_vm_data_disk_type
image_publisher = var.fw_vm_image_publisher
image_offer = var.fw_vm_image_offer
image_sku = var.fw_vm_image_sku
image_version = var.fw_vm_image_version
data_disk_enable = "true
}
```
esource "azurerm_network_interface" "nic" {
count = var.vm_instances_count
name = "${var.vm_name}-nic${format(var.count_format, var.count_offset + count.index + 1)}"
location = var.location
resource_group_name = var.resource_group_name
network_security_group_id = var.nsg_id
enable_ip_forwarding = var.enable_ip_forwarding
ip_configuration {
name = "${var.vm_name}${format(var.count_format, var.count_offset + count.index + 1)}-ipconfig"
subnet_id = var.vnet_subnet_id
private_ip_address_allocation = var.private_ip_address_allocation
load_balancer_backend_address_pools_ids = var.lb_enable == "yes" ? [var.load_balancer_backend_address_pools_ids] : null
}
}
# NIC Output
output "nic_id" {
value = azurerm_network_interface.nic[*].id
}
```