-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
256 lines (223 loc) · 7.6 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
/*
* Copyright 2017 Google Inc.
*
* 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.
*/
terraform {
required_version = "~> 0.14.10"
required_providers {
google-beta = {
source = "hashicorp/google-beta"
version = "~> 3.64.0"
}
}
}
###############
# Data Sources
###############
data "google_compute_zones" "available" {
project = var.project
region = var.region
}
#########
# Locals
#########
locals {
healthchecks = concat(
google_compute_health_check.mig-health-check.*.self_link,
)
boot_disk = [
{
source_image = var.source_image
disk_size_gb = var.disk_size_gb
disk_type = var.disk_type
auto_delete = var.auto_delete
boot = "true"
},
]
all_disks = concat(local.boot_disk, var.additional_disks)
# NOTE: Even if all the shielded_instance_config values are false, if the
# config block exists and an unsupported image is chosen, the apply will fail
# so we use a single-value array with the default value to initialize the block
# only if it is enabled.
shielded_vm_configs = var.enable_shielded_vm ? [true] : []
}
###################
# Instance Template
###################
resource "google_compute_instance_template" "default" {
name_prefix = "default-"
machine_type = var.machine_type
labels = var.instance_labels
can_ip_forward = var.can_ip_forward
tags = concat(["allow-ssh"], var.target_tags)
project = var.project
region = var.region
network_interface {
network = var.network
subnetwork = var.subnetwork
dynamic "access_config" {
for_each = var.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
network_ip = var.network_ip
subnetwork_project = var.subnetwork_project == "" ? var.project : var.subnetwork_project
}
metadata = {
tf_depends_id = ""
startup-script = var.startup_script
}
dynamic "disk" {
for_each = local.all_disks
content {
auto_delete = lookup(disk.value, "auto_delete", null)
boot = lookup(disk.value, "boot", null)
device_name = lookup(disk.value, "device_name", null)
disk_name = lookup(disk.value, "disk_name", null)
disk_size_gb = lookup(disk.value, "disk_size_gb", null)
disk_type = lookup(disk.value, "disk_type", null)
interface = lookup(disk.value, "interface", null)
mode = lookup(disk.value, "mode", null)
source = lookup(disk.value, "source", null)
source_image = lookup(disk.value, "source_image", null)
type = lookup(disk.value, "boot_type", null)
dynamic "disk_encryption_key" {
for_each = lookup(disk.value, "disk_encryption_key", [])
content {
kms_key_self_link = lookup(disk_encryption_key.value, "kms_key_self_link", null)
}
}
}
}
dynamic "service_account" {
for_each = [var.service_account]
content {
email = lookup(service_account.value, "email", null)
scopes = lookup(service_account.value, "scopes", null)
}
}
lifecycle {
create_before_destroy = "true"
}
# scheduling must have automatic_restart be false when preemptible is true.
scheduling {
preemptible = var.preemptible
automatic_restart = !var.preemptible
on_host_maintenance = "MIGRATE"
}
dynamic "shielded_instance_config" {
for_each = local.shielded_vm_configs
content {
enable_secure_boot = lookup(var.shielded_instance_config, "enable_secure_boot", shielded_instance_config.value)
enable_vtpm = lookup(var.shielded_instance_config, "enable_vtpm", shielded_instance_config.value)
enable_integrity_monitoring = lookup(var.shielded_instance_config, "enable_integrity_monitoring", shielded_instance_config.value)
}
}
}
########################
# Instance Group Manager
########################
resource "google_compute_instance_group_manager" "default" {
provider = google-beta
name = var.name
description = "compute VM Instance Group"
zone = var.zone
project = var.project
wait_for_instances = var.wait_for_instances
base_instance_name = var.name
dynamic "named_port" {
for_each = var.named_ports
content {
name = lookup(named_port.value, "name", null)
port = lookup(named_port.value, "port", null)
}
}
version {
name = "${var.name}-default"
instance_template = google_compute_instance_template.default.self_link
}
target_pools = var.target_pools
target_size = var.autoscaling_enabled ? null : var.target_size
dynamic "auto_healing_policies" {
for_each = local.healthchecks
content {
health_check = auto_healing_policies.value
initial_delay_sec = var.health_check["initial_delay_sec"]
}
}
dynamic "update_policy" {
for_each = var.update_policy
content {
type = update_policy.value.type
minimal_action = update_policy.value.minimal_action
max_surge_fixed = lookup(update_policy.value, "max_surge_fixed", null)
max_surge_percent = lookup(update_policy.value, "max_surge_percent", null)
max_unavailable_fixed = lookup(update_policy.value, "max_unavailable_fixed", null)
max_unavailable_percent = lookup(update_policy.value, "max_unavailable_percent", null)
min_ready_sec = lookup(update_policy.value, "min_ready_sec", null)
}
}
}
resource "null_resource" "dummy_dependency" {
count = var.zonal ? 1 : 0
depends_on = [google_compute_instance_group_manager.default]
triggers = {
instance_template = element(google_compute_instance_template.default.*.self_link, 0)
}
}
###################
# Health Check
###################
resource "google_compute_health_check" "mig-health-check" {
count = var.http_health_check ? 1 : 0
name = var.name
project = var.project
check_interval_sec = var.hc_interval
timeout_sec = var.hc_timeout
healthy_threshold = var.hc_healthy_threshold
unhealthy_threshold = var.hc_unhealthy_threshold
http_health_check {
port = var.hc_port == "" ? var.service_port : var.hc_port
request_path = var.hc_path
}
}
#################
# Firewall Rules
#################
resource "google_compute_firewall" "mig-health-check" {
count = var.http_health_check ? 1 : 0
project = var.subnetwork_project == "" ? var.project : var.subnetwork_project
name = "${var.name}-vm-hc"
network = var.network
target_tags = var.target_tags
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
allow {
protocol = "tcp"
ports = [var.hc_port == "" ? var.service_port : var.hc_port]
}
}
resource "google_compute_firewall" "default-ssh" {
count = var.ssh_fw_rule ? 1 : 0
project = var.subnetwork_project == "" ? var.project : var.subnetwork_project
name = "${var.name}-vm-ssh"
network = var.network
target_tags = ["allow-ssh"]
source_ranges = var.ssh_source_ranges
allow {
protocol = "tcp"
ports = ["22"]
}
}