简体   繁体   中英

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. 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"

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:

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? ( terraform docs )

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. 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.

Instead of trying to use for loops everywhere to reference values, you can simply reference them directly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM