-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
422 lines (369 loc) · 18.4 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
locals {
address = var.create_address ? join("", google_compute_global_address.default.*.address) : var.address
ipv6_address = var.create_ipv6_address ? join("", google_compute_global_address.default_ipv6.*.address) : var.ipv6_address
url_map = var.create_url_map ? join("", google_compute_url_map.default.*.self_link) : var.url_map
create_http_forward = var.http_forward || var.https_redirect
health_checked_backends = { for backend_index, backend_value in var.backends : backend_index => backend_value if backend_value["health_check"] != null }
is_internal = var.load_balancing_scheme == "INTERNAL_SELF_MANAGED"
internal_network = local.is_internal ? var.network : null
}
### IPv4 block ###
resource "google_compute_global_forwarding_rule" "http" {
provider = google-beta
project = var.project
count = local.create_http_forward ? 1 : 0
name = var.name
target = google_compute_target_http_proxy.default[0].self_link
ip_address = local.address
port_range = "80"
labels = var.labels
load_balancing_scheme = var.load_balancing_scheme
network = local.internal_network
}
resource "google_compute_global_forwarding_rule" "https" {
provider = google-beta
project = var.project
count = var.ssl || var.certificate_map != null ? 1 : 0
name = "${var.name}-https"
target = google_compute_target_https_proxy.default[0].self_link
ip_address = local.address
port_range = "443"
labels = var.labels
load_balancing_scheme = var.load_balancing_scheme
network = local.internal_network
}
resource "google_compute_global_address" "default" {
provider = google-beta
count = local.is_internal ? 0 : var.create_address ? 1 : 0
project = var.project
name = "${var.name}-address"
ip_version = "IPV4"
labels = var.labels
}
### IPv4 block ###
### IPv6 block ###
resource "google_compute_global_forwarding_rule" "http_ipv6" {
provider = google-beta
project = var.project
count = (var.enable_ipv6 && local.create_http_forward) ? 1 : 0
name = "${var.name}-ipv6-http"
target = google_compute_target_http_proxy.default[0].self_link
ip_address = local.ipv6_address
port_range = "80"
labels = var.labels
load_balancing_scheme = var.load_balancing_scheme
network = local.internal_network
}
resource "google_compute_global_forwarding_rule" "https_ipv6" {
provider = google-beta
project = var.project
count = var.enable_ipv6 && (var.ssl || var.certificate_map != null) ? 1 : 0
name = "${var.name}-ipv6-https"
target = google_compute_target_https_proxy.default[0].self_link
ip_address = local.ipv6_address
port_range = "443"
labels = var.labels
load_balancing_scheme = var.load_balancing_scheme
network = local.internal_network
}
resource "google_compute_global_address" "default_ipv6" {
provider = google-beta
count = local.is_internal ? 0 : (var.enable_ipv6 && var.create_ipv6_address) ? 1 : 0
project = var.project
name = "${var.name}-ipv6-address"
ip_version = "IPV6"
labels = var.labels
}
### IPv6 block ###
# HTTP proxy when http forwarding is true
resource "google_compute_target_http_proxy" "default" {
project = var.project
count = local.create_http_forward ? 1 : 0
name = "${var.name}-http-proxy"
url_map = var.https_redirect == false ? local.url_map : join("", google_compute_url_map.https_redirect.*.self_link)
}
# HTTPS proxy when ssl is true
resource "google_compute_target_https_proxy" "default" {
project = var.project
count = var.ssl || var.certificate_map != null ? 1 : 0
name = "${var.name}-https-proxy"
url_map = local.url_map
ssl_certificates = compact(concat(var.ssl_certificates, google_compute_ssl_certificate.default.*.self_link, google_compute_managed_ssl_certificate.default.*.self_link, ), )
certificate_map = var.certificate_map != null ? "//certificatemanager.googleapis.com/${var.certificate_map}" : null
ssl_policy = var.ssl_policy
quic_override = var.quic == null ? "NONE" : var.quic ? "ENABLE" : "DISABLE"
}
resource "google_compute_ssl_certificate" "default" {
project = var.project
count = var.ssl && length(var.managed_ssl_certificate_domains) == 0 && !var.use_ssl_certificates ? 1 : 0
name_prefix = "${var.name}-certificate-"
private_key = var.private_key
certificate = var.certificate
lifecycle {
create_before_destroy = true
}
}
resource "random_id" "certificate" {
count = var.random_certificate_suffix == true ? 1 : 0
byte_length = 4
prefix = "${var.name}-cert-"
keepers = {
domains = join(",", var.managed_ssl_certificate_domains)
}
}
resource "google_compute_managed_ssl_certificate" "default" {
provider = google-beta
project = var.project
count = var.ssl && length(var.managed_ssl_certificate_domains) > 0 && !var.use_ssl_certificates ? 1 : 0
name = var.random_certificate_suffix == true ? random_id.certificate[0].hex : "${var.name}-cert"
lifecycle {
create_before_destroy = true
}
managed {
domains = var.managed_ssl_certificate_domains
}
}
resource "google_compute_url_map" "default" {
provider = google-beta
project = var.project
count = var.create_url_map ? 1 : 0
name = "${var.name}-url-map"
default_service = google_compute_backend_service.default[keys(var.backends)[0]].self_link
}
resource "google_compute_url_map" "https_redirect" {
project = var.project
count = var.https_redirect ? 1 : 0
name = "${var.name}-https-redirect"
default_url_redirect {
https_redirect = true
redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"
strip_query = false
}
}
resource "google_compute_backend_service" "default" {
provider = google-beta
for_each = var.backends
project = coalesce(each.value["project"], var.project)
name = "${var.name}-backend-${each.key}"
load_balancing_scheme = var.load_balancing_scheme
locality_lb_policy = lookup(each.value, "locality_lb_policy", null)
port_name = lookup(each.value, "port_name", "http")
protocol = lookup(each.value, "protocol", "HTTP")
timeout_sec = lookup(each.value, "timeout_sec", null)
description = lookup(each.value, "description", null)
connection_draining_timeout_sec = lookup(each.value, "connection_draining_timeout_sec", null)
enable_cdn = lookup(each.value, "enable_cdn", false)
compression_mode = lookup(each.value, "compression_mode", "DISABLED")
custom_request_headers = lookup(each.value, "custom_request_headers", [])
custom_response_headers = lookup(each.value, "custom_response_headers", [])
session_affinity = lookup(each.value, "session_affinity", null)
affinity_cookie_ttl_sec = lookup(each.value, "affinity_cookie_ttl_sec", null)
health_checks = lookup(each.value, "health_check", null) == null ? null : [google_compute_health_check.default[each.key].self_link]
# To achieve a null backend edge_security_policy, set each.value.edge_security_policy to "" (empty string), otherwise, it fallsback to var.edge_security_policy.
edge_security_policy = lookup(each.value, "edge_security_policy") == "" ? null : (lookup(each.value, "edge_security_policy") == null ? var.edge_security_policy : each.value.edge_security_policy)
# To achieve a null backend security_policy, set each.value.security_policy to "" (empty string), otherwise, it fallsback to var.security_policy.
security_policy = lookup(each.value, "security_policy") == "" ? null : (lookup(each.value, "security_policy") == null ? var.security_policy : each.value.security_policy)
# dynamic "backend" {
# for_each = toset(each.value["groups"])
# content {
# description = lookup(backend.value, "description", null)
# group = lookup(backend.value, "group")
#
# balancing_mode = lookup(backend.value, "balancing_mode")
# capacity_scaler = lookup(backend.value, "capacity_scaler")
# max_connections = lookup(backend.value, "max_connections")
# max_connections_per_instance = lookup(backend.value, "max_connections_per_instance")
# max_connections_per_endpoint = lookup(backend.value, "max_connections_per_endpoint")
# max_rate = lookup(backend.value, "max_rate")
# max_rate_per_instance = lookup(backend.value, "max_rate_per_instance")
# max_rate_per_endpoint = lookup(backend.value, "max_rate_per_endpoint")
# max_utilization = lookup(backend.value, "max_utilization")
# }
# }
dynamic "log_config" {
for_each = lookup(lookup(each.value, "log_config", {}), "enable", true) ? [1] : []
content {
enable = lookup(lookup(each.value, "log_config", {}), "enable", true)
sample_rate = lookup(lookup(each.value, "log_config", {}), "sample_rate", "1.0")
}
}
dynamic "iap" {
for_each = lookup(lookup(each.value, "iap_config", {}), "enable", false) ? [1] : []
content {
oauth2_client_id = lookup(lookup(each.value, "iap_config", {}), "oauth2_client_id", "")
oauth2_client_secret = lookup(lookup(each.value, "iap_config", {}), "oauth2_client_secret", "")
}
}
dynamic "cdn_policy" {
for_each = each.value.enable_cdn ? [1] : []
content {
cache_mode = each.value.cdn_policy.cache_mode
signed_url_cache_max_age_sec = each.value.cdn_policy.signed_url_cache_max_age_sec
default_ttl = each.value.cdn_policy.default_ttl
max_ttl = each.value.cdn_policy.max_ttl
client_ttl = each.value.cdn_policy.client_ttl
negative_caching = each.value.cdn_policy.negative_caching
serve_while_stale = each.value.cdn_policy.serve_while_stale
dynamic "negative_caching_policy" {
for_each = each.value.cdn_policy.negative_caching_policy != null ? [1] : []
content {
code = each.value.cdn_policy.negative_caching_policy.code
ttl = each.value.cdn_policy.negative_caching_policy.ttl
}
}
dynamic "cache_key_policy" {
for_each = each.value.cdn_policy.cache_key_policy != null ? [1] : []
content {
include_host = each.value.cdn_policy.cache_key_policy.include_host
include_protocol = each.value.cdn_policy.cache_key_policy.include_protocol
include_query_string = each.value.cdn_policy.cache_key_policy.include_query_string
query_string_blacklist = each.value.cdn_policy.cache_key_policy.query_string_blacklist
query_string_whitelist = each.value.cdn_policy.cache_key_policy.query_string_whitelist
include_http_headers = each.value.cdn_policy.cache_key_policy.include_http_headers
include_named_cookies = each.value.cdn_policy.cache_key_policy.include_named_cookies
}
}
}
}
lifecycle {
ignore_changes = [
backend,
]
}
depends_on = [
google_compute_health_check.default
]
}
resource "google_compute_health_check" "default" {
provider = google-beta
for_each = local.health_checked_backends
project = coalesce(each.value["project"], var.project)
name = "${var.name}-hc-${each.key}"
check_interval_sec = lookup(each.value["health_check"], "check_interval_sec", 5)
timeout_sec = lookup(each.value["health_check"], "timeout_sec", 5)
healthy_threshold = lookup(each.value["health_check"], "healthy_threshold", 2)
unhealthy_threshold = lookup(each.value["health_check"], "unhealthy_threshold", 2)
log_config {
enable = lookup(each.value["health_check"], "logging", false)
}
dynamic "http_health_check" {
for_each = coalesce(lookup(each.value["health_check"], "protocol", null), each.value["protocol"]) == "HTTP" ? [
{
host = lookup(each.value["health_check"], "host", null)
request_path = lookup(each.value["health_check"], "request_path", null)
response = lookup(each.value["health_check"], "response", null)
port = lookup(each.value["health_check"], "port", null)
port_name = lookup(each.value["health_check"], "port_name", null)
proxy_header = lookup(each.value["health_check"], "proxy_header", null)
port_specification = lookup(each.value["health_check"], "port_specification", null)
}
] : []
content {
host = lookup(http_health_check.value, "host", null)
request_path = lookup(http_health_check.value, "request_path", null)
response = lookup(http_health_check.value, "response", null)
port = lookup(http_health_check.value, "port", null)
port_name = lookup(http_health_check.value, "port_name", null)
proxy_header = lookup(http_health_check.value, "proxy_header", null)
port_specification = lookup(http_health_check.value, "port_specification", null)
}
}
dynamic "https_health_check" {
for_each = coalesce(lookup(each.value["health_check"], "protocol", null), each.value["protocol"]) == "HTTPS" ? [
{
host = lookup(each.value["health_check"], "host", null)
request_path = lookup(each.value["health_check"], "request_path", null)
response = lookup(each.value["health_check"], "response", null)
port = lookup(each.value["health_check"], "port", null)
port_name = lookup(each.value["health_check"], "port_name", null)
proxy_header = lookup(each.value["health_check"], "proxy_header", null)
port_specification = lookup(each.value["health_check"], "port_specification", null)
}
] : []
content {
host = lookup(https_health_check.value, "host", null)
request_path = lookup(https_health_check.value, "request_path", null)
response = lookup(https_health_check.value, "response", null)
port = lookup(https_health_check.value, "port", null)
port_name = lookup(https_health_check.value, "port_name", null)
proxy_header = lookup(https_health_check.value, "proxy_header", null)
port_specification = lookup(https_health_check.value, "port_specification", null)
}
}
dynamic "http2_health_check" {
for_each = coalesce(lookup(each.value["health_check"], "protocol", null), each.value["protocol"]) == "HTTP2" ? [
{
host = lookup(each.value["health_check"], "host", null)
request_path = lookup(each.value["health_check"], "request_path", null)
response = lookup(each.value["health_check"], "response", null)
port = lookup(each.value["health_check"], "port", null)
port_name = lookup(each.value["health_check"], "port_name", null)
proxy_header = lookup(each.value["health_check"], "proxy_header", null)
port_specification = lookup(each.value["health_check"], "port_specification", null)
}
] : []
content {
host = lookup(http2_health_check.value, "host", null)
request_path = lookup(http2_health_check.value, "request_path", null)
response = lookup(http2_health_check.value, "response", null)
port = lookup(http2_health_check.value, "port", null)
port_name = lookup(http2_health_check.value, "port_name", null)
proxy_header = lookup(http2_health_check.value, "proxy_header", null)
port_specification = lookup(http2_health_check.value, "port_specification", null)
}
}
dynamic "tcp_health_check" {
for_each = coalesce(lookup(each.value["health_check"], "protocol", null), each.value["protocol"]) == "TCP" ? [
{
request = lookup(each.value["health_check"], "request", null)
response = lookup(each.value["health_check"], "response", null)
port = lookup(each.value["health_check"], "port", null)
port_name = lookup(each.value["health_check"], "port_name", null)
proxy_header = lookup(each.value["health_check"], "proxy_header", null)
port_specification = lookup(each.value["health_check"], "port_specification", null)
}
] : []
content {
request = lookup(tcp_health_check.value, "request", null)
response = lookup(tcp_health_check.value, "response", null)
port = lookup(tcp_health_check.value, "port", null)
port_name = lookup(tcp_health_check.value, "port_name", null)
proxy_header = lookup(tcp_health_check.value, "proxy_header", null)
port_specification = lookup(tcp_health_check.value, "port_specification", null)
}
}
}
resource "google_compute_firewall" "default-hc" {
count = length(var.firewall_networks)
project = length(var.firewall_networks) == 1 && var.firewall_projects[0] == "default" ? var.project : var.firewall_projects[count.index]
name = "${var.name}-hc-${count.index}"
network = var.firewall_networks[count.index]
source_ranges = [
"130.211.0.0/22",
"35.191.0.0/16"
]
target_tags = length(var.target_tags) > 0 ? var.target_tags : null
target_service_accounts = length(var.target_service_accounts) > 0 ? var.target_service_accounts : null
dynamic "allow" {
for_each = local.health_checked_backends
content {
protocol = "tcp"
ports = [allow.value["health_check"].port]
}
}
}