Ron Jarrell
Ron Jarrell

Reputation: 471

Terraform .11 to .12 conversion of deeply nested data

So, in my old .11 code, I have a file where i my output modules locals section, I'm building:

this_assigned_nat_ip = google_compute_instance.this_public.*.network_interface.0.access_config.0.assigned_nat_ip-- 

Which later gets fed to the output statement.  This module could create N instances. So what it used to do was give me the first nat ip on the first access_config block on the first network interface of all the instances we created.  (Someone locally wrote the code so we know that there's only going to be one network interface with one access config block). How do I translate that to t12?  I'm unsure of the syntax to keep the nesting.

Update: Here's a chunk of the raw data out of a terraform show from tf11 (slightly sanitized)

module.gcp_bob_servers_ams.google_compute_instance.this_public.0:
  machine_type = n1-standard-2
  min_cpu_platform =
  network_interface.# = 1
  network_interface.0.access_config.# = 1
  network_interface.0.access_config.0.assigned_nat_ip =
  network_interface.0.access_config.0.nat_ip = 1.2.3.4
  network_interface.0.access_config.0.network_tier = PREMIUM

Terraform show of equivalent host in tf12:

# module.bob.module.bob_gcp_ams.module.atom_d.google_compute_instance.this[1]:
resource "google_compute_instance" "this" {
    allow_stopping_for_update = true

    network_interface {
        name               = "nic0"
        network            = "https://www.googleapis.com/compute/v1/projects/stuff-scratch/global/networks/scratch-public"
        network_ip         = "10.112.112.6"
        subnetwork         = "https://www.googleapis.com/compute/v1/projects/stuff-scratch/regions/europe-west4/subnetworks/scratch-europe-west4-x-public-subnet"
        subnetwork_project = "stuff-scratch"

        access_config {
            nat_ip       = "35.204.132.177"
            network_tier = "PREMIUM"
        }
    }

    scheduling {
        automatic_restart   = true
        on_host_maintenance = "MIGRATE"
        preemptible         = false
    }
}

Upvotes: 1

Views: 125

Answers (2)

Ron Jarrell
Ron Jarrell

Reputation: 471

Turns out this[*].network_interface[*].access_config[*].nat_ip[*] gave me what I needed. Given there's only every going to be one address on the interface, it comes out fine.

Upvotes: 0

Saverio Proto
Saverio Proto

Reputation: 1145

If I understand correctly this_assigned_nat_ip is a list of IPs. You should be able to get the same thing in Terraform 0.12 by doing:

this_assigned_nat_ip = [for i in google_compute_instance.this_public : i.network_interface.0.access_config.0.assigned_nat_ip]

I did not test is, so I might have some small syntax error, but the for is the key to get that done.

Upvotes: 1

Related Questions