|
| 1 | +--- |
| 2 | +title: BCP139 |
| 3 | +description: 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. |
| 4 | +ms.topic: reference |
| 5 | +ms.custom: devx-track-bicep |
| 6 | +ms.date: 09/19/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# Bicep error code - BCP139 |
| 10 | + |
| 11 | +This error occurs when you use [`resource`](../file.md#resources) to deploy resources to a different scope than the target one. You should use [`module`](../file.md#modules) instead. For more information, see the following articles depending on the scope: |
| 12 | + |
| 13 | +- Resource group: [Scope to different resource group](../deploy-to-resource-group.md#scope-to-different-resource-group). |
| 14 | +- Subscription: [Deployment scopes](../deploy-to-subscription.md#deployment-scopes). |
| 15 | +- Management group: [Deployment scopes](../deploy-to-management-group.md#deployment-scopes). |
| 16 | +- Tenant: [Deployment scopes](../deploy-to-tenant.md#deployment-scopes). |
| 17 | + |
| 18 | +## Error description |
| 19 | + |
| 20 | +`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.` |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To deploy resources to a scope that isn't the target scope, add a `module`. |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | +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: |
| 29 | + |
| 30 | +```bicep |
| 31 | +param otherResourceGroup string |
| 32 | +param location string |
| 33 | +
|
| 34 | +resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = { |
| 35 | + name: uniqueString(resourceGroup().id) |
| 36 | + scope: resourceGroup(otherResourceGroup) |
| 37 | + location: location |
| 38 | + sku: { |
| 39 | + name: 'Standard_LRS' |
| 40 | + } |
| 41 | + kind: 'StorageV2' |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +You can fix the error by using the `module` declaration type: |
| 46 | + |
| 47 | +```bicep |
| 48 | +param otherResourceGroup string |
| 49 | +
|
| 50 | +// module deployed to resource group in the same subscription |
| 51 | +module exampleModule 'module.bicep' = { |
| 52 | + name: 'deployStorageToAnotherRG' |
| 53 | + scope: resourceGroup(otherResourceGroup) |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The following example deploys a resource group to a different subscription. The example raises the error because `module` isn't used |
| 58 | + |
| 59 | +```bicep |
| 60 | +targetScope = 'subscription' |
| 61 | +
|
| 62 | +param otherSubscriptionID string |
| 63 | +
|
| 64 | +// module deployed at subscription level but in a different subscription |
| 65 | +resource exampleResource 'Microsoft.Resources/resourceGroups@2024-03-01' = { |
| 66 | + name: 'deployToDifferentSub' |
| 67 | + scope: subscription(otherSubscriptionID) |
| 68 | + location: 'eastus' |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +You can fix the error by using the `module` declaration type: |
| 73 | + |
| 74 | +```bicep |
| 75 | +targetScope = 'subscription' |
| 76 | +
|
| 77 | +param otherSubscriptionID string |
| 78 | +
|
| 79 | +// module deployed at subscription level but in a different subscription |
| 80 | +module exampleModule 'module.bicep' = { |
| 81 | + name: 'deployToDifferentSub' |
| 82 | + scope: subscription(otherSubscriptionID) |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Next steps |
| 87 | + |
| 88 | +For more information about Bicep error and warning codes, see [Bicep core diagnostics](../bicep-core-diagnostics.md). |
0 commit comments