Problem using variable on yaml input file

Hello,

Is it possible to use a variable on a yaml input file ?

For exemple, the yaml file :

category:
- name: “TEST”
- name: “TEST2 ${var.account}”

The main.tf :

variable “account” {
type = string
description = “the account number”
}

locals{
input = yamldecode(file(“C:\test_terraform\view.yaml”))
}

resource “logdna_category” “my_category” {
for_each = { for x in local.input.category : x.name => x }

type = “views”
name = each.value.name
}

The result is :

terraform apply -var “account=12345”

logdna_category.my_category[“TEST2 $${var.account}”] will be created

  • resource “logdna_category” “my_category” {
    • id = (known after apply)
    • name = “TEST2 ${var.account}”
    • type = “views”
      }

Plan: 1 to add, 0 to change, 1 to destroy.

$${var.account} is view as a string and not a variable. How can I force the use of the variable ?

Thanks for your help

The YAML format does not include support for variables.

It is difficult to recommend a specific path forward - given the relatively low complexity of the test input data, moving it directly into your .tf file is the best way to go, but I imagine your real data might be more complex.

1 Like

Hello maxb,

Thanks for your response, yes the real data is more complex :slight_smile:

An upgrade is planned about that or not ?

Thanks

This is not something which can simply be upgraded. YAML is a standard format defined independently of Terraform.

In order for members of this forum to provide useful advice, I think you’ll have to describe, in more general terms, what you are trying to do - as the specific solution you’re currently aiming for is impossible, so it is necessary to step back and consider the available options without a preconceived solution.

If someone have the same problem, a solution can be the replace() function :

category:
- name: “TEST”
- name: “TEST2 @account@”

resource “logdna_category” “my_category” {
for_each = { for x in local.input.category : x.name => x }

type = “views”
name = replace(each.value.name,“@account@”,var.account)
}