-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsg.tf
28 lines (26 loc) · 1.3 KB
/
nsg.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#Create NSG for each VM
resource "azurerm_network_security_group" "nsg" {
name = format("%s%s%s%s%s%s%s", var.env_prefix, "-", var.vm_prefix, "-", "nic", "-", "nsg")
resource_group_name = azurerm_resource_group.rg.name
location = var.location
}
resource "azurerm_network_security_rule" "rules" {
for_each = local.rules
name = each.value.name
direction = each.value.direction
access = each.value.access
priority = each.value.priority
protocol = each.value.protocol
source_port_range = each.value.source_port_range
destination_port_range = each.value.destination_port_range
source_address_prefixes = each.value.source_address_prefixes
destination_address_prefix = each.value.destination_address_prefix
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
#Assign NSG to a VMs
resource "azurerm_network_interface_security_group_association" "nic_nsg" {
count = var.pip == "Yes" ? 1 : 1
network_interface_id = azurerm_network_interface.nic[count.index].id
network_security_group_id = azurerm_network_security_group.nsg.id
}