Azurerm - same nsg different subnet

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
}
}
}

++++++++++++++++++++++++++++++

I was able to solve the issue. Now i am able to add subnets in nsg. I used data blocks to get vnet and another data block to get public/private subnet. Then i associated them to public/private nsg respectively.

I am trying to create public IP in all subnet(2 private and 2 public). I need help in this. If anyone could advise me it would be very helpful