I have existing ServiceBus topics and i want to reference their subscriptions in terraform. The question is how to do it when i have multiple topics and i wnat to build reference to subscription in each topic?
data "azurerm_servicebus_topic" "subscribe_topics" {
for_each = local.subscribe_topics
name = each.value
namespace_id = data.azurerm_servicebus_namespace.sb.id
}
data "azurerm_servicebus_subscription" "subscriptions" {
topic_id = // how to iterate over topic ids from data block above?
name = local.sb_subscription_name
}
locals {
subscribe_topics = [
"topic1",
"topic2",
"topic3"
]
sb_subscription_name = "xyz"
}
2
Answers
The first thing I notice is your
subscribe_topics
is not suitable for a loop, if you try it like that it will give you an error:the fix is easy
for_each = toset(local.subscribe_topics)
I don’t have those resources to confirm, but all you need is to loop over the topic:
and another way
I do agree with @Helder Sepulveda and adding
output
block for clear vision of the service bus topics and understanding the configuration. The deployment was successful with the below terraform code as shown.Deployment successful: