-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathglb.tf
217 lines (182 loc) · 5.86 KB
/
glb.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
resource "google_compute_global_address" "glb_demo" {
name = "glb-demo"
}
resource "google_compute_global_forwarding_rule" "glb_demo_http" {
name = "glb-demo-http"
ip_address = google_compute_global_address.glb_demo.address
port_range = "80"
target = google_compute_target_http_proxy.glb_demo.self_link
load_balancing_scheme = "EXTERNAL"
}
resource "google_compute_target_http_proxy" "glb_demo" {
name = "glb-demo"
url_map = google_compute_url_map.glb_demo.self_link
}
resource "google_compute_global_forwarding_rule" "glb_demo_https" {
name = "glb-demo-https"
ip_address = google_compute_global_address.glb_demo.address
port_range = "443"
target = google_compute_target_https_proxy.glb_demo.self_link
load_balancing_scheme = "EXTERNAL"
}
resource "google_compute_ssl_certificate" "glb_demo" {
name_prefix = "glb-demo-"
private_key = file("example.key")
certificate = file("example.crt")
}
resource "google_compute_target_https_proxy" "glb_demo" {
name = "glb-demo"
ssl_certificates = [google_compute_ssl_certificate.glb_demo.self_link]
url_map = google_compute_url_map.glb_demo.self_link
}
resource "google_compute_url_map" "glb_demo" {
name = "glb-demo"
default_service = google_compute_backend_service.glb_demo_zone_printer.self_link
host_rule {
hosts = ["*"]
path_matcher = "glb-demo"
}
path_matcher {
name = "glb-demo"
default_service = google_compute_backend_service.glb_demo_zone_printer.self_link
path_rule {
paths = ["/hello-app"]
service = google_compute_backend_service.glb_demo_hello_app.self_link
}
}
}
# The max_rate for these backends is set to the minimum so that simply by
# aggressively refreshing the page traffic will be sent to different instances
# in different zones to demonstrate the load balancer in operation.
resource "google_compute_backend_service" "glb_demo_zone_printer" {
name = "glb-demo-zone-printer"
health_checks = [google_compute_health_check.glb_demo.self_link]
load_balancing_scheme = "EXTERNAL"
protocol = "HTTP"
port_name = "http"
security_policy = google_compute_security_policy.glb_demo.self_link
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_eu_1.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_eu_2.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_eu_3.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_us_1.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_us_2.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.zone_printer_neg_us_3.self_link
balancing_mode = "RATE"
max_rate = 1
}
}
resource "google_compute_backend_service" "glb_demo_hello_app" {
name = "glb-demo-hello-app"
health_checks = [google_compute_health_check.glb_demo.self_link]
load_balancing_scheme = "EXTERNAL"
protocol = "HTTP"
port_name = "http"
security_policy = google_compute_security_policy.glb_demo.self_link
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_eu_1.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_eu_2.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_eu_3.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_us_1.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_us_2.self_link
balancing_mode = "RATE"
max_rate = 1
}
backend {
group = data.google_compute_network_endpoint_group.hello_app_neg_us_3.self_link
balancing_mode = "RATE"
max_rate = 1
}
}
resource "google_compute_health_check" "glb_demo" {
name = "glb-demo"
healthy_threshold = 1
check_interval_sec = 60
unhealthy_threshold = 10
timeout_sec = 60
tcp_health_check {
port = "80"
}
}
# This is a firewall rule to allow incoming traffic on port 80
resource "google_compute_firewall" "glb_demo" {
name = "glb-demo"
network = "default"
direction = "INGRESS"
priority = 1000
allow {
protocol = "tcp"
ports = ["80"]
}
# These are the source ranges for Google's network for traffic coming in from
# the load balancer
source_ranges = [
"130.211.0.0/22",
"35.191.0.0/16",
]
}
# This is a Cloud Armor policy
resource "google_compute_security_policy" "glb_demo" {
name = "glb-demo"
# Default rule, allow all traffic
rule {
action = "allow"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "default rule"
}
# Deny traffic from some IPs
rule {
action = "deny(403)"
# Lower value means higher priority
priority = "1000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["9.9.9.0/24"]
}
}
description = "Deny access to IPs in 9.9.9.0/24"
}
}