Skip to content

Commit a535e13

Browse files
authored
feat: Add support for guest_accelerator for the instance_template module (#160)
* Add GPU variable tp the inputs * Add description to the GPU variable * Revert README changes * Generate docs with make generate_docs * Fix issue with default guest_accelerator setting * Remove deprecated list() notation in favor of []
1 parent 49829e8 commit a535e13

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

modules/instance_template/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
2121
| disk\_type | Boot disk type, can be either pd-ssd, local-ssd, or pd-standard | `string` | `"pd-standard"` | no |
2222
| enable\_confidential\_vm | Whether to enable the Confidential VM configuration on the instance. Note that the instance image must support Confidential VMs. See https://cloud.google.com/compute/docs/images | `bool` | `false` | no |
2323
| 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 | `bool` | `false` | no |
24+
| gpu | GPU information. Type and count of GPU to attach to the instance template. See https://cloud.google.com/compute/docs/gpus more details | <pre>object({<br> type = string<br> count = number<br> })</pre> | `null` | no |
2425
| labels | Labels, provided as a map | `map(string)` | `{}` | no |
2526
| machine\_type | Machine type to create, e.g. n1-standard-1 | `string` | `"n1-standard-1"` | no |
2627
| metadata | Metadata, provided as a map | `map(string)` | `{}` | no |

modules/instance_template/main.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ locals {
5151
shielded_vm_configs = var.enable_shielded_vm ? [true] : []
5252
confidential_instance_config = var.enable_confidential_vm ? [true] : []
5353

54+
gpu_enabled = var.gpu != null
5455
on_host_maintenance = (
55-
var.preemptible || var.enable_confidential_vm
56+
var.preemptible || var.enable_confidential_vm || local.gpu_enabled
5657
? "TERMINATE"
5758
: var.on_host_maintenance
5859
)
@@ -141,4 +142,12 @@ resource "google_compute_instance_template" "tpl" {
141142
confidential_instance_config {
142143
enable_confidential_compute = var.enable_confidential_vm
143144
}
145+
146+
dynamic "guest_accelerator" {
147+
for_each = local.gpu_enabled ? [var.gpu] : []
148+
content {
149+
type = guest_accelerator.value.type
150+
count = guest_accelerator.value.count
151+
}
152+
}
144153
}

modules/instance_template/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,15 @@ variable "access_config" {
209209
}))
210210
default = []
211211
}
212+
213+
###########################
214+
# Guest Accelerator (GPU)
215+
###########################
216+
variable "gpu" {
217+
description = "GPU information. Type and count of GPU to attach to the instance template. See https://cloud.google.com/compute/docs/gpus more details"
218+
type = object({
219+
type = string
220+
count = number
221+
})
222+
default = null
223+
}

0 commit comments

Comments
 (0)