Terraform 多层地图
Terraform Multi Level Maps
我在尝试使用多级地图时收到有关“terraform plan”的错误。我无法使用 google_compute_instance
模块中的地图。我的目标是使用单个 google_compute_instance
模块启动具有不同属性的多个虚拟机。我请求你帮助我纠正我的 main.tf 或建议我实现我的目标
Variable.tf
variable "instance_config" {
type = list(map(string))
default = []
}
.tfvars
instance_config = {
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"
}
}
main.tf
resource "google_compute_instance" "vm_instance" {
{ for instance_name, instance_type, instance_image in var.instance_config :
name = instance_config.instance_name
machine_type = instance_config.instance_type
tags = var.instance_tags
boot_disk {
initialize_params {
image = instance_config.instance_image
}
}
}
network_interface {
network = "${var.gcp_network}"
}
}
您 for_each
的语法不正确,您的 instance_config
格式错误。应该是:
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”的错误。我无法使用 google_compute_instance
模块中的地图。我的目标是使用单个 google_compute_instance
模块启动具有不同属性的多个虚拟机。我请求你帮助我纠正我的 main.tf 或建议我实现我的目标
Variable.tf
variable "instance_config" {
type = list(map(string))
default = []
}
.tfvars
instance_config = {
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"
}
}
main.tf
resource "google_compute_instance" "vm_instance" {
{ for instance_name, instance_type, instance_image in var.instance_config :
name = instance_config.instance_name
machine_type = instance_config.instance_type
tags = var.instance_tags
boot_disk {
initialize_params {
image = instance_config.instance_image
}
}
}
network_interface {
network = "${var.gcp_network}"
}
}
您 for_each
的语法不正确,您的 instance_config
格式错误。应该是:
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
}
}