Bicep diagnostic code - BCP139
This diagnostic occurs when you use resource
to deploy resources to a different scope than the target one. You should use module
instead. For more information, see the following articles based on the scope:
- Resource group: Scope to different resource group.
- Subscription: Deployment scopes.
- Management group: Deployment scopes.
- Tenant: Deployment scopes.
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.
Error
To deploy resources to a scope that isn't the target scope, add a module
.
The following example deploys a storage account resource to a different resource group in the same subscription. The example raises the diagnostic because the module
declaration type isn't used:
param otherResourceGroup string
param location string
// resource deployed to a different resource group in the same subscription
resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: uniqueString(resourceGroup().id)
scope: resourceGroup(otherResourceGroup)
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
You can fix the diagnostic by using the module
declaration type:
param otherResourceGroup string
// module deployed to a different resource group in the same subscription
module exampleModule 'module.bicep' = {
name: 'deployStorageToAnotherRG'
scope: resourceGroup(otherResourceGroup)
}
The following example deploys a resource group to a different subscription. The example raises the diagnostic because module
isn't used
targetScope = 'subscription'
param otherSubscriptionID string
// resource deployed to a different subscription
resource exampleResource 'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: 'deployToDifferentSub'
scope: subscription(otherSubscriptionID)
location: 'eastus'
}
You can fix the diagnostic by using the module
declaration type:
targetScope = 'subscription'
param otherSubscriptionID string
// module deployed to a different subscription
module exampleModule 'module.bicep' = {
name: 'deployToDifferentSub'
scope: subscription(otherSubscriptionID)
}
For more information about Bicep diagnostics, see Bicep core diagnostics.