Search code examples
azureazureservicebusazure-resource-managerazure-bicep

Azure Bicep: Create ServiceBus subscription in existing namespace from another Resource Group


I have an existing ServiceBus namespace and Topic in Resource Group A, and I am working on a new bicep template that will deploy resources to Resource Group B, in this template I have to create a new Subscription to the Topic that is in Resource Group A.

I have tried following till now without any success:

param EngineServiceBus string
param EngineTopicName string
param EngineMatasPlusSubscriber string

resource EngineServicebus 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
  name: EngineServiceBus
  scope: resourceGroup(offerEngineRG)
}
resource EngineTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' existing = {
  name: EngineTopicName
  parent: EngineServicebus

}

resource EngineSubscriber 'Microsoft.ServiceBus/namespaces/topics/subscriptions@2022-10-01-preview' = {
  name: '${EngineServicebus.name}/${EngineTopic.name}/${EngineMatasPlusSubscriber}'
}

Solution

  • When invoking this module from your parent bicep file, you need to scopes the module to the resource group.

    In your subscription module, remove the scope:

    // subscription.bicep
    
    param EngineServiceBus string
    param EngineTopicName string
    param EngineMatasPlusSubscriber string
    
    resource EngineServicebus 'Microsoft.ServiceBus/namespaces@2022-10-01-preview' existing = {
      name: EngineServiceBus
    }
    resource EngineTopic 'Microsoft.ServiceBus/namespaces/topics@2022-10-01-preview' existing = {
      name: EngineTopicName
      parent: EngineServicebus
    
    }
    
    resource EngineSubscriber 'Microsoft.ServiceBus/namespaces/topics/subscriptions@2022-10-01-preview' = {
      name: EngineMatasPlusSubscriber
      parent: EngineTopic
    }
    

    Then from the parent, add the scope on the module

    param offerEngineRG string
    param EngineServiceBus string
    param EngineTopicName string
    param EngineMatasPlusSubscriber string
    
    module createSubscription 'subscription.bicep' = {
      name: '...'
      scope: resourceGroup(offerEngineRG) // scope of the deployment
      params: {    
        EngineServiceBus: EngineServiceBus
        EngineTopicName: EngineTopicName
        EngineMatasPlusSubscriber: EngineMatasPlusSubscriber
      }
    }