Skip to content

Commit de34522

Browse files
authored
feat: [compute_instance] Add deletion_protection variable (#231)
1 parent f04eb64 commit de34522

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

examples/compute_instance/simple/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This is a simple, minimal example of how to use the compute_instance module
77

88
| Name | Description | Type | Default | Required |
99
|------|-------------|------|---------|:--------:|
10+
| deletion\_protection | Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. | `bool` | `false` | no |
1011
| nat\_ip | Public ip address | `any` | `null` | no |
1112
| network\_tier | Network network\_tier | `string` | `"PREMIUM"` | no |
1213
| num\_instances | Number of instances to create | `any` | n/a | yes |

examples/compute_instance/simple/main.tf

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ module "instance_template" {
2323
}
2424

2525
module "compute_instance" {
26-
source = "../../../modules/compute_instance"
27-
region = var.region
28-
zone = var.zone
29-
subnetwork = var.subnetwork
30-
num_instances = var.num_instances
31-
hostname = "instance-simple"
32-
instance_template = module.instance_template.self_link
26+
source = "../../../modules/compute_instance"
27+
region = var.region
28+
zone = var.zone
29+
subnetwork = var.subnetwork
30+
num_instances = var.num_instances
31+
hostname = "instance-simple"
32+
instance_template = module.instance_template.self_link
33+
deletion_protection = false
34+
3335
access_config = [{
3436
nat_ip = var.nat_ip
3537
network_tier = var.network_tier

examples/compute_instance/simple/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ variable "network_tier" {
5151
default = "PREMIUM"
5252
}
5353

54+
variable "deletion_protection" {
55+
type = bool
56+
description = "Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully."
57+
default = false
58+
}
5459

5560
variable "service_account" {
5661
default = null

modules/compute_instance/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
1717
|------|-------------|------|---------|:--------:|
1818
| access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. | <pre>list(object({<br> nat_ip = string<br> network_tier = string<br> }))</pre> | `[]` | no |
1919
| add\_hostname\_suffix | Adds a suffix to the hostname | `bool` | `true` | no |
20+
| deletion\_protection | Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. | `bool` | `false` | no |
2021
| hostname | Hostname of instances | `string` | `""` | no |
2122
| hostname\_suffix\_separator | Separator character to compose hostname when add\_hostname\_suffix is set to true. | `string` | `"-"` | no |
2223
| instance\_template | Instance template self\_link used to create compute instances | `any` | n/a | yes |

modules/compute_instance/main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ data "google_compute_zones" "available" {
3939
#############
4040

4141
resource "google_compute_instance_from_template" "compute_instance" {
42-
provider = google
43-
count = local.num_instances
44-
name = var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
45-
project = local.project_id
46-
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
42+
provider = google
43+
count = local.num_instances
44+
name = var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
45+
project = local.project_id
46+
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
47+
deletion_protection = var.deletion_protection
48+
4749

4850
network_interface {
4951
network = var.network

modules/compute_instance/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ variable "hostname_suffix_separator" {
8080
description = "Separator character to compose hostname when add_hostname_suffix is set to true."
8181
default = "-"
8282
}
83+
84+
variable "deletion_protection" {
85+
type = bool
86+
description = "Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully."
87+
default = false
88+
}

0 commit comments

Comments
 (0)