diff --git a/modules/compute_instance/README.md b/modules/compute_instance/README.md index a2b8b3f6..db2b3a97 100644 --- a/modules/compute_instance/README.md +++ b/modules/compute_instance/README.md @@ -25,6 +25,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm | network | Network to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no | | num\_instances | Number of instances to create. This value is ignored if static\_ips is provided. | `string` | `"1"` | no | | region | Region where the instances should be created. | `string` | `null` | no | +| resource\_policies | (Optional) A list of short names or self\_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported. | `list(string)` | `[]` | no | | static\_ips | List of static IPs for VM instances | `list(string)` | `[]` | no | | subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no | | subnetwork\_project | The project that subnetwork belongs to | `string` | `""` | no | diff --git a/modules/compute_instance/main.tf b/modules/compute_instance/main.tf index df6f092d..17331dbe 100644 --- a/modules/compute_instance/main.tf +++ b/modules/compute_instance/main.tf @@ -49,6 +49,7 @@ resource "google_compute_instance_from_template" "compute_instance" { project = local.project_id zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone deletion_protection = var.deletion_protection + resource_policies = var.resource_policies dynamic "network_interface" { diff --git a/modules/compute_instance/variables.tf b/modules/compute_instance/variables.tf index 1b9623f0..e9816fa6 100644 --- a/modules/compute_instance/variables.tf +++ b/modules/compute_instance/variables.tf @@ -95,3 +95,9 @@ variable "alias_ip_ranges" { })) default = [] } + +variable "resource_policies" { + description = "(Optional) A list of short names or self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported." + type = list(string) + default = [] +} diff --git a/modules/instance_template/README.md b/modules/instance_template/README.md index 1e56f547..020acdb1 100644 --- a/modules/instance_template/README.md +++ b/modules/instance_template/README.md @@ -18,6 +18,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example. | additional\_networks | Additional network interface details for GCE, if any. |
list(object({| `[]` | no | | alias\_ip\_range | An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks.
network = string
subnetwork = string
subnetwork_project = string
network_ip = string
access_config = list(object({
nat_ip = string
network_tier = string
}))
}))
object({| `null` | no | | auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no | +| automatic\_restart | (Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user). | `bool` | `true` | no | | can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no | | disk\_encryption\_key | The id of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no | | disk\_labels | Labels to be assigned to boot disk, provided as a map | `map(string)` | `{}` | no | diff --git a/modules/instance_template/main.tf b/modules/instance_template/main.tf index 40ff3bd3..8b076508 100644 --- a/modules/instance_template/main.tf +++ b/modules/instance_template/main.tf @@ -50,6 +50,10 @@ locals { ? "TERMINATE" : var.on_host_maintenance ) + automatic_restart = ( + # must be false when preemptible is true + var.preemptible ? false : var.automatic_restart + ) } #################### @@ -141,10 +145,9 @@ resource "google_compute_instance_template" "tpl" { create_before_destroy = "true" } - # scheduling must have automatic_restart be false when preemptible is true. scheduling { preemptible = var.preemptible - automatic_restart = !var.preemptible + automatic_restart = local.automatic_restart on_host_maintenance = local.on_host_maintenance } diff --git a/modules/instance_template/variables.tf b/modules/instance_template/variables.tf index 2750747f..3b0ca194 100644 --- a/modules/instance_template/variables.tf +++ b/modules/instance_template/variables.tf @@ -59,6 +59,12 @@ variable "preemptible" { 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)." + default = true +} + variable "on_host_maintenance" { type = string description = "Instance availability Policy"
ip_cidr_range = string
subnetwork_range_name = string
})