1

This question is similar to this one, but is more complicated: Terraform, "ignore_changes" and sub-blocks.

I'm doing this:

variable "myapp_config" {
  type = object({
    app_name                 = string
    google_bigtable_clusters = any
  })
  default = {
    app_name = "myapp"
    google_bigtable_clusters = {
      instance01 = [
        {
          zone      = "us-west1-a"
          num_nodes = 1
        },
        {
          zone      = "us-west4-a"
          num_nodes = 1
        },
        {
          zone      = "us-central1-a"
          num_nodes = 1
        },
        {
          zone      = "us-east1-b"
          num_nodes = 1
        },
      ],
      instance02 = [
        {
          zone      = "us-east4-a"
          num_nodes = 1
        },
        {
          zone      = "northamerica-northeast1-a"
          num_nodes = 1
        },
        {
          zone      = "europe-west4-a"
          num_nodes = 1
        },
      ],
      instance03 = [
        {
          zone      = "europe-west6-a"
          num_nodes = 1
        },
        {
          zone      = "asia-south1-a"
          num_nodes = 1
        },
      ],
    }
  }
}

I'm looping over a var to create google_bigtable_instance resource but I need to ignore the num_nodes fields. I can't figure out how to do this

resource "google_bigtable_instance" "myinstances" {
  for_each = var.myapp_config.google_bigtable_clusters
  name                = "${var.myapp_config.app_name}--${each.key}"
  deletion_protection = true
  dynamic "cluster" {
    iterator = instance
    for_each = each.value
    content {
      cluster_id   = instance.value.zone
      zone         = instance.value.zone
      num_nodes    = instance.value.num_nodes
      storage_type = "SSD"
    }
  }
  lifecycle {
    # num_nodes are variable so I can't statically set the index numbers
    # ignore_changes = ["cluster.*.num_nodes"]
    ignore_changes = ["cluster.${each.key}.num_nodes"]
  }
}

I get this error:

A single static variable reference is required: only attribute access and
indexing with constant keys. No calculations, function calls, template
expressions, etc are allowed here.

I hope this isn't impossible. I'm not about to statically write out all these resources because the list is going to grow and this is what for_each is designed for.

Edit

Realized ${each.key} is not what I want I need to get the index number of that nested list.

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392

1 Answers1

1

I supposed I should have tried this first, but I had trouble figuring this out googling. It seems statically specifying the indices works.

I was afraid TF would error out if the field specified does not exist, but mercifully it appears it does not. I was able to specify the total upper bound with static indices. BT instances can only support up to 4 clusters so this works for my specific use case:

  lifecycle {
    ignore_changes = ["cluster[0].num_nodes", "cluster[1].num_nodes", "cluster[2].num_nodes", "cluster[3].num_nodes"]
  }

But I'm still curious to know if what I was trying to do is currently impossible. It's not required for this use case but wondering if it's possible to dynamically ignore_changes in a nested loop like this.

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392