Skip to content

Commit b48928a

Browse files
authored
fix: Use network settings from template when no specific override is in place (#239)
1 parent 1abd8c7 commit b48928a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

modules/compute_instance/main.tf

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ locals {
2323
# determine type" error when var.static_ips is empty
2424
static_ips = concat(var.static_ips, ["NOT_AN_IP"])
2525
project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null
26+
27+
# When no network or subnetwork has been defined, we want to use the settings from
28+
# the template instead.
29+
network_interface = length(format("%s%s", var.network, var.subnetwork)) == 0 ? [] : [1]
2630
}
2731

2832
###############
@@ -47,16 +51,20 @@ resource "google_compute_instance_from_template" "compute_instance" {
4751
deletion_protection = var.deletion_protection
4852

4953

50-
network_interface {
51-
network = var.network
52-
subnetwork = var.subnetwork
53-
subnetwork_project = var.subnetwork_project
54-
network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)
55-
dynamic "access_config" {
56-
for_each = var.access_config
57-
content {
58-
nat_ip = access_config.value.nat_ip
59-
network_tier = access_config.value.network_tier
54+
dynamic "network_interface" {
55+
for_each = local.network_interface
56+
57+
content {
58+
network = var.network
59+
subnetwork = var.subnetwork
60+
subnetwork_project = var.subnetwork_project
61+
network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)
62+
dynamic "access_config" {
63+
for_each = var.access_config
64+
content {
65+
nat_ip = access_config.value.nat_ip
66+
network_tier = access_config.value.network_tier
67+
}
6068
}
6169
}
6270
}

0 commit comments

Comments
 (0)