I am trying to create multiple service bus topics with different subscriptions in azure using terraform.
locals {
servicebus = {
"topic_1" = [{
subscription = ["subscription1", "subscription2", "subscription3"]
}],
"topic_2" = [{
subscription = ["subscription4", "subscription5", "subscription6"]
}],
“topic_3” = [{
subscription = ["subscription7", "subscription8", "subscription9"]
}]
}
service_bus = merge([
for topicname, topic in local.servicebus : {
for subname in topic[0].subscription :
"${topicname}-${subname}" => {
name = topicname
subscription_name = subname
}
}
]…)
}
resource “azurerm_resource_group” “rg” {
name = “demo”
location = “eastus2”
}
############ Creating Servicebus Namespace ###########################
module “servicebus_namespace” {
source = “./servicebus/namespace”
name = “servicebusnamespace-name”
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku = “Standard”
capacity = “0”
}
############ Creating Servicebus Topic ###############################
module “servicebus_topic” {
source = “./servicebus/topic”
for_each = local.service_bus
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
namespace_name = module.servicebus_namespace.name
max_size_in_megabytes = “1024”
depends_on = [module.servicebus_namespace.name]
}
########## Creating Servicebus Subscription ###############################
resource “azurerm_servicebus_subscription” “sbs” {
for_each = local.service_bus
name = each.value.subscription_name
topic_name = module.servicebus_topic[each.value.name].name
namespace_name = module.servicebus_namespace.name
resource_group_name = azurerm_resource_group.rg.name
max_delivery_count = “10”
}
Error:
Error: Invalid index
│
│ on servicebus.tf line 79, in resource “azurerm_servicebus_subscription” “sbs”:
│ 79: topic_name = module.servicebus_topic[each.value.name].name
│ ├────────────────
│ │ each.value.name is “topic_1”
│ │ module.servicebus_topic is object with 9 attributes
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on servicebus.tf line 79, in resource “azurerm_servicebus_subscription” “sbs”:
│ 79: topic_name = module.servicebus_topic[each.value.name].name
│ ├────────────────
│ │ each.value.name is “topic_3”
│ │ module.servicebus_topic is object with 9 attributes
│
│ The given key does not identify an element in this collection value.