From 6f7471557f2beea94b91f81168aba50878737aa5 Mon Sep 17 00:00:00 2001 From: Tom Downes Date: Wed, 20 Sep 2023 18:52:56 -0500 Subject: [PATCH] feat: support setting instance_termination_action for Spot VMs (#346) Co-authored-by: Awais Malik --- modules/instance_template/README.md | 1 + modules/instance_template/main.tf | 2 +- modules/instance_template/variables.tf | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/instance_template/README.md b/modules/instance_template/README.md index 31d8e5a5..563b92d1 100644 --- a/modules/instance_template/README.md +++ b/modules/instance_template/README.md @@ -47,6 +47,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example. | 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 | +| spot\_instance\_termination\_action | Action to take when Compute Engine preempts a Spot VM. | `string` | `"STOP"` | 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 1a18c2c5..22d8c11f 100644 --- a/modules/instance_template/main.tf +++ b/modules/instance_template/main.tf @@ -177,7 +177,7 @@ resource "google_compute_instance_template" "tpl" { 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 + instance_termination_action = var.spot ? var.spot_instance_termination_action : null } advanced_machine_features { diff --git a/modules/instance_template/variables.tf b/modules/instance_template/variables.tf index b46426af..624b6431 100644 --- a/modules/instance_template/variables.tf +++ b/modules/instance_template/variables.tf @@ -80,6 +80,17 @@ variable "on_host_maintenance" { default = "MIGRATE" } +variable "spot_instance_termination_action" { + description = "Action to take when Compute Engine preempts a Spot VM." + type = string + default = "STOP" + + validation { + condition = contains(["STOP", "DELETE"], var.spot_instance_termination_action) + error_message = "Allowed values for spot_instance_termination_action are: \"STOP\" or \"DELETE\"." + } +} + variable "region" { type = string description = "Region where the instance template should be created."