Skip to content

Commit

Permalink
feat: add spot instances (#294)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Peabody <[email protected]>
Co-authored-by: Bharath KKB <[email protected]>
Co-authored-by: Awais Malik <[email protected]>
  • Loading branch information
4 people authored Aug 22, 2023
1 parent 976c323 commit 99efc45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions modules/instance_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
18 changes: 12 additions & 6 deletions modules/instance_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions modules/instance_template/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
Expand Down

0 comments on commit 99efc45

Please sign in to comment.