-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathc6-04-app-subnet-and-nsg.tf
More file actions
49 lines (44 loc) · 1.88 KB
/
c6-04-app-subnet-and-nsg.tf
File metadata and controls
49 lines (44 loc) · 1.88 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Resource-1: Create AppTier Subnet
resource "azurerm_subnet" "appsubnet" {
name = "${azurerm_virtual_network.vnet.name}-${var.app_subnet_name}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.app_subnet_address
}
# Resource-2: Create Network Security Group (NSG)
resource "azurerm_network_security_group" "app_subnet_nsg" {
name = "${azurerm_subnet.appsubnet.name}-nsg"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
# Resource-3: Associate NSG and Subnet
resource "azurerm_subnet_network_security_group_association" "app_subnet_nsg_associate" {
depends_on = [azurerm_network_security_rule.app_nsg_rule_inbound]
subnet_id = azurerm_subnet.appsubnet.id
network_security_group_id = azurerm_network_security_group.app_subnet_nsg.id
}
# Resource-4: Create NSG Rules
## Locals Block for Security Rules
locals {
app_inbound_ports_map = {
"100" : "80", # If the key starts with a number, you must use the colon syntax ":" instead of "="
"110" : "443",
"120" : "8080",
"130" : "22"
}
}
## NSG Inbound Rule for AppTier Subnets
resource "azurerm_network_security_rule" "app_nsg_rule_inbound" {
for_each = local.app_inbound_ports_map
name = "Rule-Port-${each.value}"
priority = each.key
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = each.value
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.app_subnet_nsg.name
}