Use Variable File to Define Body of Local Variable

Hello all, I’m a fairly recent user of terraform and still finding my ways around it.

I have come across a situation where I need to set up a local variable named env of type map, and inside this I want to pass in key-value pairs which are present in another file.

Essentially, I’ve a .tfvars file which I wanted to pass to the env local variable. But I haven’t been able to get it work. I have tried some combinations of file, tomap, jsonencode.

Any ideas on this front would be really appreciated.

Hi @sohi429,

A .tfvars file is only used for the purpose of assigning values to input variables declared in the root module. If you’ve seen child module blocks in your usage already then you could perhaps think of the contents of a .tfvars file as being like the contents of a virtual module block that “calls” the root module.

With that said, the only way to make use of data in such a file would be to declare a root module input variable, and then write a definition of that variable in your .tfvars file:

variable "example" {
  type = object({
    argument1 = string
    argument2 = number

You could then assign that a value by writing a .tfvars file like this:

example = {
  argument1 = "hello"
  argument2 = 5

If you want to refer to these attributes elsewhere in your module, including in local value expressions, then you can refer them as var.example.argument1 and var.example.argument2.

With that said, I expect I’m misunderstanding what you are trying to achieve here. If so, it’d be helpful if you could say a little more about the real underlying problem you are trying to solve, rather than the specific implementation strategy you’ve chosen so far, because I’d prefer to help you think through a few different approaches to solving your problem that are typical Terraform patterns.

Thanks a lot @apparentlymart !

The scenario that we have is the following:

We are making use of Terraform Enterprise and have set up separate workspaces for each environment (dev, test and prod). We have a single repo underneath and three distinct set of variables for each environment. Now, we want to manage the variables deployment separately for all these environments via terraform (not Terraform Enterprise).

Now, these variables are located in three separate files (essentially .tfvars). I have set up a module to convert these into a tfe_variable resource using jsonencode, regex replace and the for_each argument.

For testing/development, I’m able to get this work using a local variable like the following:

locals {
    env = {
        var1 = "var1"
        var2 = "var2"
        varn = "varn"

Now, this has been working fine during local development. Ideally I would want to refer the env variable in the code above to point to a file (which would in the same format as a .tfvars file) instead of hardcoding these values. So, the idea is to plug in different files (based on environment) during the build/release pipeline.