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"
}
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:
Error: Invalid for_each argument
The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type tuple.
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:
data "azurerm_servicebus_topic" "subscribe_topics" {
for_each = toset(local.subscribe_topics)
name = each.value
namespace_id = data.azurerm_servicebus_namespace.sb.id
}
data "azurerm_servicebus_subscription" "subscriptions" {
for_each = data.azurerm_servicebus_topic.subscribe_topics
topic_id = each.value.id
name = local.sb_subscription_name
}
locals {
subscribe_topics = [
"topic1",
"topic2",
"topic3"
]
sb_subscription_name = "xyz"
}
and another way
data "azurerm_servicebus_topic" "subscribe_topics" {
for_each = toset(local.subscribe_topics)
name = each.value
namespace_id = data.azurerm_servicebus_namespace.sb.id
}
data "azurerm_servicebus_subscription" "subscriptions" {
for_each = toset(local.subscribe_topics)
topic_id = data.azurerm_servicebus_topic.subscribe_topics[each.key].id
name = local.sb_subscription_name
}
locals {
subscribe_topics = [
"topic1",
"topic2",
"topic3"
]
sb_subscription_name = "xyz"
}