Reference map in locals in terraform file
local
blocks,interpolations and expressions that are not constants cannot be used in terraform.tfvars
file.
See github-issue for further dicussion
The way around is to define the variable only once in terraform.tfvars
and make the duplicate variables local in the terraform module file.
Example:
variable.tf
variable var1 {
type = "map"
}
terraform.tfvars
var1= {
"key1" = "value1",
"key2" = "value2"
}
module.tf
locals {
var2="${var.var1}"
}
output show_var2 {
value = "${local.var2}"
}
Related videos on Youtube
Oliver
Software development for me is a craft. I like clean (as in "Clean Coder") code, separation of concerns, DRY, KISS, self-documenting code, robust and extendable code, refactoring often so that the above can be achieved with agility and speed, and test based development with high degree of code coverage so maintenance can be done with confidence. I like to know my tools well, whether they be programming languages, libraries, build tools, documentation generation tools, test tools, etc. I like to keep up to date on what is coming up in the next release of a programming language, seek out dependable libraries and locate summaries of best practices so that I can base my work on solid foundations and, when possible, on the shoulders of giants. Guiding all of the above is user experience, usefulness, and client happiness. I don't mind taking the time to patiently, calmly, and respectfully explain issues to a client until all requirements have a clear, affordable purpose and minimal complexity. As my contribution to the online community, I maintain three open source projects (I have many others that I no longer maintain but these 3 have stood the test of time): on github I have PyPubSub to support publish-subscribe architecture in Python (this use to be on SourceForge); on SourceForge I have lua-icxx to embed Lua in C++ without having to use the Lua stack, and IOF to provide printf-like formatted output via C++ streams. I am now in the process of learning Google Cloud Platform (incl. cluster orchestration via Kubernetes; Docker, networking, big data tools like Spanner and BigQuery, etc) and backend technologies like nodejs and go. Programming languages I most used before transitioning to cloud stack: C++ (including templates) Python Lua C# Domains of expertise: Machine and business process modeling and simulation Virtual Reality Publish-subscribe (i.e. Event-based) and Distributed architectures Skillfull at: System Architecture and Design Coding Automation of testing and deployment Distributed computing (interprocess communication, network packet analysis) User experience, 3D graphics (VR/AR systems)
Updated on September 16, 2022Comments
-
Oliver over 1 year
In a tvfars file I have this:
locals { common = { "my key" = "value" } }
because I want to use the map in multiple places in that file. I read the terraform docs about variables and I cannot find the correct syntax. I tried the following (var1 and 2 are both declared as maps):
With
var1 = "${local.common}" var2 = "${local.common}"
I get
variable "var1" should be type map, got string
With
var1 = locals.common var2 = locals.common
I get
invalid value "myfile.auto.tfvars" for flag -var-file-default: Error parsing myfile.auto.tfvars: At 18:15: Unknown token: 18:15 IDENT locals.common
With
var1 = {"${local.common}"} var2 = {"${local.common}"}
which fails without an error message but a print of terraform help and terraform exits.
I verified that everything works fine if I copy/paste the map multiple times:
var1 = { "my key" = "value" } var2 = { "my key" = "value" }
Anyone know correct syntax?
-
Oliver over 5 yearsThis shows that I cannot use locals in tfvars, but once that is fixed, a missing piece is what the syntax would be for map. Any chance you could expand your answer to include that? I think you just need to identify the contents of the tfvars file and the variables.tf where the map would be declared.
-
Oliver over 5 yearsI doubt this will work because var2 is a map too yet you are assigning a string to it. Also would it not be a lot simpler to declare common in variable.tf and give it a value in the tfvars, and define var1 and var2 in module.tf to be ${var.common}?
-
keety over 5 years@Oliver not sure what you mean by "var2 is map .... assigning a string"? var1 can be assigned to var2 .i.e a map variable can be assigned to another variable in local block . If you want an additional
common
yeah sure but it has more to do with individual preference & style .