gpt4 book ai didi

azure - 使用 for_each 引用资源实例

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

我正在为服务总线命名空间创建蓝/绿部署。我使用一个局部变量来定义服务总线是“高级”还是“标准”sku。这两个命名空间创建得很好,但我遇到的问题是当我想引用这些命名空间的特定实例时,以使用服务总线命名空间创建队列。

请参阅下面的代码:

这将查看变量 is_product 并决定命名空间是“Premium”还是“Standard”

locals {
sb_sku = var.is_production ? ["Premium"] : ["Standard"]
}

这是命名空间创建代码:

resource "azurerm_servicebus_namespace" "service_bus_namespace_blue" {
for_each = { for sku in local.sb_sku : sku => sku }

name = format("company-sb-%s-gwc-%s-siem-%s-blue", var.location_short, var.client_name, terraform.workspace)
location = var.resource_group_location
resource_group_name = var.resource_group_name
sku = each.key

tags = var.tags

}

resource "azurerm_servicebus_namespace" "service_bus_namespace_green" {
for_each = { for sku in local.sb_sku : sku => sku if var.is_production != false }

name = format("company-sb-%s-gwc-%s-siem-%s-green", var.location_short, var.client_name, terraform.workspace)
location = var.resource_group_location
resource_group_name = var.resource_group_name
sku = each.key

tags = var.tags

}

最后是我发现问题的代码:

resource "azurerm_servicebus_queue" "service_bus_queue_blue" {
for_each = { for queue in var.servicebus_queue_config : queue.name => queue }

namespace_id = [ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ] <<<< This is the offender
name = each.value.name
dead_lettering_on_message_expiration = each.value.dead_lettering_on_message_expiration
max_size_in_megabytes = each.value.max_size_in_megabytes
max_delivery_count = each.value.max_delivery_count
lock_duration = each.value.lock_duration
requires_duplicate_detection = each.value.requires_duplicate_detection
duplicate_detection_history_time_window = each.value.duplicate_detection_history_time_window

}

resource "azurerm_servicebus_queue" "service_bus_queue_green" {
for_each = { for queue in var.servicebus_queue_config : queue.name => queue if var.is_production != false }

namespace_id = azurerm_servicebus_namespace.service_bus_namespace_green["Premium"].id
name = each.value.name
dead_lettering_on_message_expiration = each.value.dead_lettering_on_message_expiration
max_size_in_megabytes = each.value.max_size_in_megabytes
max_delivery_count = each.value.max_delivery_count
lock_duration = each.value.lock_duration
requires_duplicate_detection = each.value.requires_duplicate_detection
duplicate_detection_history_time_window = each.value.duplicate_detection_history_time_window

}

这是我收到的错误:

│ Error: Incorrect attribute value type

│ on modules/service-bus/main.tf line 32, in resource "azurerm_servicebus_queue" "service_bus_queue_blue":
│ 32: namespace_id = [ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ]
│ ├────────────────
│ │ azurerm_servicebus_namespace.service_bus_namespace_blue is object with 1 attribute "Standard"

│ Inappropriate value for attribute "namespace_id": string required.

servicebus_queue_config 输入:

variable "servicebus_queue_config" {
type = list(object({
name = string
dead_lettering_on_message_expiration = bool
max_size_in_megabytes = number
max_delivery_count = number
lock_duration = string
requires_duplicate_detection = bool
duplicate_detection_history_time_window = string
}))
}

# whats passed in from the tfvars

servicebus_queue_config = [
{
name = "queuename1",
dead_lettering_on_message_expiration = true,
max_size_in_megabytes = 1024,
max_delivery_count = 10,
lock_duration = "PT3M30S",
requires_duplicate_detection = true,
duplicate_detection_history_time_window = "P7D"
},
{
name = "queuename2",
dead_lettering_on_message_expiration = true,
max_size_in_megabytes = 1024,
max_delivery_count = 10,
lock_duration = "PT3M30S",
requires_duplicate_detection = true,
duplicate_detection_history_time_window = "P7D"
}
]

最佳答案

在这一行中,您正在传递一个列表:

namespace_id = [ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ]

这是一个只有一个元素的列表,但它仍然是一个列表。 namespace_id 应该是一个字符串。

您可以直接引用它们,而不是尝试在任何地方使用 for 循环来引用值:

namespace_id = azurerm_servicebus_namespace.service_bus_namespace_blue[local.sb_sku].id

关于azure - 使用 for_each 引用资源实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70955355/

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