给定的 "for_each" 参数值不合适

The given "for_each" argument value is unsuitable

我是 Terraform 的新手,我正在使用 Terraform 构建 Azure 基础结构。

这是我的 main.tf 文件。

module "azResourceGroup" {
  source              = "./Modules/ResourceGroup"
  resource_group_name = var.resource_group_name
  tags                = var.tags
}

module "azStorageAccount" {
  source              = "./Modules/StorageAccount"
  resource_group_name = var.resource_group_name
  tags                = var.tags

  for_each                  = var.storage_account_list
  storage_account_name      = each.value.storage_account_name
  account_kind              = each.value.account_kind
  account_tier              = each.value.storage_account_tier
  enable_https_traffic_only = each.value.enable_https_traffic_only
}

这是用于存储帐户的变量(来自 variables.tf 文件):

variable "storage_account_list" {
  type = list(object({
    storage_account_name      = string
    account_tier              = string
    account_kind              = string
    enable_https_traffic_only = bool
  }))
}

下面是变量的值(来自 terraform.tfvars 文件)

storage_account_list = [
  {
    storage_account_name      = "my-storage"
    account_tier              = "Standard"
    account_kind              = "BlobStorage"
    enable_https_traffic_only = false
  }
]

terraform plan 命令中,出现以下错误。请帮我解决这个问题。

 Error: Invalid for_each argument
│
│   on main.tf line 13, in module "azStorageAccount":
│   13:   for_each                  = var.storage_account_list
│     ├────────────────
│     │ var.storage_account_list is list of object with 1 element
│
│ The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have
│ provided a value of type list of object.

您不能在 for_each 中使用对象列表。只有字符串列表(没有其他类型)或映射。因此,在您的情况下,您可以将列表更改为地图,如下所示:

module "azStorageAccount" {
  source              = "./Modules/StorageAccount"
  resource_group_name = var.resource_group_name
  tags                = var.tags

  for_each                  = {for idx, val in var.storage_account_list: idx => val}

  storage_account_name      = each.value.storage_account_name
  account_kind              = each.value.account_kind
  account_tier              = each.value.storage_account_tier
  enable_https_traffic_only = each.value.enable_https_traffic_only
}