doubledecker
doubledecker

Reputation: 363

create terraform hcl variable as a map

I am working on terraform workspace automation using tfe provider and want to create a terraform HCL variable as a map using custom_tags from the below data structure.

workspaces = {
  "PROD" = {
    "custom_tags" = {
      "Application" = "demo"
      "EnvironmentType" = "prod"
      "NamePrefix" = "sof"
      "ProductType" = "terraform"
    }
    "env_variables" = {}
    "id" = "alfsdfksf"
    "name" = "PROD"
    "repo" = "github/something"
    "tf_variables" = {}
  }
  "UAT" = {
    "custom_tags" = {
      "Application" = "demo"
      "EnvironmentType" = "uat"
      "NamePrefix" = "sof"
      "ProductType" = "terraform"
    }
    "env_variables" = {}
    "id" = "ws-k7KWYfsdfsdf"
    "name" = "UAT"
    "repo" = "github/otherthing"
    "tf_variables" = {}
  }
}

Here is my resource block

resource "tfe_variable" "terraform_hcl_variables" {
  for_each     = { for w in local.workspaces : w.name => w }
  key          = "custom_tags"
  value        = each.value.custom_tags
  category     = "terraform"
  hcl          = true
  sensitive    = false
  workspace_id = tfe_workspace.main[each.key].id
}

And, I am getting this error. Any help is appreciated to resolve this.

**each.value.custom_tags is object with 4 attributes
Inappropriate value for attribute "value": string required.**

Expected outcome

custom_tags should be created as a HCL variable

custom_tags = 
{
   "Application" = "demo"
   "EnvironmentType" = "prod"
   "NamePrefix" = "sof"
   "ProductType" = "terraform"
}

Upvotes: 3

Views: 2372

Answers (2)

Mitko Keckaroski
Mitko Keckaroski

Reputation: 1040

Try this:

workspace_id = tfe_workspace.main[each.key].workspace_id

Upvotes: 0

Marcin
Marcin

Reputation: 238131

Sadly you can't do this. value attribute must be string, but you are trying to assign an "object with 4 attributes" to it.

You could convert your each.value.custom_tags into string using jsonencode, but this is probably not what you want.

Upvotes: 1

Related Questions