forked from terraform-google-modules/terraform-google-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
146 lines (131 loc) · 5.29 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
/**
* Copyright 2019 Google LLC
*
* 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.
*/
#########
# Locals
#########
locals {
source_image = var.source_image != "" ? var.source_image : "centos-7-v20201112"
source_image_family = var.source_image_family != "" ? var.source_image_family : "centos-7"
source_image_project = var.source_image_project != "" ? var.source_image_project : "centos-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] : []
confidential_instance_config = var.enable_confidential_vm ? [true] : []
gpu_enabled = var.gpu != null
on_host_maintenance = (
var.preemptible || var.enable_confidential_vm || local.gpu_enabled
? "TERMINATE"
: var.on_host_maintenance
)
}
####################
# Instance Template
####################
resource "google_compute_instance_template" "tpl" {
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
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)
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]
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
dynamic "access_config" {
for_each = var.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
}
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 = local.on_host_maintenance
}
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
}
dynamic "guest_accelerator" {
for_each = local.gpu_enabled ? [var.gpu] : []
content {
type = guest_accelerator.value.type
count = guest_accelerator.value.count
}
}
}