Hi all,
Using terraform 1.0.5:
$ terraform --version
Terraform v1.0.5
on linux_amd64
Your version of Terraform is out of date! The latest version
is 1.0.7. You can update by downloading from https://www.terraform.io/downloads.html
1.0.7 is not yet available on my system.
Using that input file:
platforms_desc = {
fake_main = {
name = "fake_main",
is_root = true,
is_main = true
},
fake_sub = {
name = "fake_sub",
}
}
is_main does not use quotes, I would expect that data shall contain a boolean value.
In that main.tf:
variable "platforms_desc" {
type = map(any)
description = ""
default = {}
}
module "input_data_mgmt" {
source = "./module"
platforms_description = var.platforms_desc
}
output "main_platform" {
value = module.input_data_mgmt.main_platform
}
With that code in module/main.tf:
variable "platforms_description" {
type = map(any)
description = ""
default = {}
}
locals {
// NOTE: to get at least root folder for later uses
main_platform = {
for name, object in var.platforms_description : name => {
name = name,
is_root = lookup(object, "is_root", false)
is_main = object.is_main
} if lookup(object, "is_main", false) == "true"
}
}
output "main_platform" {
value = local.main_platform
}
When trying the value of is_main the test must be done using string format
I get that output:
$ terraform apply -var-file=input.tfvars
Changes to Outputs:
+ main_platform = {
+ fake_main = {
+ is_main = "true"
+ is_root = "true"
+ name = "fake_main"
}
}
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
In this output the is_main data (as “is_root”) is shown as a string.
Removing the quotes from the test as follow:
locals {
// NOTE: to get at least root folder for later uses
main_platform = {
for name, object in var.platforms_description : name => {
name = name,
is_root = lookup(object, "is_root", false)
is_main = object.is_main
} if lookup(object, "is_main", false) == true
}
}
Gives the following output:
terraform apply -var-file=input.tfvars
Changes to Outputs:
+ main_platform = {}
You can apply this plan...
Here main_platform is {} empty as the test on boolean didn’t matched any object.
Changing
type = map(any)
to
type = map(object({
name = string,
is_root = bool,
is_main = bool
}))
in both variables declaration (root + module), terraform complains with the following:
The given value is not valid for variable "platforms_desc": element "fake_sub": attributes "is_main" and "is_root" are required.
So, using that syntax removes the possibility to not declare each and every element of objects. And this simplify drastically input files.
So, I’m puzzled.
Is it safe, official, to consider all variables contents as strings, always? Or perhaps only once the variable is passed to some module? I did not made this test yet but tests may be numerous…
Best regards,
mathias