This article describes how to set scope with Bicep when deploying to a management group.
As your organization matures, you can deploy a Bicep file to create resources at the management group level. For example, you may need to define and assign policies or Azure role-based access control (Azure RBAC) for a management group. With management group level templates, you can declaratively apply policies and assign roles at the management group level. For more information, see Understand scope.
Management groups are tenant-level resources. However, you can create management groups in a management group deployment by setting the scope of the new management group to the tenant. See Management group.
Set scope
To set the scope to management group, use:
Bicep
targetScope = 'managementGroup'
Deployment commands
To deploy to a management group, use the management group deployment commands.
For management group level deployments, you must provide a location for the deployment. The location of the deployment is separate from the location of the resources you deploy. The deployment location specifies where to store deployment data. Subscription and tenant deployments also require a location. For resource group deployments, the location of the resource group is used to store the deployment data.
You can provide a name for the deployment, or use the default deployment name. The default name is the name of the template file. For example, deploying a template named main.bicep creates a default deployment name of main.
For each deployment name, the location is immutable. You can't create a deployment in one location when there's an existing deployment with the same name in a different location. For example, if you create a management group deployment with the name deployment1 in centralus, you can't later create another deployment with the name deployment1 but a location of westus. If you get the error code InvalidDeploymentLocation, either use a different name or the same location as the previous deployment for that name.
Deployment scopes
In a Bicep file, all resources declared with the resource keyword must be deployed at the same scope as the deployment. For a management group deployment, this means all resource declarations in the Bicep file must be deployed to the same management group or as a child or extension resource of a resource in the same management group as the deployment.
However, this restriction doesn't apply to existing resources. You can reference existing resources at a different scope than the deployment.
To deploy resources at multiple scopes within a single deployment, use modules. Deploying a module triggers a "nested deployment," allowing you to target different scopes. The user deploying the parent Bicep file must have the necessary permissions to initiate deployments at those scopes.
You can deploy a Bicep module from within a management-group scope Bicep file at the following scopes:
To deploy resources to the target management group, add those resources with the resource keyword.
Bicep
targetScope = 'managementGroup'// policy definition created in the management groupresourcepolicyDefinition'Microsoft.Authorization/policyDefinitions@2025-01-01' = {
...
}
To target another management group, add a module. Use the managementGroup function to set the scope property. Provide the management group name.
Bicep
targetScope = 'managementGroup'paramotherManagementGroupNamestring// module deployed at management group level but in a different management groupmoduleexampleModule'module.bicep' = {
name: 'deployToDifferentMG'scope: managementGroup(otherManagementGroupName)
}
Scope to subscription
You can also target subscriptions within a management group. The user deploying the template must have access to the specified scope.
To target a subscription within the management group, add a module. Use the subscription function to set the scope property. Provide the subscription ID.
Bicep
targetScope = 'managementGroup'paramsubscriptionIDstring// module deployed to subscription in the management groupmoduleexampleModule'module.bicep' = {
name: 'deployToSub'scope: subscription(subscriptionID)
}
Scope to resource group
You can also target resource groups within the management group. The user deploying the template must have access to the specified scope.
To target a resource group within the management group, add a module. Use the resourceGroup function to set the scope property. Provide the subscription ID and resource group name.
Bicep
targetScope = 'managementGroup'paramsubscriptionIDstringparamresourceGroupNamestring// module deployed to resource group in the management groupmoduleexampleModule'module.bicep' = {
name: 'deployToRG'scope: resourceGroup(subscriptionID, resourceGroupName)
}
Custom policy definitions that are deployed to the management group are extensions of the management group. To get the ID of a custom policy definition, use the extensionResourceId() function. Built-in policy definitions are tenant level resources. To get the ID of a built-in policy definition, use the tenantResourceId() function.
The following example shows how to define a policy at the management group level, and how to assign it.
Bicep
targetScope = 'managementGroup'
@description('An array of the allowed locations, all other locations will be denied by the created policy.')paramallowedLocationsarray = [
'australiaeast''australiasoutheast''australiacentral'
]
resourcepolicyDefinition'Microsoft.Authorization/policyDefinitions@2023-04-01' = {
name: 'locationRestriction'properties: {
policyType: 'Custom'mode: 'All'parameters: {}
policyRule: {
if: {
not: {
field: 'location'in: allowedLocations
}
}
then: {
effect: 'deny'
}
}
}
}
resourcepolicyAssignment'Microsoft.Authorization/policyAssignments@2024-04-01' = {
name: 'locationAssignment'properties: {
policyDefinitionId: policyDefinition.id
}
}
Plan and execute an endpoint deployment strategy, using essential elements of modern management, co-management approaches, and Microsoft Intune integration.
Azure Microsoft.Resources/resourceGroups syntax and properties to use in Azure Resource Manager templates for deploying the resource. API version latest
Azure Microsoft.Subscription/subscriptionDefinitions syntax and properties to use in Azure Resource Manager templates for deploying the resource. API version latest