-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal-alb.tf
158 lines (136 loc) · 4.81 KB
/
external-alb.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
locals {
external_dns_domain = trimsuffix(data.google_dns_managed_zone.managed_zone_external.dns_name, ".")
}
//Create Health Check to ensure the service is healthy
resource "google_compute_health_check" "frontend_external" {
name = "frontend-external"
description = "frontend-external"
timeout_sec = 10
check_interval_sec = 60
healthy_threshold = 4
unhealthy_threshold = 5
http_health_check {
port_specification = "USE_SERVING_PORT"
request_path = "/_healthz"
}
}
//Create our Blue Backend Service for Blue
resource "google_compute_backend_service" "blue_external" {
project = var.project_id
name = "blue-external"
load_balancing_scheme = "EXTERNAL_MANAGED"
health_checks = [google_compute_health_check.frontend_external.id]
lifecycle {
ignore_changes = [backend]
}
}
//Create our Blue Backend Service for Green
resource "google_compute_backend_service" "green_external" {
project = var.project_id
name = "green-external"
load_balancing_scheme = "EXTERNAL_MANAGED"
health_checks = [google_compute_health_check.frontend_external.id]
lifecycle {
ignore_changes = [backend]
}
}
// Global External LoadBalancers only support Classic SSL Certificates
// This means Wildcards are not supported
// This means you have to create a resource for each ingress point
resource "google_compute_managed_ssl_certificate" "external_ssl_cert" {
name = "external-ssl-cert"
managed {
domains = [local.external_dns_domain]
}
}
resource "google_compute_managed_ssl_certificate" "color_external" {
for_each = toset(["blue", "green"])
name = "${each.value}-external-cert"
managed {
domains = ["${each.value}.${local.external_dns_domain}"]
}
}
// Use google_compute_target_http_proxy if you don't require SSL
// This will form the frontend of our loadbalancer
resource "google_compute_target_https_proxy" "external_global_https_proxy" {
name = "external-global-target-proxy"
url_map = google_compute_url_map.external_global_http_url_map.id
ssl_certificates = [
google_compute_managed_ssl_certificate.external_ssl_cert.id,
google_compute_managed_ssl_certificate.color_external["blue"].id,
google_compute_managed_ssl_certificate.color_external["green"].id
]
}
// Create Google Compute URL Map
// This will be used on the LoadBalancer to determine routing
resource "google_compute_url_map" "external_global_http_url_map" {
name = "external"
description = "external"
default_service = google_compute_backend_service.blue_external.name
host_rule {
hosts = [local.external_dns_domain]
path_matcher = "blue"
}
host_rule {
hosts = ["blue-${local.external_dns_domain}"]
path_matcher = "blue"
}
host_rule {
hosts = ["green-${local.external_dns_domain}"]
path_matcher = "green"
}
path_matcher {
name = "blue"
default_service = google_compute_backend_service.blue_external.id
}
path_matcher {
name = "green"
default_service = google_compute_backend_service.blue_external.id
}
}
// Create Static IP Address
resource "google_compute_global_address" "ip_address_external" {
name = "external-address"
}
//Creating Forwarding Rule
// This will form the Google LoadBalancer its-self
resource "google_compute_global_forwarding_rule" "external_global_http_forwarding_rule" {
name = "external-lb"
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL_MANAGED"
ip_address = google_compute_global_address.ip_address_external.address
port_range = "443"
target = google_compute_target_https_proxy.external_global_https_proxy.id
}
//Create Firewall Rule to allow GCP Probes to Access our Health Checks
resource "google_compute_firewall" "external-lb-hc" {
name = "external-allow-lb-health-check"
network = module.vpc.network_name
target_tags = ["allow-health-checks"]
source_ranges = [
"130.211.0.0/22",
"35.191.0.0/16"
]
allow {
protocol = "tcp"
ports = ["80", "8080"]
}
}
#
# DNS Resources
#
data "google_dns_managed_zone" "managed_zone_external" {
name = var.dns_external_domain_name
}
resource "google_dns_record_set" "external" {
managed_zone = data.google_dns_managed_zone.managed_zone_external.name
name = data.google_dns_managed_zone.managed_zone_external.dns_name
rrdatas = [google_compute_global_address.ip_address_external.address]
type = "A"
}
resource "google_dns_record_set" "wildcard_external" {
managed_zone = data.google_dns_managed_zone.managed_zone_external.name
name = "*.${data.google_dns_managed_zone.managed_zone_external.dns_name}"
rrdatas = [google_compute_global_address.ip_address_external.address]
type = "A"
}