I am trying to assign RBAC role 'Azure Service Bus Data Receiver' to an Azure Service Bus Queue for a Function App by using Bicep. I have done this before just fine with Storage Account Queues but this is giving me some problems now with the Service Bus Queues.
Here is my Bicep so far:
var globalServiceBusName = 'sbns-global-nonprod'
var globalServiceBusQueueName = 'sbq-queue01'
resource serviceBusReceiverRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
scope: subscription()
name: '4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0' // Azure Service Bus Data Receiver
}
// Get a reference to a Service Bus in another Resource Group
resource globalServiceBus 'Microsoft.ServiceBus/namespaces@2021-11-01' existing = {
name: globalServiceBusName
}
// Get a reference to a Service Bus Queue in another Resource Group
resource globalServiceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2022-10-01-preview' existing = {
parent: globalServiceBus
name: globalServiceBusQueueName
}
// Assign RBAC role 'Azure Service Bus Data Receiver' to the Service Bus queue
resource RBACAzureServiceBusDataReceiver 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resourceGroup().id, functionApp.id, globalServiceBusQueue.id, serviceBusReceiverRoleDefinition.id)
scope: globalServiceBusQueue
properties: {
principalId: functionApp.identity.principalId
roleDefinitionId: serviceBusReceiverRoleDefinition.id
principalType: 'ServicePrincipal'
}
}
The ARM validation goes fine, but when it deploys it gives me the error:
"Can not perform requested operation on nested resource. Parent resource 'sbns-global-nonprod' not found."
When I output the names of the resources globalServiceBus with "globalServiceBus.name" and globalServiceBusQueue with "globalServiceBusQueue.name", I can see the actual names which means the Bicep has a reference to them.
Edit: This code works when I move the Service Bus and its queue to the same Resource Group where my Function App resides. So all resources are in 1 RG. Then it works. My desire is to have 1 shared RG where shared resources can be placed, like a Service Bus in Premium Tier, which can be used by Queue receivers like Function App from other RGs.
EDIT: I have tried this code as well:
resource serviceBusNamespace 'Microsoft.ServiceBus/namespaces@2021-11-01' existing = {
scope: resourceGroup(globalResourceGroupName)
name: globalServiceBusName
}
// Get a reference to global Service Bus Queue used for User Migration
resource serviceBusQueue 'Microsoft.ServiceBus/namespaces/queues@2022-10-01-preview' existing = {
name: globalServiceBusQueueName
parent: serviceBusNamespace
}
// Assign RBAC role 'Azure Service Bus Data Receiver' to the Service Bus queue
resource RBACAzureServiceBusDataReceiver 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resourceGroup().id, functionApp.id, serviceBusQueue.id, serviceBusReceiverRoleDefinition.id)
scope: serviceBusQueue // throws error here: A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope.bicep(BCP139)
properties: {
principalId: functionApp.identity.principalId
roleDefinitionId: serviceBusReceiverRoleDefinition.id
principalType: 'ServicePrincipal'
}
}
But I get at an error: "A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope.bicep(BCP139)"