Reputation: 765
just want to ask for your help on how I can my main.tf file possibly take the value of "labrador" from the nested variables from variables.tfvars
variables.tfvars
PETS = {
dog = {
indoor = "poodle"
outdoor = "labrador"
others = "bulldog"
}
cat = {
indoor = "siamese"
outdoor = "persian"
others = "bengal"
}
}
variables.tf
variable PETS { type = map }
main.tf
module "lambda-module" {
source = "../../module/lambda-module"
PETS = var.PETS[var.TYPE[var.BREED]]
}
I would like to execute it and take the proper values and assign to the the lambda-module using this command:
terraform plan -var-file=variables.tf -var "TYPE=dog" -var "BREED=outdoor"
however I'm getting this issue:
Error: Invalid index
on main.tf line 3, in module "lambda-module":
3: PETS = var.PETS[var.TYPE[var.BREED]]
|----------------
| var.TYPE is "dog"
| var.BREED is "outdoor"
Upvotes: 1
Views: 822
Reputation: 17594
Your issue is in:
var.PETS[var.TYPE[var.BREED]]
that should be:
var.PETS[var.TYPE][var.BREED]
Here is an example:
variable PETS {
type = map
default = {
"dog" = {
indoor = "poodle"
outdoor = "labrador"
others = "bulldog"
},
"cat" = {
indoor = "siamese"
outdoor = "persian"
others = "bengal"
}
}
}
variable TYPE {
type = string
}
variable BREED {
type = string
}
output "TYPE_BREED" {
value = var.PETS[var.TYPE][var.BREED]
}
terraform apply -var "TYPE=dog" -var "BREED=outdoor"
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
TYPE_BREED = labrador
terraform apply -var "TYPE=cat" -var "BREED=indoor"
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
TYPE_BREED = siamese
Upvotes: 1