From b48928abdca289f927fa0e5ab0fc6d8401694f18 Mon Sep 17 00:00:00 2001 From: Tim Stoop Date: Wed, 2 Mar 2022 18:52:59 +0100 Subject: [PATCH] fix: Use network settings from template when no specific override is in place (#239) --- modules/compute_instance/main.tf | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/compute_instance/main.tf b/modules/compute_instance/main.tf index c85df2e9..29a7d70e 100644 --- a/modules/compute_instance/main.tf +++ b/modules/compute_instance/main.tf @@ -23,6 +23,10 @@ locals { # determine type" error when var.static_ips is empty static_ips = concat(var.static_ips, ["NOT_AN_IP"]) project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null + + # When no network or subnetwork has been defined, we want to use the settings from + # the template instead. + network_interface = length(format("%s%s", var.network, var.subnetwork)) == 0 ? [] : [1] } ############### @@ -47,16 +51,20 @@ resource "google_compute_instance_from_template" "compute_instance" { deletion_protection = var.deletion_protection - network_interface { - network = var.network - subnetwork = var.subnetwork - subnetwork_project = var.subnetwork_project - network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index) - dynamic "access_config" { - for_each = var.access_config - content { - nat_ip = access_config.value.nat_ip - network_tier = access_config.value.network_tier + dynamic "network_interface" { + for_each = local.network_interface + + content { + network = var.network + subnetwork = var.subnetwork + subnetwork_project = var.subnetwork_project + network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index) + dynamic "access_config" { + for_each = var.access_config + content { + nat_ip = access_config.value.nat_ip + network_tier = access_config.value.network_tier + } } } }