-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
279 lines (249 loc) · 10.1 KB
/
main.tf
File metadata and controls
279 lines (249 loc) · 10.1 KB
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
locals {
hostname = var.hostname == "" ? "default" : var.hostname
num_instances = length(var.static_ips) == 0 ? var.num_instances : length(var.static_ips)
# local.static_ips is the same as var.static_ips with a dummy element appended
# at the end of the list to work around "list does not have any elements so cannot
# determine type" error when var.static_ips is empty
static_ips = concat(var.static_ips, ["NOT_AN_IP"])
project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null
# When no network or subnetwork has been defined, we want to use the settings from
# the template instead.
network_interface = length(format("%s%s", var.network, var.subnetwork)) == 0 ? [] : [1]
}
###############
# Data Sources
###############
data "google_compute_zones" "available" {
project = local.project_id
region = var.region
}
#############
# Instances
#############
resource "google_compute_instance_from_template" "compute_instance" {
provider = google
count = local.num_instances
name = local.hostname //var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
project = local.project_id
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
deletion_protection = var.deletion_protection
resource_policies = var.resource_policies
labels = var.labels
desired_status = var.desired_status
params {
resource_manager_tags = var.resource_manager_tags
}
dynamic "network_interface" {
for_each = local.network_interface
content {
network = var.network
subnetwork = var.subnetwork
subnetwork_project = var.subnetwork_project
network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)
dynamic "access_config" {
for_each = var.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
dynamic "ipv6_access_config" {
for_each = var.ipv6_access_config
content {
network_tier = ipv6_access_config.value.network_tier
}
}
dynamic "alias_ip_range" {
for_each = var.alias_ip_ranges
content {
ip_cidr_range = alias_ip_range.value.ip_cidr_range
subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
}
}
}
}
source_instance_template = var.instance_template
}
#########
# Locals
#########
locals {
source_image = "${var.source_image}" //!= "" //? var.source_image : "debian-11-bullseye-v20240515"
source_image_family = "${var.source_image_family}" //!= "" ? var.source_image_family : "debian-11"
source_image_project = "${var.source_image_project}" //!= "" ? var.source_image_project : "debian-cloud"
boot_disk = [
{
source_image = var.source_image != "" ? format("${local.source_image_project}/${local.source_image}") : format("${local.source_image_project}/${local.source_image_family}")
disk_size_gb = var.disk_size_gb
disk_type = var.disk_type
disk_labels = var.disk_labels
auto_delete = var.auto_delete
boot = "true"
},
]
all_disks = concat(local.boot_disk, var.additional_disks)
# NOTE: Even if all the shielded_instance_config or confidential_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] : []
gpu_enabled = var.gpu != null
alias_ip_range_enabled = var.alias_ip_range != null
on_host_maintenance = (
var.preemptible || var.enable_confidential_vm || local.gpu_enabled || var.spot
? "TERMINATE"
: var.on_host_maintenance
)
automatic_restart = (
# must be false when preemptible or spot is true
var.preemptible || var.spot ? false : var.automatic_restart
)
preemptible = (
# must be true when preemtible or spot is true
var.preemptible || var.spot ? true : false
)
}
####################
# Instance Template
####################
resource "google_compute_instance_template" "tpl" {
provider = google-beta
name_prefix = "${var.name_prefix}"
project = var.project_id
machine_type = var.machine_type
labels = var.labels
metadata = var.metadata
tags = var.tags
can_ip_forward = var.can_ip_forward
metadata_startup_script = var.startup_script
region = var.region
min_cpu_platform = var.min_cpu_platform
resource_policies = var.resource_policies
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", lookup(disk.value, "disk_type", null) == "local-ssd" ? "375" : null)
disk_type = lookup(disk.value, "disk_type", null)
interface = lookup(disk.value, "interface", lookup(disk.value, "disk_type", null) == "local-ssd" ? "NVME" : null)
mode = lookup(disk.value, "mode", null)
source = lookup(disk.value, "source", null)
source_image = lookup(disk.value, "source_image", null)
source_snapshot = lookup(disk.value, "source_snapshot", null)
type = lookup(disk.value, "disk_type", null) == "local-ssd" ? "SCRATCH" : "PERSISTENT"
labels = lookup(disk.value, "disk_labels", null)
dynamic "disk_encryption_key" {
for_each = compact([var.disk_encryption_key == null ? null : 1])
content {
kms_key_self_link = var.disk_encryption_key
}
}
}
}
dynamic "service_account" {
for_each = var.service_account == null ? [] : [var.service_account]
content {
email = lookup(service_account.value, "email", null)
scopes = lookup(service_account.value, "scopes", null)
}
}
network_interface {
network = var.network
subnetwork = var.subnetwork
subnetwork_project = var.subnetwork_project
network_ip = length(var.network_ip) > 0 ? var.network_ip : null
nic_type = var.nic_type
stack_type = var.stack_type
dynamic "access_config" {
for_each = var.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
dynamic "ipv6_access_config" {
for_each = var.ipv6_access_config
content {
network_tier = ipv6_access_config.value.network_tier
}
}
dynamic "alias_ip_range" {
for_each = local.alias_ip_range_enabled ? [var.alias_ip_range] : []
content {
ip_cidr_range = alias_ip_range.value.ip_cidr_range
subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
}
}
}
dynamic "network_interface" {
for_each = var.additional_networks
content {
network = network_interface.value.network
subnetwork = network_interface.value.subnetwork
subnetwork_project = network_interface.value.subnetwork_project
network_ip = length(network_interface.value.network_ip) > 0 ? network_interface.value.network_ip : null
nic_type = network_interface.value.nic_type
stack_type = network_interface.value.stack_type
queue_count = network_interface.value.queue_count
dynamic "access_config" {
for_each = network_interface.value.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
dynamic "ipv6_access_config" {
for_each = network_interface.value.ipv6_access_config
content {
network_tier = ipv6_access_config.value.network_tier
}
}
dynamic "alias_ip_range" {
for_each = network_interface.value.alias_ip_range
content {
ip_cidr_range = alias_ip_range.value.ip_cidr_range
subnetwork_range_name = alias_ip_range.value.subnetwork_range_name
}
}
}
}
lifecycle {
create_before_destroy = "true"
}
scheduling {
automatic_restart = local.automatic_restart
instance_termination_action = var.spot ? var.spot_instance_termination_action : null
maintenance_interval = var.maintenance_interval
on_host_maintenance = local.on_host_maintenance
preemptible = local.preemptible
provisioning_model = var.spot ? "SPOT" : null
}
advanced_machine_features {
enable_nested_virtualization = var.enable_nested_virtualization
threads_per_core = var.threads_per_core
}
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)
}
}
confidential_instance_config {
enable_confidential_compute = var.enable_confidential_vm
}
network_performance_config {
total_egress_bandwidth_tier = var.total_egress_bandwidth_tier
}
dynamic "guest_accelerator" {
for_each = local.gpu_enabled ? [var.gpu] : []
content {
type = guest_accelerator.value.type
count = guest_accelerator.value.count
}
}
}