I recently started using terraform . I am practicing on azure right now. I have created 2 virtual networks with 2 subnets (public and private) in each network. I have network security group with inbound allow traffic on ports 22 and 80. I am trying to assign this same nsg to both public subnets in 2 networks. Please guide me how can i achieve this. Below is the code -
this code will create virtual network-
++++++++++++++++++++++++++++++++++++++
resource “azurerm_virtual_network” “virt_net” {
count = length(var.VNET)
resource_group_name = azurerm_resource_group.rg.name
name = var.VNET[count.index].vnet_name
location = azurerm_resource_group.rg.location
address_space = var.VNET[count.index].address_space
dynamic “subnet”{
for_each = var.VNET[count.index].subnets
content{
name = subnet.value.name
address_prefix = subnet.value.address
}
}
}
+++++++++++++++++++++++++++++++++++++
this is for nsg-
+++++++++++++++++++++++++++++++++++++++++
resource “azurerm_network_security_group” “nsg” {
name = var.NSG_NAME
resource_group_name= azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
count = length(var.PORTS)
dynamic “security_rule”{
for_each = var.PORTS
content{
name = “port-${var.PORTS[count.index].port}”
direction = “Inbound”
access = “Allow”
protocol = “Tcp”
priority = var.PORTS[count.index].priority
source_port_range = var.PORTS[count.index].port
}
}
++++++++++++++++++++++++++++++++++++++++++++
this is what i am trying to assign same nsg to both subnets-
+++++++++++++++++++++++++++++++++++++
resource “azurerm_subnet_network_security_group_association” “nsg_subnet” {
dynamic “subnet_id”{
for_each = var.VNET.subnets.name[0]
content{
subnet_id = azurerm_virtual_network.virt_net[count.index].subnet[0].id
network_security_group_id = azurerm_network_security_group.nsg.id
}
}
}
++++++++++++++++++++++++++++++