gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:15:01 24 4
gpt4 key购买 nike

我是 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 中使用对象列表。仅字符串列表(无其他类型)或 map 。因此,根据您的情况,您可以将列表更改为 map ,如下所示:

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
}

关于azure - 给定的 "for_each"参数值不合适,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71171974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com