Invalid index and data dependency behavior

Hi guys,

At this point, i’m not sure this is a bug or a scrambled template.

I need to create a folder structure in GCP based on an input yaml.

It globally works fine, when i initialize the environment (terraform plans and applies the whole structure from without error) or when i add a subfolder.

But it wont work when i try to modify an existing applied infrastructure by adding a folder and its subfolders (more than one level of change).

Same behaviour with TF 0.13.4 and 0.13.5

Like i said i have a simple yaml structure of this kind :

fh:
  folder1:
    owner: []
    subs:
      production:
        owner: []
        subs:
          app1:
            owner:  []
            subs: {}
          app2:
            owner: []
            subs: {}
          app3:
            owner: []
            subs: {}
      staging:
        owner: []
        subs:
          app1:
            owner:  []
            subs: {}
          app2:
            owner: []
            subs: {}
          app3:
            owner: []
            subs: {}

This is decoded and parsed by the variables.tf

locals {
  folder_root = flatten([
    for root_folder, child_folders in var.folder_structure_iam.fh : {
      folder = root_folder
      parent = var.root_hierarchy_element
    }
  ])

  folder_sub1_childs = flatten([
    for root_folder, root_content in var.folder_structure_iam.fh : [
      for sub1, sub2_folders in root_content.subs : {
                                parent_name = root_folder
        folder      = sub1
        parent      = data.google_active_folder.root_folder_lookup[format("%s", root_folder)].name
      }
    ]
  ])

  folder_sub2_childs = flatten([
                for root_folder, root_content in var.folder_structure_iam.fh : [
      for sub1, sub1_content in root_content.subs : [
        for sub2, sub2_content in sub1_content.subs : {
                                        parent_name = format("%s_%s", root_folder, sub1)
          folder      = sub2
          parent      = data.google_active_folder.sub1_folder_lookup[format("%s_%s", root_folder, sub1)].name
        }
      ]
    ]
  ])

These locals are feed to the main.tf

resource "google_folder" "root_folder" {
  for_each     = { for fold in local.folder_root : fold.folder => fold }
  parent       = each.value.parent
  display_name = each.value.folder
}

data "google_active_folder" "root_folder_lookup" {
  for_each     = { for fold in local.folder_root : fold.folder => fold }
  parent       = each.value.parent
  display_name = each.value.folder
  depends_on = [
    google_folder.root_folder,
  ]
}

resource "google_folder" "sub1_folder" {
  for_each     = { for fold in local.folder_sub1_childs : "${fold.parent_name}_${fold.folder}" => fold }
  parent       = each.value.parent
  display_name = each.value.folder
  depends_on = [
    google_folder.root_folder,
  ]
}

data "google_active_folder" "sub1_folder_lookup" {
  for_each     = { for fold in local.folder_sub1_childs : "${fold.parent_name}_${fold.folder}" => fold }
  parent       = each.value.parent
  display_name = each.value.folder
  depends_on = [
    google_folder.sub1_folder,
  ]
}

resource "google_folder" "sub2_folder" {
  for_each     = { for fold in local.folder_sub2_childs : "${fold.parent_name}_${fold.folder}" => fold }
  parent       = each.value.parent
  display_name = each.value.folder
  depends_on = [
    google_folder.sub1_folder,
  ]
}

If i try for example, to add a new folder “development” and its three subfolders. It throws an error, even if i place the depends_on instructions.

The resource creation of the subfolders fails because it queries for the id of the development not yet created.
`

Error: Invalid index

  on variables.tf line 25, in locals:
  25:           parent      = data.google_active_folder.sub1_folder_lookup[format("%s_%s", root_folder, sub1)].name
    |----------------
    | data.google_active_folder.sub1_folder_lookup is object with 11 attributes

The given key does not identify an element in this collection value.

Error: Invalid index

  on variables.tf line 25, in locals:
  25:           parent      = data.google_active_folder.sub1_folder_lookup[format("%s_%s", root_folder, sub1)].name
    |----------------
    | data.google_active_folder.sub1_folder_lookup is object with 11 attributes

The given key does not identify an element in this collection value.

Error: Invalid index

  on variables.tf line 25, in locals:
  25:           parent      = data.google_active_folder.sub1_folder_lookup[format("%s_%s", root_folder, sub1)].name
    |----------------
    | data.google_active_folder.sub1_folder_lookup is object with 11 attributes

The given key does not identify an element in this collection value.

Is this working as designed ?

I just did some tests with different 0.12, and it works fine with the 0.12.29 only for the creation or modification of the structure applied. Although, the destroy complains a lot about invalid keys and doesnt work.

With that template, with 0.13.5, destroy runs fine.

I guess it’s related to these :

Everything is working as expected with 0.14.0-beta1