terraform 12 嵌套映射
terraform 12 nested mapping
只是想请您帮忙,我的 main.tf 文件如何取 "labrador" 的值] 来自 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]]
}
我想执行它并获取正确的值并使用此命令分配给 lambda 模块:
terraform plan -var-file=variables.tf -var "TYPE=dog" -var "BREED=outdoor"
但是我遇到了这个问题:
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"
您的问题在:
var.PETS[var.TYPE[var.BREED]]
应该是:
var.PETS[var.TYPE][var.BREED]
这是一个例子:
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
只是想请您帮忙,我的 main.tf 文件如何取 "labrador" 的值] 来自 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]]
}
我想执行它并获取正确的值并使用此命令分配给 lambda 模块:
terraform plan -var-file=variables.tf -var "TYPE=dog" -var "BREED=outdoor"
但是我遇到了这个问题:
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"
您的问题在:
var.PETS[var.TYPE[var.BREED]]
应该是:
var.PETS[var.TYPE][var.BREED]
这是一个例子:
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