尝试将多级地图与.tfvar
. 我请求你帮我纠正我的.tfvars
.tfvars
instance_config = {
default = {
test-vm1 = {
instance_name = "test-vm1"
instance_image = "debian-cloud/debian-8"
instance_type = "n1-standard-4"
},
test-vm2 = {
instance_name = "test-vm2"
instance_image = "debian-cloud/debian-9"
instance_type = "f1-micro"
},
test-vm3 = {
instance_name = "test-vm2"
instance_image = "debian-cloud/debian-9"
instance_type = "f1-micro"
}
}
}
变量.tf
variable "instance_config" {
type = map(object({
instance_name = string
instance_image = string
instance_type = string
}))
default = {
test-vm1 = {
instance_name = "test-vm1"
instance_image = "debian-cloud/debian-8"
instance_type = "n1-standard-4"
},
test-vm2 = {
instance_name = "test-vm2"
instance_image = "debian-cloud/debian-9"
instance_type = "f1-micro"
}
}
}
主文件
resource "google_compute_instance" "vm_instance" {
for_each = var.instance_config
name = each.value.instance_name
machine_type = each.value.instance_type
tags = var.instance_tags
boot_disk {
initialize_params {
image = each.value.instance_image
}
}
network_interface {
network = var.gcp_network
}
}
当我做地形计划时,我看到了这个问题
terraform plan -var-file=profiles/liverpool.tfvars
╷
│ Error: Invalid value for input variable
│
│ on profiles/liverpool.tfvars line 11:
│ 11: instance_config = {
│ 12: default = {
│ 13: test-vm1 = {
│ 14: instance_name = "test-vm1"
│ 15: instance_image = "debian-cloud/debian-8"
│ 16: instance_type = "n1-standard-4"
│ 17: },
│ 18: test-vm2 = {
│ 19: instance_name = "test-vm2"
│ 20: instance_image = "debian-cloud/debian-9"
│ 21: instance_type = "f1-micro"
│ 22: },
│ 23: test-vm3 = {
│ 24: instance_name = "test-vm2"
│ 25: instance_image = "debian-cloud/debian-9"
│ 26: instance_type = "f1-micro"
│ 27: }
│ 28: }
│ 29: }
The given value is not valid for variable "instance_config": list of map of string required.
你不需要
default
在你的.tfvars
. 因此它应该是: