Skip to content

Commit

Permalink
feat: support setting instance_termination_action for Spot VMs (#346)
Browse files Browse the repository at this point in the history
Co-authored-by: Awais Malik <[email protected]>
  • Loading branch information
tpdownes and g-awmalik authored Sep 20, 2023
1 parent 04fa8f6 commit 6f74715
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/instance_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion modules/instance_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions modules/instance_template/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit 6f74715

Please sign in to comment.