This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
148 lines (131 loc) · 6.66 KB
/
main.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#------------------------
# Local declarations
#------------------------
locals {
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
if_ddos_enabled = var.create_ddos_plan ? [{}] : []
}
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = var.resource_group_name
location = var.location
tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, )
}
#-------------------------------------
# VNET Creation - Default is "true"
#-------------------------------------
resource "azurerm_virtual_network" "vnet" {
name = var.vnetwork_name
location = local.location
resource_group_name = local.resource_group_name
address_space = var.vnet_address_space
dns_servers = var.dns_servers
tags = merge({ "Name" = format("%s", var.vnetwork_name) }, var.tags, )
dynamic "ddos_protection_plan" {
for_each = local.if_ddos_enabled
content {
id = azurerm_network_ddos_protection_plan.ddos[0].id
enable = true
}
}
}
#--------------------------------------------
# Ddos protection plan - Default is "false"
#--------------------------------------------
resource "azurerm_network_ddos_protection_plan" "ddos" {
count = var.create_ddos_plan ? 1 : 0
name = var.ddos_plan_name
resource_group_name = local.resource_group_name
location = local.location
tags = merge({ "Name" = format("%s", var.ddos_plan_name) }, var.tags, )
}
#-------------------------------------
# Network Watcher - Default is "true"
#-------------------------------------
resource "azurerm_resource_group" "nwatcher" {
count = var.create_network_watcher != false ? 1 : 0
name = "NetworkWatcherRG"
location = local.location
tags = merge({ "Name" = "NetworkWatcherRG" }, var.tags, )
}
resource "azurerm_network_watcher" "nwatcher" {
count = var.create_network_watcher != false ? 1 : 0
name = "NetworkWatcher_${local.location}"
location = local.location
resource_group_name = azurerm_resource_group.nwatcher.0.name
tags = merge({ "Name" = format("%s", "NetworkWatcher_${local.location}") }, var.tags, )
}
#--------------------------------------------------------------------------------------------------------
# Subnets Creation with, private link endpoint/servie network policies, service endpoints and Deligation.
#--------------------------------------------------------------------------------------------------------
resource "azurerm_subnet" "fw-snet" {
count = var.firewall_subnet_address_prefix != null ? 1 : 0
name = "AzureFirewallSubnet"
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.firewall_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 10, 0)]
service_endpoints = var.firewall_service_endpoints
}
resource "azurerm_subnet" "gw_snet" {
count = var.gateway_subnet_address_prefix != null ? 1 : 0
name = "GatewaySubnet"
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.gateway_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 8, 1)]
service_endpoints = ["Microsoft.Storage"]
}
resource "azurerm_subnet" "snet" {
for_each = var.subnets
name = each.value.subnet_name
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = each.value.subnet_address_prefix
service_endpoints = lookup(each.value, "service_endpoints", [])
enforce_private_link_endpoint_network_policies = lookup(each.value, "enforce_private_link_endpoint_network_policies", null)
enforce_private_link_service_network_policies = lookup(each.value, "enforce_private_link_service_network_policies", null)
dynamic "delegation" {
for_each = lookup(each.value, "delegation", {}) != {} ? [1] : []
content {
name = lookup(each.value.delegation, "name", null)
service_delegation {
name = lookup(each.value.delegation.service_delegation, "name", null)
actions = lookup(each.value.delegation.service_delegation, "actions", null)
}
}
}
}
#-----------------------------------------------
# Network security group - Default is "false"
#-----------------------------------------------
resource "azurerm_network_security_group" "nsg" {
for_each = var.subnets
name = lower("nsg_${each.key}_in")
resource_group_name = local.resource_group_name
location = local.location
tags = merge({ "ResourceName" = lower("nsg_${each.key}_in") }, var.tags, )
dynamic "security_rule" {
for_each = concat(lookup(each.value, "nsg_inbound_rules", []), lookup(each.value, "nsg_outbound_rules", []))
content {
name = security_rule.value[0] == "" ? "Default_Rule" : security_rule.value[0]
priority = security_rule.value[1]
direction = security_rule.value[2] == "" ? "Inbound" : security_rule.value[2]
access = security_rule.value[3] == "" ? "Allow" : security_rule.value[3]
protocol = security_rule.value[4] == "" ? "Tcp" : security_rule.value[4]
source_port_range = "*"
destination_port_range = security_rule.value[5] == "" ? "*" : security_rule.value[5]
source_address_prefix = security_rule.value[6] == "" ? element(each.value.subnet_address_prefix, 0) : security_rule.value[6]
destination_address_prefix = security_rule.value[7] == "" ? element(each.value.subnet_address_prefix, 0) : security_rule.value[7]
description = "${security_rule.value[2]}_Port_${security_rule.value[5]}"
}
}
}
resource "azurerm_subnet_network_security_group_association" "nsg-assoc" {
for_each = var.subnets
subnet_id = azurerm_subnet.snet[each.key].id
network_security_group_id = azurerm_network_security_group.nsg[each.key].id
}