此 object 没有名为 "az" 的属性

This object does not have an attribute named "az"

我是 terraform 的新手,需要一些帮助。我有一些基本配置来构建一个 VPC 和两个带有实例的子网。当我做了一个 'terraform apply' 时,这个 运行 成功了。现在 运行 一个 terraform destroy 并在标题中出现错误。即使 运行 terraform 计划查看是否有任何更改也会抛出相同的错误。完整的错误说

      each.value is object with no attributes 
This object does not have an attribute named "az".

我猜我没有做对与 'for_each' 功能有关的事情。但后来我不确定它是如何成功应用的。我查过了,apply创建的资源还在。

main.tf

resource "aws_subnet" "iperf_subnet" {
  vpc_id = aws_vpc.ireland_vpc.id
  for_each = var.private_subnets
  cidr_block = each.value.subnet
  availability_zone = each.value.az
}

variables.tf

variable "private_subnets" {
  type = map(object({}))
}

exercise.tfvars

private_subnets = {
    host_a = {
        az = "eu-west-1a"
        subnet = "172.30.1.0/25"
        }      
    host_b = {
        az = "eu-west-1b"
        subnet = "172.30.1.128/25"
        }
}

您将变量 private_subnets 的类型指定为 map(object({}))。由于 object 没有明确指定任何属性,Terraform 将抛出错误。

您应该将其更改为 map(map(string))map(object({az = string, subnet = string})) 如果您想要更具体。