# Preserve map-like values when read from an input file

**URL:** <https://discuss.hashicorp.com/t/preserve-map-like-values-when-read-from-an-input-file/42715>\
**Category:** Terraform\
**Created:** [August 2, 2022, 6:49pm UTC](https://discuss.hashicorp.com/t/preserve-map-like-values-when-read-from-an-input-file/42715 "2022-08-02T18:49:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcloud9](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dcloud9/32/991_2.png) [@dcloud9](https://discuss.hashicorp.com/u/dcloud9)\
**Post date:** [August 2, 2022, 6:49pm UTC](https://discuss.hashicorp.com/t/preserve-map-like-values-when-read-from-an-input-file/42715/1 "2022-08-02T18:49:39Z")

</div>

My use case is to read from a dynamic env file a mix of string literals and var.\* to pass them as env variables, however, I can’t preserve them like `var.region` becomes `"var.region"` (string literal) after parsing them.  
Is there a much better way to preserve such so they can be interpolated when reading from an input file?

Env file:

```auto
REGION=var.region
FOO="bar"

```

main.tf

```auto
environment_variables = { for k, v in toset(split("\n", file("${path.module}/lambda/${each.key}/environment_variables"))) : trim(split("=", k)[0], "\"") => trim(split("=", k)[1], "\"") }

}

```

Plan output:

```auto
      + environment {
          + variables = {
              + "FOO" = "bar"
              + "REGION" = "var.region"
            }
        }

```

Required plan output:

```auto
      + environment {
          + variables = {
              + "FOO" = "bar"
              + "REGION" = "eu-west-2" #interpolated by var.region
            }
        }

```

---

<div class="post-metadata">

**Author:** ![maxb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maxb/32/26655_2.png) [@maxb](https://discuss.hashicorp.com/u/maxb)\
**Post date:** [August 2, 2022, 7:28pm UTC](https://discuss.hashicorp.com/t/preserve-map-like-values-when-read-from-an-input-file/42715/2 "2022-08-02T19:28:22Z")

</div>

Terraform does not support that.

You will need to move your environment variable definitions into actual native .tf files, in the Terraform language, rather than loading them from a different text format using string manipulation.

---

<div class="post-metadata">

**Author:** ![dcloud9](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dcloud9/32/991_2.png) [@dcloud9](https://discuss.hashicorp.com/u/dcloud9)\
**Post date:** [August 3, 2022, 7:15am UTC](https://discuss.hashicorp.com/t/preserve-map-like-values-when-read-from-an-input-file/42715/3 "2022-08-03T07:15:31Z")

</div>

Cracked it.  
With a bit of using `locals{}` and `lookup()`, I can pass both interpolated vars and literal strings. The key to lookup() value is assign a unique-enough keyword like `lookup_` (could be as unique as you like), so it’s developer/user friendly when scaling instances of the module.

TF config:  
main.tf

```auto
locals {
  lookup_map = {
    lookup_region = var.region
    lookup_ddb_table_name = aws_dynamodb_table.this.name
  }
}

...
  environment_variables = {
    for k, v in each.value["environment_variables"] : k => lookup(local.lookup_map, v, v)
  }

```

variables.tf

```auto
variable "api_names" {
  description = "(Optional) Map of API names in Appsync data source"
  type = map(any)
  default = {}
}

```

terraform.tfvars

```auto
api_names = {
  create_foo = {
    field_name = "createFoo"
    type_name = "Mutation"
    environment_variables = {
      REGION = "lookup_region"
      foo = "bar"
      TABLE = "lookup_ddb_table_name"
    }
  }
}

```

TF plan output:

```auto
      + environment {
          + variables = {
              + "REGION" = "eu-west-2" #interpolated from lookup_map
              + "foo" = "bar"
              + "TABLE" = "baz-ddb-table" #interpolated from lookup_map
            }
        }

```

Now, I can just reuse/repeat the module using `for_each`, happy days!
