I have created a compute module that has a conditional to create an external IP.
resource "google_compute_address" "external" {
count = "${var.EXT_IP_CREATE ? 1 : 0}"
name = "${var.NAME}-ext-ip"
address_type = "EXTERNAL"
region = "${var.REGION}"}
Within the compute instance resource block, I have the following network interface configuration:
network_interface {
network= "${var.NETWORK}"
network_ip = "${google_compute_address.internal.address}"
access_config {
nat_ip = "${var.EXT_IP_CREATE ? google_compute_address.external.address : 0 }"
}
}
If the resource google_compute_address.external has not been created, I need to set nat_ip to null or in other words 0.
That looks like it should work but it does not.
When setting EXT_IP_CREATE to true TF succeeds to create the resource. When setting it to false I receive the following error:
Error: Error running plan: 1 error(s) occurred:
* module.compute-dbma-dev.google_compute_instance.compute: 1 error(s) occurred:
* module.compute-dbma-dev.google_compute_instance.compute: Resource 'google_compute_address.external' not found for variable 'google_compute_address.external.address'
When I explicitly pass nat_ip = 0 TF recognizes the blank value and successfully creates the compute instances without the external IP.
Im currently on Terraform version Terraform v0.11. There is probably a super simple solution but I am just starting out with conditionals in TF and I getting stuck here.
Thanks in advance!
null
is not possible before 0.12, but your real problem here is you need to set it for all of thenetwork_interface
. – Matt Schuchard