Skip to content

Commit f1c6775

Browse files
authored
Merge pull request #38 from onetwopunch/support-shielded-vm
Support Shielded VMs in Instance Template
2 parents 5ac6fe8 + 9b1fada commit f1c6775

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

modules/instance_template/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
2020
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | string | `"false"` | no |
2121
| disk\_size\_gb | Boot disk size in GB | string | `"100"` | no |
2222
| disk\_type | Boot disk type, can be either pd-ssd, local-ssd, or pd-standard | string | `"pd-standard"` | no |
23+
| enable\_shielded\_vm | Whether to enable the Shielded VM configuration on the instance. Note that the instance image must support Shielded VMs. See https://cloud.google.com/compute/docs/images | string | `"false"` | no |
2324
| labels | Labels, provided as a map | map(string) | `<map>` | no |
2425
| machine\_type | Machine type to create, e.g. n1-standard-1 | string | `"n1-standard-1"` | no |
2526
| metadata | Metadata, provided as a map | map(string) | `<map>` | no |
@@ -28,9 +29,10 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
2829
| preemptible | Allow the instance to be preempted | bool | `"false"` | no |
2930
| project\_id | The GCP project ID | string | `"null"` | no |
3031
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account. | object | n/a | yes |
32+
| shielded\_instance\_config | Not used unless enable_shielded_vm is true. Shielded VM configuration for the instance. | object | `<map>` | no |
3133
| source\_image | Source disk image. If neither source_image nor source_image_family is specified, defaults to the latest public CentOS image. | string | `""` | no |
32-
| source\_image\_family | Source image family. If neither source_image nor source_image_family is specified, defaults to the latest public CentOS image. | string | `""` | no |
33-
| source\_image\_project | Project where the source image comes from | string | `""` | no |
34+
| source\_image\_family | Source image family. If neither source_image nor source_image_family is specified, defaults to the latest public CentOS image. | string | `"centos-7"` | no |
35+
| source\_image\_project | Project where the source image comes from. The default project contains images that support Shielded VMs if desired | string | `"gce-uefi-images"` | no |
3436
| startup\_script | User startup script to run when instances spin up | string | `""` | no |
3537
| subnetwork | The name of the subnetwork to attach this interface to. The subnetwork must exist in the same region this instance will be created in. Either network or subnetwork must be provided. | string | `""` | no |
3638
| subnetwork\_project | The ID of the project in which the subnetwork belongs. If it is not provided, the provider project is used. | string | `""` | no |

modules/instance_template/main.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ locals {
4343
]
4444

4545
all_disks = concat(local.boot_disk, var.additional_disks)
46+
47+
# NOTE: Even if all the shielded_instance_config values are false, if the
48+
# config block exists and an unsupported image is chosen, the apply will fail
49+
# so we use a single-value array with the default value to initialize the block
50+
# only if it is enabled.
51+
shielded_vm_configs = var.enable_shielded_vm ? [true] : []
4652
}
4753

4854
####################
@@ -80,6 +86,7 @@ resource "google_compute_instance_template" "tpl" {
8086
}
8187
}
8288
}
89+
8390
dynamic "service_account" {
8491
for_each = [var.service_account]
8592
content {
@@ -98,9 +105,18 @@ resource "google_compute_instance_template" "tpl" {
98105
create_before_destroy = "true"
99106
}
100107

101-
// scheduling must have automatic_restart be false when preemptible is true.
108+
# scheduling must have automatic_restart be false when preemptible is true.
102109
scheduling {
103110
preemptible = var.preemptible
104111
automatic_restart = ! var.preemptible
105112
}
113+
114+
dynamic "shielded_instance_config" {
115+
for_each = local.shielded_vm_configs
116+
content {
117+
enable_secure_boot = lookup(var.shielded_instance_config, "enable_secure_boot", shielded_instance_config.value)
118+
enable_vtpm = lookup(var.shielded_instance_config, "enable_vtpm", shielded_instance_config.value)
119+
enable_integrity_monitoring = lookup(var.shielded_instance_config, "enable_integrity_monitoring", shielded_instance_config.value)
120+
}
121+
}
106122
}

modules/instance_template/variables.tf

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ variable "source_image" {
6363

6464
variable "source_image_family" {
6565
description = "Source image family. If neither source_image nor source_image_family is specified, defaults to the latest public CentOS image."
66-
default = ""
66+
default = "centos-7"
6767
}
6868

6969
variable "source_image_project" {
70-
description = "Project where the source image comes from"
71-
default = ""
70+
description = "Project where the source image comes from. The default project contains images that support Shielded VMs if desired"
71+
default = "gce-uefi-images"
7272
}
7373

7474
variable "disk_size_gb" {
@@ -141,3 +141,26 @@ variable "service_account" {
141141
})
142142
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account."
143143
}
144+
145+
###########################
146+
# Shielded VMs
147+
###########################
148+
variable "enable_shielded_vm" {
149+
default = false
150+
description = "Whether to enable the Shielded VM configuration on the instance. Note that the instance image must support Shielded VMs. See https://cloud.google.com/compute/docs/images"
151+
}
152+
153+
variable "shielded_instance_config" {
154+
description = "Not used unless enable_shielded_vm is true. Shielded VM configuration for the instance."
155+
type = object({
156+
enable_secure_boot = bool
157+
enable_vtpm = bool
158+
enable_integrity_monitoring = bool
159+
})
160+
161+
default = {
162+
enable_secure_boot = true
163+
enable_vtpm = true
164+
enable_integrity_monitoring = true
165+
}
166+
}

0 commit comments

Comments
 (0)