首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Terraform多层地图

Terraform多层地图
EN

Stack Overflow用户
提问于 2021-05-13 13:13:12
回答 1查看 55关注 0票数 1

当我尝试使用多级地图时,我在"terraform plan“上遇到了错误。我无法使用google_compute_instance模块中的地图。我的目标是使用单个google_compute_instance模块启动多个具有不同属性的VM。我请求您帮助我纠正我的main.tf或建议我实现我的目标

Variable.tf

代码语言:javascript
运行
复制
variable "instance_config" {
  type    = list(map(string))
  default = []
}

.tfvars

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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}"
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-13 13:23:37

您的for_each语法不正确,并且您的instance_config格式错误。它应该是:

代码语言:javascript
运行
复制
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
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67514190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档