-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
200 lines (167 loc) · 6.75 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
/**
* Copyright 2020 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 = {}
}
### IPv4 block ###
resource "google_compute_global_forwarding_rule" "http" {
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"
}
resource "google_compute_global_forwarding_rule" "https" {
project = var.project
count = var.ssl ? 1 : 0
name = "${var.name}-https"
target = google_compute_target_https_proxy.default[0].self_link
ip_address = local.address
port_range = "443"
}
resource "google_compute_global_address" "default" {
count = var.create_address ? 1 : 0
project = var.project
name = "${var.name}-address"
ip_version = "IPV4"
}
### IPv4 block ###
### IPv6 block ###
resource "google_compute_global_forwarding_rule" "http_ipv6" {
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"
}
resource "google_compute_global_forwarding_rule" "https_ipv6" {
project = var.project
count = (var.enable_ipv6 && var.ssl) ? 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"
}
resource "google_compute_global_address" "default_ipv6" {
count = (var.enable_ipv6 && var.create_ipv6_address) ? 1 : 0
project = var.project
name = "${var.name}-ipv6-address"
ip_version = "IPV6"
}
### 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 ? 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, ), )
ssl_policy = var.ssl_policy
quic_override = var.quic ? "ENABLE" : null
}
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" {
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 = var.project
name = "${var.name}-backend-${each.key}"
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)
custom_request_headers = lookup(each.value, "custom_request_headers", [])
custom_response_headers = lookup(each.value, "custom_response_headers", [])
# 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")
}
}
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", "")
}
}
}