Yamldecode with terraform

Hi

I am not sure what I am doing wrong.

I have the following yaml file.

- workspaces:
  - workspace_name: foo-test-dev
    workspace_permissions:
    - team_name: foo
      access_type: admin
    - team_name: foobar
      access_type: read

in my locals.tf
I have following

locals {
  application_config = flatten([for config in var.configs : yamldecode(file("${path.module}/configs/workspace_config.yaml"))])

  config = {
    applications = local.application_config,
  }

  workspaces = flatten([for application in local.config.applications : [
    for workspace in application.workspaces : {
      workspace_name = workspace.workspace_name
      workspace      = workspace
    }]
  ])

  terraform_workspace_team_permissions = flatten([for workspace in local.workspaces : [
    for permission in workspace.workspace.workspace_permissions : {
      workspace_name = workspace.workspace_name
      team_name      = permission.team_name
      access_type    = permission.access_type
    }]
  ])
}

when I run terraform apply I get the following error message.

│ Error: Unsupported attribute
│
│   on locals.tf line 18, in locals:
│   18:       team_name      = permission.team_name
│
│ This object does not have an attribute named "team_name".
╵
╷
│ Error: Unsupported attribute
│
│   on locals.tf line 19, in locals:
│   19:       access_type    = permission.accee_type
│
│ This object does not have an attribute named "accee_type".
╵
╷

If anyone could assist it would be much appreciated.

Thanks

This line doesn’t make sense. For each entry in var.configs you repeatedly load the same file over and over.

This transformation seems unwise - by introducing a second different representation of a workspace, for no clear reason, the readers and writers of your code now have to figure out which representation is being used at various points in the code. I think you should avoid introducing this extra complexity - I see no reason for it.

The code you’ve shown in this question, works. I literally tested it by copy/pasting it into files and running terraform console on it. It does not produce these errors.

There is also a clear misspelling in the error message (accee_type) that’s fixed in the sample code.

Hi maxb

Thanks for your feedback, that is so strange that it works for you, I am still getting the same error.

Also, yes you are right regarding the var.config I was playing with some options on leading different files by specifying it in the variables file, but decided to go against it and forgot to clean it up.

Could I ask how exactly did you test it with Terraform console?

  1. Copy first code section into a YAML file
  2. Copy second code section into a .tf file
  3. Make minimal updates to .tf file for compatibility - replace YAML file name with one I used, replace reference to var.configs with a constant
  4. terraform init
  5. terraform console
  6. Enter local.terraform_workspace_team_permissions at the console prompt
  7. Successfully get rendered output:
[
  {
    "access_type" = "admin"
    "team_name" = "foo"
    "workspace_name" = "foo-test-dev"
  },
  {
    "access_type" = "read"
    "team_name" = "foobar"
    "workspace_name" = "foo-test-dev"
  },
]

Thank you, I still have got no idea why I am getting this error…