Azure service bus parent child relationship issue

Hi,

I have created the below modules to create topics and subscriptions for the Azure Service Bus resource.

The issue I am experiencing is although I have used map object types to ensure the indexing does not force a destroy and recreate whenever I update the topic list or Subscription lists, I have noticed that the the creation works fine but any updates to the the topic or subscription lists, the subscriptions are dropped and re-created, this is not due to the indexing, but due to the topic id which shows as being blank at the plan stage on the update. The apply does subsequently correctly set the topic id again.

I don’t want all the subscriptions to be dropped and created, what am I doing wrong ?.. any guidance would be appreciated.


// topics
variable p_servicebus_topics {
  type = map(object({
    default_message_ttl                     = string
    max_size_in_megabytes                   = string
    requires_duplicate_detection            = bool
    duplicate_detection_history_time_window = string
    enable_batched_operations               = bool
    status                                  = string
    support_ordering                        = bool
    auto_delete_on_idle                     = string
    enable_partitioning                     = bool
    enable_express                          = bool
    topic_auth_rules = map(object({
        name = string
        manage  = bool
        listen  = bool
        send    = bool    
        }))                 
    topic_subscriptions = map(object({
        name                                      = string
        lock_duration                             = string
        requires_session                          = bool
        default_message_ttl                       = string
        dead_lettering_on_message_expiration      = bool
        dead_lettering_on_filter_evaluation_error = bool
        max_delivery_count                        = string
        status                                    = string
        enable_batched_operations                 = bool
        auto_delete_on_idle                       = string
         }))
      
    }))

locals {


# Subscriptions
 loc_subscriptions = flatten([
   for idx, val in var.p_servicebus_topics : [
      for subscription in val.topic_subscriptions : {
        key = "${idx}__${subscription.name}"
        subscription_details = {
           topic_name                                = idx 
           name                                      = subscription.name       
           lock_duration                             = subscription.lock_duration
           requires_session                          = subscription.requires_session
           default_message_ttl                       = subscription.default_message_ttl
           dead_lettering_on_message_expiration      = subscription.dead_lettering_on_message_expiration
           dead_lettering_on_filter_evaluation_error = subscription.dead_lettering_on_filter_evaluation_error
           max_delivery_count                        = subscription.max_delivery_count
           status                                    = subscription.status
           enable_batched_operations                 = subscription.enable_batched_operations
           auto_delete_on_idle                       = subscription.auto_delete_on_idle
          }
        }
      ]
  ])

}

resource "azurerm_servicebus_topic" "servicebus_topics" {
 for_each                                   = var.p_servicebus_topics
    namespace_id                            = azurerm_servicebus_namespace.servicebus_ns.id
    name                                    = each.key
    default_message_ttl                     = each.value.default_message_ttl
    max_size_in_megabytes                   = each.value.max_size_in_megabytes
    requires_duplicate_detection            = each.value.requires_duplicate_detection
    duplicate_detection_history_time_window = each.value.duplicate_detection_history_time_window
    enable_batched_operations               = each.value.enable_batched_operations
    status                                  = each.value.status
    support_ordering                        = each.value.support_ordering
    auto_delete_on_idle                     = each.value.auto_delete_on_idle
    enable_partitioning                     = each.value.enable_partitioning
    enable_express                          = each.value.enable_express

  depends_on                                = [azurerm_servicebus_namespace.servicebus_ns]
}

resource "azurerm_servicebus_subscription" "topic_subscriptions" {
  for_each                                    = { for idx, val in local.loc_subscriptions : val.key => val  }
    topic_id                                  = data.azurerm_servicebus_topic.topic_subscriptions[each.key].id
    name                                      = each.value.subscription_details.name
    max_delivery_count                        = each.value.subscription_details.max_delivery_count
    auto_delete_on_idle                       = each.value.subscription_details.auto_delete_on_idle
    default_message_ttl                       = each.value.subscription_details.default_message_ttl
    lock_duration                             = each.value.subscription_details.lock_duration
    dead_lettering_on_message_expiration      = each.value.subscription_details.dead_lettering_on_message_expiration
    dead_lettering_on_filter_evaluation_error = each.value.subscription_details.dead_lettering_on_filter_evaluation_error
    status                                    = each.value.subscription_details.status
    requires_session                          = each.value.subscription_details.requires_session
    enable_batched_operations                 = each.value.subscription_details.enable_batched_operations

  depends_on                                  = [azurerm_servicebus_topic.servicebus_topics]
}