gpt4 book ai didi

azure - Terraform:即使添加了第 6 个和第 7 个属性,each.value 也是具有 5 个属性的对象

转载 作者:行者123 更新时间:2023-12-03 06:25:19 25 4
gpt4 key购买 nike

我正在尝试将几个设置(is_hns_enabled 和 account_kind)添加到 2 个现有存储帐户,同时创建第三个。 2个exisint只需要明确说明这些设置的默认值。在这两个新设置上,所有 3 个帐户都遇到以下错误。这是错误的 1 个实例

错误:

│ Error: Invalid index

│ on storage.tf line 48, in resource "azurerm_storage_account" "storage-account":
│ 48: is_hns_enabled = each.value["is_hns_enabled"]
│ ├────────────────
│ │ each.value is object with 5 attributes

│ The given key does not identify an element in this collection value.

存储帐户已存在,但我需要明确设置此设置以将其与其他帐户区分开。我将其设置为 false,这已经是默认值。

地形:

locals {
containers = merge([
for account, account_details in var.storage_accounts : {
for container in account_details.containers :
"${account}-${container["name"]}" => {
"name" = container["name"]
"access_type" = container["access_type"]
"storage_account_name" = account
}
}
]...)
roles = merge([
for account, account_details in var.storage_accounts : {
for role in account_details.roles :
"${account}-${role["objectId"]}" => {
"storage_account_name" = account
"objectId" = role["objectId"]
"storage_role" = role["storage_role"]
}
}
]...)
}


resource "azurerm_storage_account" "storage-account" {
for_each = var.storage_accounts
name = substr(replace(lower(each.key), "/\\W|_|\\s/", ""), 0, 24)
resource_group_name = var.azure["resource_group_name"]
location = var.azure["location"]
account_tier = each.value["account_tier"]
account_kind = each.value["account_kind"]
is_hns_enabled = each.value["is_hns_enabled"]
account_replication_type = each.value["account_replication_type"]

blob_properties {
versioning_enabled = each.value["versioning_enabled"]
}
}

TF,存储变量:

storage_accounts = {
acct1 = {
account_tier = "Premium"
account_kind = "BlockBlobStorage"
account_replication_type = "RAGRS"
versioning_enabled = false
is_hns_enabled = false
roles = []
containers = [
{
name = "container1"
access_type = "private"
}
]
}
}

最佳答案

The given key does not identify an element in this collection value.

解决此问题后,我发现了以下方法:

方法 1:

您可以包含coalesce()函数。函数coalesce() in terraform 返回其参数中的第一个非空值。因此,如果 each.value["is hnsenabled"] 为 null 或未定义,则将使用默认值 false

is_hns_enabled = coalesce(each.value["is_hns_enabled"],false)

进行一些更改后,您的代码如下所示。

resource "azurerm_storage_account" "storage-account" {
for_each = var.storage_accounts
name = substr(replace(lower(each.key), "/\\W|_|\\s/", ""), 0, 24)
resource_group_name = "Jahnavi"
location = "eastus"
account_tier = each.value["account_tier"]
account_kind = each.value["account_kind"]
is_hns_enabled = coalesce(each.value["is_hns_enabled"],false)
account_replication_type = each.value["account_replication_type"]

blob_properties {
versioning_enabled = each.value["versioning_enabled"]
}
}

方法 2:

或者,如果您尝试获取现有的存储帐户并设置所需的属性,请使用 data block 获取现有的存储帐户。

data "azurerm_storage_account" "example"{
name = "storageaccount"
location="eastus"
}

Terraform 已成功初始化且配置验证成功。

enter image description here

enter image description here

关于azure - Terraform:即使添加了第 6 个和第 7 个属性,each.value 也是具有 5 个属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75593687/

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