[英]Referencing instances of a resource using for_each
I am creating a blue/green deployment for a service bus namespace.我正在为服务总线命名空间创建蓝/绿部署。 I'm using one local variable to define if the service bus is "Premium" or "Standard" sku.
我使用一个局部变量来定义服务总线是“Premium”还是“Standard”sku。 These two namespaces create fine but the issue I have is when I want to reference the specific instances of these namespaces, to create the queues with the Service Bus Namespaces.
这两个命名空间创建得很好,但我遇到的问题是当我想引用这些命名空间的特定实例时,使用服务总线命名空间创建队列。
Please see code below:请看下面的代码:
This will look at variable is_production and decide if the namespace will be "Premium" or "Standard"这将查看变量 is_production 并决定命名空间是“Premium”还是“Standard”
locals {
sb_sku = var.is_production ? ["Premium"] : ["Standard"]
}
This is the namespace creation code:这是命名空间创建代码:
resource "azurerm_servicebus_namespace" "service_bus_namespace_blue" {
for_each = { for sku in local.sb_sku : sku => sku }
name = format("glasswall-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("glasswall-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
}
Lastly the code where I'm finding the issue:最后是我发现问题的代码:
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
}
This is the error I'm receiving:这是我收到的错误:
│ 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 inputs: 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"
}
]
Could you try and add the tostring()
function?您可以尝试添加
tostring()
function 吗? ( terraform docs ) ( terraform 文档)
tostring([ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ])
I think you are returning a value in a list or something, using the tostring
function will convert any value.我认为您正在返回列表或其他内容中的值,使用
tostring
function 将转换任何值。 If it cannot convert the value (if it's a tuple for example) it will throw an error.如果它不能转换值(例如,如果它是一个元组),它将抛出一个错误。
On this line, you are passing a list:在这一行,您传递了一个列表:
namespace_id = [ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ]
It's a list with only one element, but it's still a list.这是一个只有一个元素的列表,但它仍然是一个列表。
namespace_id
is supposed to be a string. namespace_id
应该是一个字符串。
Instead of trying to use for loops everywhere to reference values, you can simply reference them directly:您可以简单地直接引用它们,而不是尝试在任何地方使用 for 循环来引用值:
namespace_id = azurerm_servicebus_namespace.service_bus_namespace_blue[local.sb_sku].id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.