為了簡化資源管理,您可以在 Azure 訂閱層級部署資源。 例如,您可以將策略和 Azure 基於角色的存取控制 (Azure RBAC) 部署到您的訂閱,從而將它們套用到您的整個訂閱中。
本文介紹如何在 Bicep 檔案中將部署範圍設定為訂閱。 如需詳細資訊,請參閱了解範圍。
附註
在訂閱等級部署中,您可以部署到 800 個不同的資源群組。
支援的資源
並非所有資源類型都可以部署到訂閱等級。 本節列出支援的資源類型。
針對 Azure 藍圖,請使用:
針對 Azure 原則,請使用:
針對存取控制,請使用:
- accessReviewScheduleDefinitions
- accessReviewScheduleSettings
- roleAssignments
- roleAssignmentScheduleRequests
- roleDefinitions
- roleEligibilityScheduleRequests
- roleManagementPolicyAssignments
對於部署到資源群組的巢狀範本,請使用:
若要建立新的資源群組,請使用:
針對管理您的訂閱,請使用:
針對監視,請使用:
針對安全性,請使用:
- advancedThreatProtectionSettings
- alertsSuppressionRules
- assessmentMetadata
- assessments
- autoProvisioningSettings
- 連接器。
- deviceSecurityGroups
- ingestionSettings
- 定價
- securityContacts
- 設定
- workspaceSettings
其他支援的類型包括:
設定範圍
若要將範圍設為訂閱,請使用:
targetScope = 'subscription'
部署命令
若要部署至訂閱,請使用訂閱層級部署命令。
針對 Azure CLI,請使用 az deployment sub create。 下列範例會部署範本來建立資源群組:
az deployment sub create \
--name demoSubDeployment \
--location centralus \
--template-file main.bicep \
--parameters rgName=demoResourceGroup rgLocation=centralus
如需關於部署 ARM 範本的部署指令與選項的更詳細資訊,請參閱:
部署位置和名稱
對於訂閱級部署,您必須提供部署位置。 部署的位置與您部署的資源位置是分開、彼此獨立的。 部署的位置用來指定要將部署資料儲存在哪裡。 管理群組和租用戶部署也需要位置。 針對資源群組部署,資源群組的位置會用來儲存部署資料。
您可以為部署指定一個名稱,或者使用預設的部署名稱。 預設名稱為範本檔案的名稱。 例如,部署名稱為 main.json 的範本會建立預設部署名稱 main。
對於每個部署名稱,其位置是不可變的。 當同一名稱的部署已存在於其他位置時,您無法在另一個位置建立部署。 例如,如果您在 centralus 中建立名稱為 deployment1 的訂閱部署,稍後就無法再使用名稱 deployment1 建立另一個部署,只能在 westus 的位置建立另一個部署。 如果出現錯誤代碼 InvalidDeploymentLocation,請使用不同的名稱,或對該名稱使用與先前部署相同的位置。
部署範圍
在 Bicep 檔案中,使用 resource 關鍵詞宣告的所有資源都必須部署在與部署相同的範圍。 對於訂用帳戶部署,這表示 resource Bicep 檔案中的所有宣告都必須部署至相同的訂用帳戶,或部署相同訂用帳戶中資源的子或擴充資源。
不過,這項限制不適用於 existing 資源。 您可以引用與部署範圍不同的現有資源。
若要在單一部署內的多個範圍部署資源,請使用模組。 部署模組會觸發「巢狀部署」,讓您可以針對不同的範圍。 部署父 Bicep 檔案的使用者必須擁有在這些範圍內啟動部署所需的權限。
您可以從訂閱範圍 Bicep 檔案部署資源,範圍如下:
將範圍設為訂用帳戶
若要將資源部署至目標訂閱,請使用 resource 關鍵字新增這些資源。
targetScope = 'subscription'
// resource group created in target subscription
resource exampleResource 'Microsoft.Resources/resourceGroups@2025-04-01' = {
...
}
如需部署至訂閱的範例,請參閱使用 Bicep 建立資源群組和指派原則定義。
若要將資源部署至與來自作業之訂閱不同的訂閱,請新增模組。 使用訂閱函式來設定 scope 屬性。 將 subscriptionId 屬性提供給您想要部署的訂閱識別碼。
targetScope = 'subscription'
param otherSubscriptionID string
// module deployed at subscription level but in a different subscription
module exampleModule 'module.bicep' = {
name: 'deployToDifferentSub'
scope: subscription(otherSubscriptionID)
}
範圍設定為資源群組
若要將資源部署至訂閱內的資源群組,請新增模組並設定其 scope 屬性。 如果資源群組已經存在,請使用 resourceGroup 函式設定範圍值。 提供資源群組名稱。
targetScope = 'subscription'
param resourceGroupName string
module exampleModule 'module.bicep' = {
name: 'exampleModule'
scope: resourceGroup(resourceGroupName)
}
如果資源組是在同一個 Bicep 檔案中建立的,請使用資源組的符號名稱來設定範圍值。 如需將範圍設為符號名稱的範例,請參閱使用 Bicep 建立資源群組。
將範圍設為租用戶
若要在租用戶處建立資源,請新增模組。 使用租用戶功能設定其 scope 屬性。
部署範本的使用者必須擁有在租用戶部署的必要存取權。
以下範例包含一個部署到租用戶的模組。
targetScope = 'subscription'
// module deployed at tenant level
module exampleModule 'module.bicep' = {
name: 'deployToTenant'
scope: tenant()
}
除了使用模組外,您也可以將某些資源類型的範圍設為 tenant()。 下列範例在租用戶部署管理群組。
targetScope = 'subscription'
param mgName string = 'mg-${uniqueString(newGuid())}'
// management group created at tenant
resource managementGroup 'Microsoft.Management/managementGroups@2024-02-01-preview' = {
scope: tenant()
name: mgName
properties: {}
}
output output string = mgName
如需詳細資訊,請參閱管理群組。
資源群組
如需建立資源群組的相關訊息,請參閱使用 Bicep 建立資源群組。
Azure 原則
指派原則定義
以下範例將現有策略定義指派給訂閱。 如果策略定義需要參數,請將它們作為物件提供。 如果策略定義不接受參數,則使用預設的空物件。
targetScope = 'subscription'
param policyDefinitionID string
param policyName string
param policyParameters object = {}
resource policyAssign 'Microsoft.Authorization/policyAssignments@2025-03-01' = {
name: policyName
properties: {
policyDefinitionId: policyDefinitionID
parameters: policyParameters
}
}
建立及指派原則定義
您可以在同一個 Bicep 檔案中定義和指派原則定義。
targetScope = 'subscription'
resource locationPolicy 'Microsoft.Authorization/policyDefinitions@2025-03-01' = {
name: 'locationpolicy'
properties: {
policyType: 'Custom'
parameters: {}
policyRule: {
if: {
field: 'location'
equals: 'northeurope'
}
then: {
effect: 'deny'
}
}
}
}
resource locationRestrict 'Microsoft.Authorization/policyAssignments@2025-03-01' = {
name: 'allowedLocation'
properties: {
policyDefinitionId: locationPolicy.id
}
}
存取控制
若要了解如何指派角色,請參閱使用 Azure Resource Manager 範本新增 Azure 角色指派。
以下範例建立資源組,對其套用鎖定,並將角色指派給主體。
targetScope = 'subscription'
@description('Name of the resourceGroup to create')
param resourceGroupName string
@description('Location for the resourceGroup')
param resourceGroupLocation string
@description('principalId of the user that will be given contributor access to the resourceGroup')
param principalId string
@description('roleDefinition to apply to the resourceGroup - default is contributor')
param roleDefinitionId string = 'b24988ac-6180-42a0-ab88-20f7382dd24c'
@description('Unique name for the roleAssignment in the format of a guid')
param roleAssignmentName string = guid(principalId, roleDefinitionId, resourceGroupName)
var roleID = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/${roleDefinitionId}'
resource newResourceGroup 'Microsoft.Resources/resourceGroups@2025-04-01' = {
name: resourceGroupName
location: resourceGroupLocation
properties: {}
}
module applyLock 'lock.bicep' = {
name: 'applyLock'
scope: newResourceGroup
}
module assignRole 'role.bicep' = {
name: 'assignRBACRole'
scope: newResourceGroup
params: {
principalId: principalId
roleNameGuid: roleAssignmentName
roleDefinitionId: roleID
}
}
下列範例顯示要套用鎖定的模組:
resource createRgLock 'Microsoft.Authorization/locks@2020-05-01' = {
name: 'rgLock'
properties: {
level: 'CanNotDelete'
notes: 'Resource group should not be deleted.'
}
}
下一個範例展示了用於分配角色的模組:
@description('The principal to assign the role to')
param principalId string
@description('A GUID used to identify the role assignment')
param roleNameGuid string = newGuid()
param roleDefinitionId string
resource roleNameGuid_resource 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: roleNameGuid
properties: {
roleDefinitionId: roleDefinitionId
principalId: principalId
}
}
後續步驟
若要了解其他範圍,請參閱: