Search code examples
terraformterraform-cloud

Get variable value from terraform cloud


I have data result:

test = [
      + {
          + category  = "terraform"
          + hcl       = false
          + id        = "var-1adsJ88M"
          + name      = "myValue"
          + sensitive = false
          + value     = "keys"
        },
      + {
          + category  = "terraform"
          + hcl       = false
          + id        = "var-WcFasdas1"
          + name      = "potoken"
          + sensitive = false
          + value     = "b6adasd222gt5Nh("
        }
       ]

How to get value a specific name.

Example: I need get value from of name myValue. This name can be in any position, I need to search the array

My attempts:

output "variables_cloud" {
  value = data.tfe_variables.test.variables[*].name == "myValue"
}

Solution

  • The easiest way is to search iteratively:

    locals {
        test = [
               {
                   category  = "terraform"
                   hcl       = false
                   id        = "var-1adsJ88M"
                   name      = "myValue"
                   sensitive = false
                   value     = "keys"
                },
               {
                   category  = "terraform"
                   hcl       = false
                   id        = "var-WcFasdas1"
                   name      = "potoken"
                   sensitive = false
                   value     = "b6adasd222gt5Nh("
                }
               ]
               
        value_found = [for a_map in local.test: a_map.value if a_map.name == "myValue"][0]
    }