Yamldecode - Cannot include the given value in a string template: string required

Viewed 20

I have some very simple code to do some testing with and I am running into some issues. My overall goal is to have config stored in a .yaml file which I then use templatefile to replace some variables and finally it will be jsonencoded.

main.tf

locals {
  test = yamldecode(templatefile("${path.module}/test.yaml",
    {
      admin_groups         = ["123456-7890"]
    }
  ))
}

output "test" {
  value = jsonencode(local.test)
}

test.yaml

GITLAB_OMNIBUS_CONFIG: |
  gitlab_rails['omniauth_providers'] = |
      admin_groups: ['${admin_groups}']

When terraform plan however I am getting the following error message:

│ Error: Error in function call
│ 
│   on main.tf line 2, in locals:
│    2:   test = yamldecode(templatefile("${path.module}/test.yaml",
│    3:     {
│    4:       admin_groups = ["123456-7890"]
│    5:     }
│    6:   ))
│     ├────────────────
│     │ path.module is "."
│ 
│ Call to function "templatefile" failed: ./test.yaml:3,25-37: Invalid template interpolation value; Cannot include the given value in a string template: string required..

Is there some way to interpolate this list of strings that I am not doing? Cheers for any help.

1 Answers

For list in yaml, you should iterate the values like:

test.yaml

GITLAB_OMNIBUS_CONFIG: |
  gitlab_rails['omniauth_providers'] = |
      admin_groups: 
      %{ for g in admin_groups ~}
        - ${g}
      %{ endfor ~}
Related