provider::terraform::encode_expr Function

provider::terraform::encode_expr is a rarely-needed function which takes any value and produces a string containing Terraform language expression syntax approximating that value.

To use this function, your module must declare a dependency on the built-in terraform provider, which contains this function:

terraform {
  required_providers {
    terraform = {
      source = "terraform.io/builtin/terraform"
    }
  }
}

The primary use for this function is in conjunction with the hashicorp/tfe provider's resource type tfe_variable, which expects variable values to be provided in Terraform expression syntax.

For example, the following concisely declares multiple input variables for a particular HCP Terraform workspace:

locals {
  workspace_vars = {
    example1 = "Hello"
    example2 = ["A", "B"]
  }
}

resource "tfe_variable" "test" {
  for_each = local.workspace_vars

  category     = "terraform"
  workspace_id = tfe_workspace.example.id

  key   = each.key
  value = provider::terraform::encode_expr(each.value)
  hcl   = true
}

When using this pattern, always set hcl = true in the resource declaration to ensure that HCP Terraform will expect value to be given as Terraform expression syntax.

We do not recommend using this function in any other situation.