/
nsg.tf
27 lines (25 loc) · 1.26 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
#Create NSG for the 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 = data.azurerm_resource_group.vn_rg.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" {
network_interface_id = azurerm_network_interface.nic.id
network_security_group_id = azurerm_network_security_group.nsg.id
}