-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
362 lines (295 loc) · 10.8 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
# --------------------------------------------------------------------------------------------
# Provider configuration
# --------------------------------------------------------------------------------------------
terraform {}
provider "google" {
project = local.project_id
region = local.region
}
data "google_compute_zones" "main" {}
# --------------------------------------------------------------------------------------------
# Local variables
# --------------------------------------------------------------------------------------------
locals {
project_id = var.project_id
region = var.region
vmseries_image = var.vmseries_image
public_key_path = var.public_key_path
mgmt_allow_ips = var.mgmt_allow_ips
prefix = var.prefix
subnet_cidr_mgmt = var.subnet_cidr_mgmt
subnet_cidr_untrust = var.subnet_cidr_untrust
subnet_cidr_untrust_lb = var.subnet_cidr_untrust_lb
subnet_cidr_trust = var.subnet_cidr_trust
subnet_cidr_web = var.subnet_cidr_web
subnet_cidr_external = var.subnet_cidr_external
create_test_vms = var.create_test_vms
}
# --------------------------------------------------------------------------------------------
# Create VPC networks
# --------------------------------------------------------------------------------------------
resource "google_compute_network" "mgmt" {
name = "${local.prefix}mgmt-vpc"
routing_mode = "GLOBAL"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
}
resource "google_compute_network" "untrust" {
name = "${local.prefix}untrust-vpc"
routing_mode = "GLOBAL"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
}
resource "google_compute_network" "trust" {
name = "${local.prefix}trust-vpc"
routing_mode = "GLOBAL"
auto_create_subnetworks = false
enable_ula_internal_ipv6 = true
delete_default_routes_on_create = true
}
# --------------------------------------------------------------------------------------------
# Create subnets
# --------------------------------------------------------------------------------------------
resource "google_compute_subnetwork" "mgmt" {
name = "${local.prefix}mgmt-subnet"
ip_cidr_range = local.subnet_cidr_mgmt
region = local.region
stack_type = "IPV4_IPV6"
ipv6_access_type = "EXTERNAL"
network = google_compute_network.mgmt.id
}
resource "google_compute_subnetwork" "untrust" {
name = "${local.prefix}untrust-subnet"
ip_cidr_range = local.subnet_cidr_untrust
region = local.region
stack_type = "IPV4_IPV6"
ipv6_access_type = "EXTERNAL"
network = google_compute_network.untrust.id
}
resource "google_compute_subnetwork" "untrust_lb" {
name = "${local.prefix}untrust-lb-subnet"
ip_cidr_range = local.subnet_cidr_untrust_lb
region = local.region
stack_type = "IPV4_IPV6"
ipv6_access_type = "EXTERNAL"
network = google_compute_network.untrust.id
}
resource "google_compute_subnetwork" "trust" {
name = "${local.prefix}trust-subnet"
ip_cidr_range = local.subnet_cidr_trust
region = local.region
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
network = google_compute_network.trust.id
}
resource "google_compute_subnetwork" "web" {
name = "${local.prefix}web-subnet"
ip_cidr_range = local.subnet_cidr_web
region = local.region
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
network = google_compute_network.trust.id
}
# --------------------------------------------------------------------------------------------
# Create ingress firewall rules
# --------------------------------------------------------------------------------------------
resource "google_compute_firewall" "mgmt_ipv4" {
name = "${local.prefix}all-ingress-mgmt"
network = google_compute_network.mgmt.id
source_ranges = var.mgmt_allow_ips
allow {
protocol = "tcp"
ports = ["443", "22"]
}
}
resource "google_compute_firewall" "untrust_ipv4" {
name = "${local.prefix}all-ingress-untrust"
network = google_compute_network.untrust.id
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "all"
}
}
resource "google_compute_firewall" "untrust_ipv6" {
name = "${local.prefix}all-ingress-untrust-ipv6"
network = google_compute_network.untrust.id
source_ranges = ["::/0"]
direction = "INGRESS"
allow {
protocol = "all"
}
}
resource "google_compute_firewall" "trust_ipv4" {
name = "${local.prefix}all-ingress-trust"
network = google_compute_network.trust.id
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "all"
}
}
resource "google_compute_firewall" "trust_ipv6" {
name = "${local.prefix}all-ingress-trust-ipv6"
network = google_compute_network.trust.id
source_ranges = ["::/0"]
direction = "INGRESS"
allow {
protocol = "all"
}
}
# --------------------------------------------------------------------------------------------
# Create VM-Series
# --------------------------------------------------------------------------------------------
# Service account for bootstrapping
module "iam_service_account" {
source = "github.com/PaloAltoNetworks/terraform-google-vmseries-modules//modules/iam_service_account?ref=main"
service_account_id = "${local.prefix}vmseries-mig-sa"
project_id = local.project_id
}
# # Create the bootstrap storage bucket.
# module "bootstrap" {
# source = "PaloAltoNetworks/vmseries-modules/google//modules/bootstrap"
# service_account = module.iam_service_account.email
# location = "US"
# files = {
# "bootstrap_files/init-cfg.txt" = "config/init-cfg.txt"
# "bootstrap_files/bootstrap.xml" = "config/bootstrap.xml"
# "bootstrap_files/authcodes" = "license/authcodes"
# }
# }
# Create VM-Series
resource "google_compute_instance" "vmseries" {
name = "${local.prefix}vmseries"
zone = data.google_compute_zones.main.names[0]
machine_type = "n2-standard-4"
can_ip_forward = true
allow_stopping_for_update = true
metadata = {
mgmt-interface-swap = "enable"
# vmseries-bootstrap-gce-storagebucket = module.bootstrap.bucket_name
serial-port-enable = true
ssh-keys = "admin:${file(local.public_key_path)}"
}
network_interface {
subnetwork = google_compute_subnetwork.untrust.id
stack_type = "IPV4_IPV6"
access_config {
network_tier = "PREMIUM"
}
ipv6_access_config {
network_tier = "PREMIUM"
}
}
network_interface {
subnetwork = google_compute_subnetwork.mgmt.id
stack_type = "IPV4_IPV6"
access_config {
network_tier = "PREMIUM"
}
ipv6_access_config {
network_tier = "PREMIUM"
}
}
network_interface {
subnetwork = google_compute_subnetwork.trust.id
stack_type = "IPV4_IPV6"
}
boot_disk {
initialize_params {
image = local.vmseries_image
type = "pd-standard"
}
}
service_account {
email = module.iam_service_account.email
scopes = [
"https://www.googleapis.com/auth/compute.readonly",
"https://www.googleapis.com/auth/cloud.useraccounts.readonly",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write"
]
}
depends_on = [
# module.bootstrap,
module.iam_service_account
]
}
# Create instance group
resource "google_compute_instance_group" "vmseries" {
name = "${local.prefix}vmseries"
zone = data.google_compute_zones.main.names[0]
instances = [
google_compute_instance.vmseries.id
]
}
# --------------------------------------------------------------------------------------------
# Create an external load balancer to distribute traffic to VM-Series trust interfaces.
# --------------------------------------------------------------------------------------------
resource "google_compute_region_health_check" "extlb" {
name = "${local.prefix}vmseries-extlb-hc"
project = var.project_id
region = var.region
check_interval_sec = 3
healthy_threshold = 1
timeout_sec = 1
unhealthy_threshold = 1
http_health_check {
port = 80
request_path = "/php/login.php"
}
}
resource "google_compute_address" "extlb_ipv4" {
name = "${local.prefix}vmseries-extlb-pip-ipv4"
region = local.region
network_tier = "PREMIUM"
address_type = "EXTERNAL"
}
resource "google_compute_address" "extlb_ipv6" {
name = "${local.prefix}vmseries-extlb-pip-ipv6"
region = local.region
network_tier = "PREMIUM"
address_type = "EXTERNAL"
ip_version = "IPV6"
ipv6_endpoint_type = "NETLB"
subnetwork = google_compute_subnetwork.untrust_lb.id
}
resource "google_compute_forwarding_rule" "extlb_ipv4" {
name = "${local.prefix}vmseries-extlb-rule-ipv4"
project = var.project_id
region = var.region
network_tier = "PREMIUM"
load_balancing_scheme = "EXTERNAL"
ip_protocol = "L3_DEFAULT"
all_ports = true
backend_service = google_compute_region_backend_service.extlb.self_link
ip_address = google_compute_address.extlb_ipv4.address
}
resource "google_compute_forwarding_rule" "extlb_ipv6" {
name = "${local.prefix}vmseries-extlb-rule-ipv6"
project = var.project_id
region = var.region
network_tier = "PREMIUM"
load_balancing_scheme = "EXTERNAL"
ip_protocol = "L3_DEFAULT"
all_ports = true
backend_service = google_compute_region_backend_service.extlb.self_link
ip_address = google_compute_address.extlb_ipv6.id
ip_version = "IPV6"
subnetwork = google_compute_subnetwork.untrust_lb.id
}
resource "google_compute_region_backend_service" "extlb" {
provider = google-beta
name = "${local.prefix}vmseries-extlb"
project = var.project_id
region = var.region
load_balancing_scheme = "EXTERNAL"
health_checks = [google_compute_region_health_check.extlb.self_link]
protocol = "UNSPECIFIED"
connection_tracking_policy {
tracking_mode = "PER_SESSION"
connection_persistence_on_unhealthy_backends = "NEVER_PERSIST"
}
backend {
group = google_compute_instance_group.vmseries.self_link
}
}