diff --git a/modules/instance_template/README.md b/modules/instance_template/README.md index fbb6e0c9..5ce8c472 100644 --- a/modules/instance_template/README.md +++ b/modules/instance_template/README.md @@ -45,6 +45,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example. | source\_image | Source disk image. If neither source\_image nor source\_image\_family is specified, defaults to the latest public CentOS image. | `string` | `""` | no | | 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 | | source\_image\_project | Project where the source image comes from. The default project contains CentOS images. | `string` | `"centos-cloud"` | no | +| spot | Provision a SPOT instance | `bool` | `false` | no | | stack\_type | The stack type for this network interface to identify whether the IPv6 feature is enabled or not. Values are `IPV4_IPV6` or `IPV4_ONLY`. Default behavior is equivalent to IPV4\_ONLY. | `string` | `null` | no | | startup\_script | User startup script to run when instances spin up | `string` | `""` | no | | 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 | diff --git a/modules/instance_template/main.tf b/modules/instance_template/main.tf index bd4a846e..4b2d4729 100644 --- a/modules/instance_template/main.tf +++ b/modules/instance_template/main.tf @@ -45,13 +45,17 @@ locals { 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.preemptible || var.enable_confidential_vm || local.gpu_enabled || var.spot ? "TERMINATE" : var.on_host_maintenance ) automatic_restart = ( - # must be false when preemptible is true - var.preemptible ? false : var.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 ) } @@ -158,9 +162,11 @@ resource "google_compute_instance_template" "tpl" { } scheduling { - preemptible = var.preemptible - automatic_restart = local.automatic_restart - on_host_maintenance = local.on_host_maintenance + preemptible = local.preemptible + automatic_restart = local.automatic_restart + on_host_maintenance = local.on_host_maintenance + provisioning_model = var.spot ? "SPOT" : null + instance_termination_action = var.spot ? "STOP" : null } advanced_machine_features { diff --git a/modules/instance_template/variables.tf b/modules/instance_template/variables.tf index c1072376..97e73958 100644 --- a/modules/instance_template/variables.tf +++ b/modules/instance_template/variables.tf @@ -62,6 +62,12 @@ variable "preemptible" { default = false } +variable "spot" { + type = bool + description = "Provision a SPOT instance" + default = false +} + variable "automatic_restart" { type = bool description = "(Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user)."