使用 for_each 引用资源实例

Referencing instances of a resource using for_each

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

请看下面的代码:

这将查看变量 is_production 并决定命名空间是“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"
  }
]

您可以尝试添加 tostring() 功能吗? (terraform docs)

tostring([ for queue in azurerm_servicebus_namespace.service_bus_namespace_blue: queue.id ])

我认为您正在返回列表或其他内容中的值,使用 tostring 函数将转换任何值。如果它不能转换值(例如,如果它是一个元组),它将抛出一个错误。

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

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