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:
REGION=var.region
FOO="bar"
main.tf
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], "\"") }
}
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.
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
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
variable "api_names" {
description = "(Optional) Map of API names in Appsync data source"
type = map(any)
default = {}
}