Terraform Values from .tfvars are not loading when using Multi Level Maps
I am getting below errors on "terraform plan" when trying to use a multi-level map with .tfvar
. I request you to help me correct my .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"
}
}
}
variable.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"
}
}
}
main.tf
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
}
}
when I do terraform plan, I see this issue
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.
Solution 1:
You don't need default
in your .tfvars
. Thus it should be:
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"
}
}