azureazureservicebusazure-servicebus-queuesazure-bicepazure-deployment

Bicep: deploy a Service Bus Queue while the Service Bus in another resource group


I have a LA deployment that require adding a SB queue. However the the SB is located in another resource group than the LA.

So, when I call the existing the SB I use the following code:

resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2017-04-01' existing = {
  name: serviceBusName
  scope: resourceGroup(serviceBusRg)
}

and the queue code:

resource serviceBusNamespaceName_serviceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2024-01-01' = {
  parent: serviceBusNamespace
  name: serviceBusQueueName  
  properties: {
    lockDuration: 'PT5M'
    maxSizeInMegabytes: 1024
    requiresDuplicateDetection: true
    requiresSession: false
    defaultMessageTimeToLive: 'P14D'
    deadLetteringOnMessageExpiration: true
    enableBatchedOperations: true
    duplicateDetectionHistoryTimeWindow: 'PT30M'
    maxDeliveryCount: 10
    status: 'Active'
    autoDeleteOnIdle: 'P10675199DT2H48M5.4775807S'
    enablePartitioning: false
    enableExpress: false
  }
}

When I have the scope written in the SB name space, I am getting an error that:

A resource's computed scope must match that of the Bicep file for it to be deployable. This resource's scope is computed from the "scope" property value assigned to ancestor resource "serviceBusNamespace". You must use modules to deploy resources to a different scope.

So, what is the solution for this problem?

Thank you in advance


Solution

  • A deployment is tied to a resource group (default scope). If you want to deploy a resource in another resource group, you need to create a new deployment. In bicep this is done using module (modules convert to inner/sub deployment):

    1. Create a module to create the servicebus queue:
    //servicebus-queue.bicep
    param serviceBusName string
    param serviceBusQueueName string
    
    // get a reference to an existing resource
    resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2017-04-01' existing = {
      name: serviceBusName
    }
    
    resource serviceBusNamespaceName_serviceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2024-01-01' = {
      parent: serviceBusNamespace
      name: serviceBusQueueName  
      properties: {
        lockDuration: 'PT5M'
        maxSizeInMegabytes: 1024
        requiresDuplicateDetection: true
        requiresSession: false
        defaultMessageTimeToLive: 'P14D'
        deadLetteringOnMessageExpiration: true
        enableBatchedOperations: true
        duplicateDetectionHistoryTimeWindow: 'PT30M'
        maxDeliveryCount: 10
        status: 'Active'
        autoDeleteOnIdle: 'P10675199DT2H48M5.4775807S'
        enablePartitioning: false
        enableExpress: false
      }
    }
    
    1. Invoke this module from your main bicep with a different scope:
    // main.bicep
    param serviceBusRg string
    param serviceBusName string
    param serviceBusQueueName string
    
    // Create SB queue in another resource group
    module  servicebusQueue 'servicebus-queue.bicep' = {
      // specify scope to deploy the queue in another resource group
      scope: resourceGroup(serviceBusRg) 
      params: {
        serviceBusName: serviceBusName
        serviceBusQueueName: serviceBusQueueName
      }
    }