unble to attach Azure network security group with NIC with count value using terraform

835 Views Asked by At

I Have written below code to attach security group with network interface using terraform.

resource "azurerm_network_interface_security_group_association" "attach_Nic_Nsg" {
     count                     = 2
     network_interface_id      = "${azurerm_network_interface.network_interface[count.index].id}"
     network_security_group_id = module.security_group.security_group_id
}

When i am executing terraform plan i am getting below error.

enter image description here

How to attach network security group for multiple Nic?

2

There are 2 best solutions below

1
On

resource "azurerm_network_interface_security_group_association" "attach_Nic_Nsg" { network_interface_id = azurerm_network_interface.testnic.id network_security_group_id = azurerm_network_security_group.testnsg.id }

0
On

Below Snippet solved my issue.

resource "azurerm_network_interface_security_group_association" "attach_Nic_Nsg" {
    count                     = 2
    network_interface_id      = element(azurerm_network_interface.network_interface.*.id, count.index)
    network_security_group_id = module.security_group.security_group_id
}