Bicep error code - BCP139

This error 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:

Error description

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.

Solution

To deploy resources to a scope that isn't the target scope, add a module.

Examples

The following example deploys a storage account resource to a different resource group in the same subscription. The example raises the error 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 error 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 error 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 error 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)
}

Next steps

For more information about Bicep error and warning codes, see Bicep core diagnostics.