diff --git a/modules/compute-vm/README.md b/modules/compute-vm/README.md index f934066fb8..0942db0eec 100644 --- a/modules/compute-vm/README.md +++ b/modules/compute-vm/README.md @@ -850,10 +850,10 @@ module "sole-tenancy" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| -| [name](variables.tf#L235) | Instance name. | string | ✓ | | -| [network_interfaces](variables.tf#L247) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | -| [project_id](variables.tf#L293) | Project id. | string | ✓ | | -| [zone](variables.tf#L391) | Compute zone. | string | ✓ | | +| [name](variables.tf#L264) | Instance name. | string | ✓ | | +| [network_interfaces](variables.tf#L276) | Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed. | list(object({…})) | ✓ | | +| [project_id](variables.tf#L322) | Project id. | string | ✓ | | +| [zone](variables.tf#L420) | Compute zone. | string | ✓ | | | [attached_disk_defaults](variables.tf#L17) | Defaults for attached disks options. | object({…}) | | {…} | | [attached_disks](variables.tf#L37) | Additional disks, if options is null defaults will be used in its place. Source type is one of 'image' (zonal disks in vms and template), 'snapshot' (vm), 'existing', and null. | list(object({…})) | | [] | | [boot_disk](variables.tf#L83) | Boot disk properties. | object({…}) | | {…} | @@ -863,23 +863,24 @@ module "sole-tenancy" { | [description](variables.tf#L134) | Description of a Compute Instance. | string | | "Managed by the compute-vm Terraform module." | | [enable_display](variables.tf#L140) | Enable virtual display on the instances. | bool | | false | | [encryption](variables.tf#L146) | Encryption options. Only one of kms_key_self_link and disk_encryption_key_raw may be set. If needed, you can specify to encrypt or not the boot disk. | object({…}) | | null | -| [group](variables.tf#L156) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | -| [hostname](variables.tf#L164) | Instance FQDN name. | string | | null | -| [iam](variables.tf#L170) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | -| [instance_schedule](variables.tf#L176) | Assign or create and assign an instance schedule policy. Either resource policy id or create_config must be specified if not null. Set active to null to dtach a policy from vm before destroying. | object({…}) | | null | -| [instance_type](variables.tf#L211) | Instance type. | string | | "f1-micro" | -| [labels](variables.tf#L217) | Instance labels. | map(string) | | {} | -| [metadata](variables.tf#L223) | Instance metadata. | map(string) | | {} | -| [min_cpu_platform](variables.tf#L229) | Minimum CPU platform. | string | | null | -| [network_attached_interfaces](variables.tf#L240) | Network interfaces using network attachments. | list(string) | | [] | -| [options](variables.tf#L263) | Instance options. | object({…}) | | {…} | -| [scratch_disks](variables.tf#L298) | Scratch disks configuration. | object({…}) | | {…} | -| [service_account](variables.tf#L310) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | object({…}) | | {} | -| [shielded_config](variables.tf#L320) | Shielded VM configuration of the instances. | object({…}) | | null | -| [snapshot_schedules](variables.tf#L330) | Snapshot schedule resource policies that can be attached to disks. | map(object({…})) | | {} | -| [tag_bindings](variables.tf#L373) | Resource manager tag bindings for this instance, in tag key => tag value format. | map(string) | | null | -| [tag_bindings_firewall](variables.tf#L379) | Firewall (network scoped) tag bindings for this instance, in tag key => tag value format. | map(string) | | null | -| [tags](variables.tf#L385) | Instance network tags for firewall rule targets. | list(string) | | [] | +| [gpu](variables.tf#L156) | GPU information. Based on https://cloud.google.com/compute/docs/gpus. | object({…}) | | null | +| [group](variables.tf#L185) | Define this variable to create an instance group for instances. Disabled for template use. | object({…}) | | null | +| [hostname](variables.tf#L193) | Instance FQDN name. | string | | null | +| [iam](variables.tf#L199) | IAM bindings in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | +| [instance_schedule](variables.tf#L205) | Assign or create and assign an instance schedule policy. Either resource policy id or create_config must be specified if not null. Set active to null to dtach a policy from vm before destroying. | object({…}) | | null | +| [instance_type](variables.tf#L240) | Instance type. | string | | "f1-micro" | +| [labels](variables.tf#L246) | Instance labels. | map(string) | | {} | +| [metadata](variables.tf#L252) | Instance metadata. | map(string) | | {} | +| [min_cpu_platform](variables.tf#L258) | Minimum CPU platform. | string | | null | +| [network_attached_interfaces](variables.tf#L269) | Network interfaces using network attachments. | list(string) | | [] | +| [options](variables.tf#L292) | Instance options. | object({…}) | | {…} | +| [scratch_disks](variables.tf#L327) | Scratch disks configuration. | object({…}) | | {…} | +| [service_account](variables.tf#L339) | Service account email and scopes. If email is null, the default Compute service account will be used unless auto_create is true, in which case a service account will be created. Set the variable to null to avoid attaching a service account. | object({…}) | | {} | +| [shielded_config](variables.tf#L349) | Shielded VM configuration of the instances. | object({…}) | | null | +| [snapshot_schedules](variables.tf#L359) | Snapshot schedule resource policies that can be attached to disks. | map(object({…})) | | {} | +| [tag_bindings](variables.tf#L402) | Resource manager tag bindings for this instance, in tag key => tag value format. | map(string) | | null | +| [tag_bindings_firewall](variables.tf#L408) | Firewall (network scoped) tag bindings for this instance, in tag key => tag value format. | map(string) | | null | +| [tags](variables.tf#L414) | Instance network tags for firewall rule targets. | list(string) | | [] | ## Outputs diff --git a/modules/compute-vm/main.tf b/modules/compute-vm/main.tf index a5a3295d0f..56adefae3e 100644 --- a/modules/compute-vm/main.tf +++ b/modules/compute-vm/main.tf @@ -30,11 +30,12 @@ locals { k => v if try(v.options.replica_zone, null) == null } on_host_maintenance = ( - var.options.spot || var.confidential_compute + var.options.spot || var.confidential_compute || local.gpu ? "TERMINATE" : "MIGRATE" ) region = join("-", slice(split("-", var.zone), 0, 2)) + gpu = var.gpu != null service_account = var.service_account == null ? null : { email = ( var.service_account.auto_create @@ -333,6 +334,14 @@ resource "google_compute_instance" "default" { resource_manager_tags = local.tags_combined } } + + dynamic "guest_accelerator" { + for_each = local.gpu ? [var.gpu] : [] + content { + type = guest_accelerator.value.type + count = guest_accelerator.value.count + } + } } resource "google_compute_instance_iam_binding" "default" { @@ -376,6 +385,13 @@ resource "google_compute_instance_template" "default" { } } + dynamic "guest_accelerator" { + for_each = local.gpu ? [var.gpu] : [] + content { + type = guest_accelerator.value.type + count = guest_accelerator.value.count + } + } dynamic "disk" { for_each = local.attached_disks iterator = config diff --git a/modules/compute-vm/variables.tf b/modules/compute-vm/variables.tf index d11e00ec8c..8d6e8bc37f 100644 --- a/modules/compute-vm/variables.tf +++ b/modules/compute-vm/variables.tf @@ -153,6 +153,35 @@ variable "encryption" { default = null } +variable "gpu" { + description = "GPU information. Based on https://cloud.google.com/compute/docs/gpus." + type = object({ + count = number + type = string + }) + default = null + + validation { + condition = ( + var.gpu == null || + contains( + [ + "nvidia-tesla-a100", + "nvidia-tesla-p100", + "nvidia-tesla-v100", + "nvidia-tesla-k80", + "nvidia-tesla-p4", + "nvidia-tesla-t4", + "nvidia-l4", + "nvidia-a2" + ], + try(var.gpu.type, "-") + ) + ) + error_message = "GPU type must be one of the allowed values: nvidia-tesla-a100, nvidia-tesla-p100, nvidia-tesla-v100, nvidia-tesla-k80, nvidia-tesla-p4, nvidia-tesla-t4, nvidia-l4, nvidia-a2." + } +} + variable "group" { description = "Define this variable to create an instance group for instances. Disabled for template use." type = object({ @@ -392,3 +421,5 @@ variable "zone" { description = "Compute zone." type = string } + +