-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn.tf
306 lines (240 loc) · 10.5 KB
/
cdn.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
resource "azurerm_cdn_frontdoor_profile" "cdn" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdn"
resource_group_name = local.resource_group.name
sku_name = local.cdn_frontdoor_sku
response_timeout_seconds = local.cdn_frontdoor_response_timeout
tags = local.tags
}
resource "azurerm_cdn_frontdoor_origin_group" "group" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}origingroup"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
load_balancing {}
dynamic "health_probe" {
for_each = local.enable_cdn_frontdoor_health_probe ? [0] : []
content {
protocol = local.cdn_frontdoor_health_probe_protocol
interval_in_seconds = local.cdn_frontdoor_health_probe_interval
request_type = local.cdn_frontdoor_health_probe_request_type
path = local.cdn_frontdoor_health_probe_path
}
}
}
resource "azurerm_cdn_frontdoor_origin" "origin" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}origin"
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.group[0].id
enabled = true
certificate_name_check_enabled = true
host_name = local.cdn_frontdoor_origin_fqdn_override
origin_host_header = local.cdn_frontdoor_origin_host_header_override
http_port = local.cdn_frontdoor_origin_http_port
https_port = local.cdn_frontdoor_origin_https_port
}
resource "azurerm_cdn_frontdoor_endpoint" "endpoint" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdnendpoint"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
tags = local.tags
}
resource "azurerm_cdn_frontdoor_custom_domain" "custom_domain" {
for_each = local.enable_cdn_frontdoor ? toset(local.cdn_frontdoor_custom_domains) : []
name = "${local.resource_prefix}custom-domain${index(local.cdn_frontdoor_custom_domains, each.value)}"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
dns_zone_id = local.enable_dns_zone && endswith(each.value, local.dns_zone_domain_name) ? azurerm_dns_zone.default[0].id : null
host_name = each.value
tls {
certificate_type = "ManagedCertificate"
minimum_tls_version = "TLS12"
}
}
resource "azurerm_cdn_frontdoor_route" "route" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}route"
cdn_frontdoor_endpoint_id = azurerm_cdn_frontdoor_endpoint.endpoint[0].id
cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.group[0].id
cdn_frontdoor_origin_ids = [azurerm_cdn_frontdoor_origin.origin[0].id]
cdn_frontdoor_rule_set_ids = local.ruleset_ids
enabled = true
forwarding_protocol = local.cdn_frontdoor_forwarding_protocol
https_redirect_enabled = true
patterns_to_match = ["/*"]
supported_protocols = ["Http", "Https"]
cdn_frontdoor_custom_domain_ids = [
for custom_domain in azurerm_cdn_frontdoor_custom_domain.custom_domain : custom_domain.id
]
link_to_default_domain = true
}
resource "azurerm_cdn_frontdoor_custom_domain_association" "custom_domain_association" {
for_each = local.enable_cdn_frontdoor ? [] : toset(local.cdn_frontdoor_custom_domains)
cdn_frontdoor_custom_domain_id = azurerm_cdn_frontdoor_custom_domain.custom_domain[each.value].id
cdn_frontdoor_route_ids = [azurerm_cdn_frontdoor_route.route[0].id]
}
resource "azurerm_cdn_frontdoor_rule_set" "redirects" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_host_redirects) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}redirects"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "redirect" {
for_each = local.enable_cdn_frontdoor ? { for index, host_redirect in local.cdn_frontdoor_host_redirects : index => { "from" : host_redirect.from, "to" : host_redirect.to } } : {}
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = "redirect${each.key}"
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.redirects[0].id
order = each.key
behavior_on_match = "Continue"
actions {
url_redirect_action {
redirect_type = "Moved"
redirect_protocol = "Https"
destination_hostname = each.value.to
}
}
conditions {
host_name_condition {
operator = "Equal"
negate_condition = false
match_values = [each.value.from]
transforms = ["Lowercase", "Trim"]
}
}
}
resource "azurerm_cdn_frontdoor_firewall_policy" "waf" {
count = local.cdn_frontdoor_enable_waf ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}waf"
resource_group_name = local.resource_group.name
sku_name = azurerm_cdn_frontdoor_profile.cdn[0].sku_name
enabled = true
mode = local.cdn_frontdoor_waf_mode
custom_block_response_status_code = 403
custom_block_response_body = filebase64("${path.module}/html/waf-response.html")
dynamic "custom_rule" {
for_each = local.cdn_frontdoor_enable_rate_limiting ? [0] : []
content {
name = "RateLimiting"
enabled = true
priority = 1
rate_limit_duration_in_minutes = local.cdn_frontdoor_rate_limiting_duration_in_minutes
rate_limit_threshold = local.cdn_frontdoor_rate_limiting_threshold
type = "RateLimitRule"
action = "Block"
dynamic "match_condition" {
for_each = length(local.cdn_frontdoor_rate_limiting_bypass_ip_list) > 0 ? [0] : []
content {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = true
match_values = local.cdn_frontdoor_rate_limiting_bypass_ip_list
}
}
match_condition {
match_variable = "RequestUri"
operator = "RegEx"
negation_condition = false
match_values = ["/.*"]
}
}
}
tags = local.tags
}
resource "azurerm_cdn_frontdoor_security_policy" "waf" {
count = local.cdn_frontdoor_enable_waf ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}wafsecuritypolicy"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
security_policies {
firewall {
cdn_frontdoor_firewall_policy_id = azurerm_cdn_frontdoor_firewall_policy.waf[0].id
association {
patterns_to_match = ["/*"]
domain {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_endpoint.endpoint[0].id
}
dynamic "domain" {
for_each = toset(local.cdn_frontdoor_custom_domains)
content {
cdn_frontdoor_domain_id = azurerm_cdn_frontdoor_custom_domain.custom_domain[domain.value].id
}
}
}
}
}
}
resource "azurerm_cdn_frontdoor_rule_set" "add_response_headers" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_host_add_response_headers) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}addresponseheaders"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "add_response_headers" {
for_each = local.enable_cdn_frontdoor ? { for index, response_header in local.cdn_frontdoor_host_add_response_headers : index => { "name" : response_header.name, "value" : response_header.value } } : {}
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = replace("addresponseheaders${each.key}", "-", "")
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.add_response_headers[0].id
order = each.key
behavior_on_match = "Continue"
actions {
response_header_action {
header_action = "Overwrite"
header_name = each.value.name
value = each.value.value
}
}
}
resource "azurerm_cdn_frontdoor_rule_set" "remove_response_headers" {
count = local.enable_cdn_frontdoor && length(local.cdn_frontdoor_remove_response_headers) > 0 ? 1 : 0
name = "${replace(local.resource_prefix, "-", "")}removeresponseheaders"
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.cdn[0].id
}
resource "azurerm_cdn_frontdoor_rule" "remove_response_header" {
for_each = local.enable_cdn_frontdoor ? toset(local.cdn_frontdoor_remove_response_headers) : []
depends_on = [azurerm_cdn_frontdoor_origin_group.group, azurerm_cdn_frontdoor_origin.origin]
name = replace("removeresponseheader${each.value}", "-", "")
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.remove_response_headers[0].id
order = index(local.cdn_frontdoor_remove_response_headers, each.value)
behavior_on_match = "Continue"
actions {
response_header_action {
header_action = "Delete"
header_name = each.value
}
}
}
resource "azurerm_monitor_diagnostic_setting" "cdn" {
count = local.enable_cdn_frontdoor ? 1 : 0
name = "${local.resource_prefix}cdn"
target_resource_id = azurerm_cdn_frontdoor_profile.cdn[0].id
log_analytics_workspace_id = azurerm_log_analytics_workspace.container_app.id
log_analytics_destination_type = "AzureDiagnostics"
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_waf_logs ? [1] : []
content {
category = "FrontdoorWebApplicationFirewallLog"
retention_policy {
enabled = false
}
}
}
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_access_logs ? [1] : []
content {
category = "FrontdoorAccessLog"
retention_policy {
enabled = false
}
}
}
dynamic "enabled_log" {
for_each = local.cdn_frontdoor_enable_health_probe_logs ? [1] : []
content {
category = "FrontdoorHealthProbeLog"
retention_policy {
enabled = false
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}