首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多级映射时,来自.tfvars的Terraform值不加载

使用多级映射时,来自.tfvars的Terraform值不加载
EN

Server Fault用户
提问于 2021-05-13 05:58:39
回答 1查看 3.5K关注 0票数 0

当我试图使用带有.tfvar的多级地图时,我会在“地形图”上出现以下错误。我请求你帮我改正我的.tfvars

.tfvars

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

variable.tf

代码语言: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"
       }
  }
}

main.tf

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

代码语言:javascript
复制
╷
│ 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.
EN

回答 1

Server Fault用户

回答已采纳

发布于 2021-05-13 06:04:37

您不需要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"
       },

    test-vm3 = {
        instance_name  = "test-vm2"
        instance_image = "debian-cloud/debian-9"
        instance_type =  "f1-micro"
       }

}
票数 3
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1063395

复制
相关文章

相似问题

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