skip to Main Content

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


  1. 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"
    }
     
    
    Login or Signup to reply.
  2. When I have multiple topics and I want to build reference to subscription in each topic, how to achieve it:

    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.

    provider "azurerm" {
      features {}
    }
    
    locals {
      subscribe_topics = [
        "topic1",
        "topic2",
        "topic3"
      ]
    
      sb_subscription_name = "subsj"
    }
    data "azurerm_resource_group" "example" {
        name = "xxxx"
    }
    
    data "azurerm_servicebus_namespace" "example" {
        name = "newsbxyz"
        resource_group_name = data.azurerm_resource_group.example.name
    }
    
    data "azurerm_servicebus_topic" "subscribe_topics" {
      for_each     = toset(local.subscribe_topics)
    
      name         = each.value
      namespace_id = data.azurerm_servicebus_namespace.example.id
      depends_on = [ data.azurerm_servicebus_namespace.example ]
    }
    
    data "azurerm_servicebus_subscription" "subscriptions" {
      for_each = data.azurerm_servicebus_topic.subscribe_topics
    
      topic_id = data.azurerm_servicebus_topic.subscribe_topics[each.key].id
      name     = local.sb_subscription_name
      depends_on = [ data.azurerm_servicebus_topic.subscribe_topics ]
    }
    
    output "subscriptions_output" {
      value = data.azurerm_servicebus_subscription.subscriptions
    }
    

    Deployment successful:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search