An attribute name is required after a dot

Hi Team,
I am using the following code to create organization on terraform cloud and create VCS provider for each mentioned organization with their Github Org, and i wanted to take the name of varialbe from a given yml file and use that variable inside the child module, i tried the below but getting the following error, can you please help me to resolve this issue.

Code:

terraform {
required_version = “~> 1.0”
required_providers {
tfe = {
version = “~> 0.27.0”
}
}
}

variable “tfe_dev_token” {}
variable “TESTABCD” {}
variable “TEST1ABCD” {}

locals {
rawdata = yamldecode(file(“meta.yml”))
}

provider “tfe” {
alias = “dev”
hostname = “*******”
token = var.tfe_dev_token
}

module “myorg” {
source = “./modules/myorg/”
providers = { tfe = tfe.dev }
for_each = { for k,v in local.rawdata.organization : k => v }

orgame = each.value.name
owner_email = each.value.contact
my_gh_token = var."${each.value.gh_var}"
}

my sample yml file:


organizations:

ERROR:

Error: Invalid attribute name

on main.tf line 31, in module "myorg":
 31:  my_gh_token  = var."${each.value.gh_var}"

An attribute name is required after a dot.

Hi @vburamdo,

Gather all of the values you want to allow the YAML to refer to into a mapping value:

locals {
  gh_vars = {
    TESTABCD  = var.TESTABCD
    TESTABCD1 = var.TESTABCD1
  }
}

Then in your module call:

  my_gh_token = local.gh_vars[each.value.gh_var]

Terraform is not a shell scripting language which treats variable references just as trivial replacements into the source code. In Terraform we must program with data structures, and so in this case I used a mapping data structure which is an appropriate approach whereever you need to look up a value using a dynamically-selected key. This is what might be called a “map”, “table”, “hash”, or “hashtable” in other programming languages.

Hi @apparentlymart ,

This worked, Thanks a lot.