I've got two bicep files, main.bicep and webAppRoleAssignment.bicep
main.bicep
....
module webAppRoleAssignment 'webAppRoleAssignment.bicep' = {
name: 'webAppRoleAssignment'
scope: az.resourceGroup('123', 'rg-name')
params: {
containerRegistryName: containerRegistryName
webAppIdentityId: webAppIdentity.id
webAppIdentityPrincipalId: webAppIdentity.properties.principalId
}
}
webAppRoleAssignment.bicep
@description('Role definition ID for the role ACRPull that is assigned to the
UserAssignedIdentity')
resource acrPullRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-05-01- preview' existing = {
scope: subscription()
name: 'role_name'
}
@description('Existing Container Registry in the same Resource Group')
resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-11-01-preview' existing = {
scope: az.resourceGroup('123', 'rg-name')
name: containerRegistryName
}
@description('ACRPull role assignment to the Container App User Assigned Identity. Needed to pull images from the Container Registry')
resource webAppRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: containerRegistry
name: guid(containerRegistry.id, webAppIdentityId)
properties: {
principalId: webAppIdentityPrincipalId
roleDefinitionId: acrPullRoleDefinition.id
principalType: 'ServicePrincipal'
}
}
When I try to run this command az bicep build --file .\main.bicep with the code above I get this 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.
The error points to the scope (containerRegistry) of the "webAppRoleAssignment" resource (webAppRoleAssignment.bicep).
BUT if I change the container registry resource (webAppRoleAssignment.bicep) like this:
@description('Existing Container Registry in the same Resource Group')
resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-11-01-preview' existing = {
scope: az.resourceGroup() //removed resource group id and sub name
name: containerRegistryName
}
everything works!! And I do not understand why, because the scope pointed by az.resourceGroup() or az.resourceGroup('123', 'rg-name') should be the same.
Any suggestions?

