1

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!

1
  • So your intention to do an optional argument with null is not possible before 0.12, but your real problem here is you need to set it for all of the network_interface. Commented Mar 13, 2019 at 17:57

2 Answers 2

1

Two ways to fix that:

  1. TF_WARN_OUTPUT_ERRORS=1 terraform apply
  2. ${element(concat(google_compute_address.*.address, list("")), 0)}
Sign up to request clarification or add additional context in comments.

2 Comments

Neither 1 or 2 worked. For 1, I am still receiving the same resource not found for variable error. For 2, I am receiving this: Error: resource 'google_compute_instance.compute' config: unknown resource 'google_compute_address.*' referenced in variable google_compute_address.*.address
try: ${element(concat(google_compute_address.external.*.address, list("")), 0)}"
0

When I tried to use a similar conditional, I get the following error:

* google_compute_instance.main: __builtin_StringToInt: strconv.ParseInt: parsing "": invalid syntax in:

${var.external_ip != "" ? var.external_ip : 0}

I do not see how the external IP can be conditionally attached, based on how GCP API works currently [1]:

networkInterfaces[].accessConfigs[].natIP => string

An external IP address associated with this instance. Specify an unused static external IP address available to the project or leave this field undefined to use an IP from a shared ephemeral IP address pool. If you specify a static external IP address, it must live in the same region as the zone of the instance.

[1] https://cloud.google.com/compute/docs/reference/rest/v1/instances

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.