To simplify the management of resources, you can deploy resources at the level of your Azure subscription. For example, you can deploy policies and Azure role-based access control (Azure RBAC) to your subscription, which applies them across your subscription.
This article describes how to set the deployment scope to a subscription in a Bicep file. For more information, see Understand scope.
Bilješka
You can deploy to 800 different resource groups in a subscription level deployment.
For Azure CLI, use az deployment sub create. The following example deploys a template to create a resource group:
Azure CLI
az deployment sub create \
--name demoSubDeployment \
--location centralus \
--template-file main.bicep \
--parametersrgName=demoResourceGroup rgLocation=centralus
For the PowerShell deployment command, use New-AzDeployment or its alias New-AzSubscriptionDeployment. The following example deploys a template to create a resource group:
For subscription 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. Management group 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.json 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 subscription 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 subscription deployment, this means all resource declarations in the Bicep file must be deployed to the same subscription or as a child or extension resource of a resource in the same subscription 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 resource from within a subscription scope Bicep file at the following scopes:
To deploy resources to the target subscription, add those resources with the resource keyword.
Bicep
targetScope = 'subscription'// resource group created in target subscriptionresourceexampleResource'Microsoft.Resources/resourceGroups@2024-11-01' = {
...
}
To deploy resources to a subscription that is different than the subscription from the operation, add a module. Use the subscription function to set the scope property. Provide the subscriptionId property to the ID of the subscription you want to deploy to.
Bicep
targetScope = 'subscription'paramotherSubscriptionIDstring// module deployed at subscription level but in a different subscriptionmoduleexampleModule'module.bicep' = {
name: 'deployToDifferentSub'scope: subscription(otherSubscriptionID)
}
Scope to resource group
To deploy resources to a resource group within the subscription, add a module and set its scope property. If the resource group already exists, use the resourceGroup function to set the scope value. Provide the resource group name.
If the resource group is created in the same Bicep file, use the symbolic name of the resource group to set the scope value. For an example of setting the scope to the symbolic name, see Create resource group with Bicep.
Scope to tenant
To create resources at the tenant, add a module. Use the tenant function to set its scope property.
The following example assigns an existing policy definition to the subscription. If the policy definition takes parameters, provide them as an object. If the policy definition doesn't take parameters, use the default empty object.
The following example creates a resource group, applies a lock to it, and assigns a role to a principal.
Bicep
targetScope = 'subscription'
@description('Name of the resourceGroup to create')paramresourceGroupNamestring
@description('Location for the resourceGroup')paramresourceGroupLocationstring
@description('principalId of the user that will be given contributor access to the resourceGroup')paramprincipalIdstring
@description('roleDefinition to apply to the resourceGroup - default is contributor')paramroleDefinitionIdstring = 'b24988ac-6180-42a0-ab88-20f7382dd24c'
@description('Unique name for the roleAssignment in the format of a guid')paramroleAssignmentNamestring = guid(principalId, roleDefinitionId, resourceGroupName)varroleID = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/${roleDefinitionId}'resourcenewResourceGroup'Microsoft.Resources/resourceGroups@2024-03-01' = {
name: resourceGroupNamelocation: resourceGroupLocationproperties: {}
}
moduleapplyLock'lock.bicep' = {
name: 'applyLock'scope: newResourceGroup
}
moduleassignRole'role.bicep' = {
name: 'assignRBACRole'scope: newResourceGroupparams: {
principalId: principalIdroleNameGuid: roleAssignmentNameroleDefinitionId: roleID
}
}
The following example shows the module to apply the lock:
Bicep
resourcecreateRgLock'Microsoft.Authorization/locks@2020-05-01' = {
name: 'rgLock'properties: {
level: 'CanNotDelete'notes: 'Resource group should not be deleted.'
}
}
The next example shows the module to assign the role:
Bicep
@description('The principal to assign the role to')paramprincipalIdstring
@description('A GUID used to identify the role assignment')paramroleNameGuidstring = newGuid()paramroleDefinitionIdstringresourceroleNameGuid_resource'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: roleNameGuidproperties: {
roleDefinitionId: roleDefinitionIdprincipalId: principalId
}
}
Azure Microsoft.Resources/resourceGroups syntax and properties to use in Azure Resource Manager templates for deploying the resource. API version latest