使用多级地图时,无法加载 .tfvars 中的 Terraform 值

使用多级地图时,无法加载 .tfvars 中的 Terraform 值

当我尝试使用多层地图时,我在“地形规划”中遇到了以下错误.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.

答案1

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

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

}

相关内容