使用 Bicep 檔案部署訂閱

若要簡化資源的管理,您可以在您的 Azure 訂用帳戶層級部署資源。 例如,您可以將原則Azure 角色型存取控制 (Azure RBAC) 部署至您的訂閱,如此系統便會在整個訂閱中套用這些原則和存取控制。

本文說明如何將部署範圍設定為 Bicep 檔案中的訂用帳戶。

注意

您可以在訂用帳戶層級部署中部署最多 800 個不同的資源群組。

訓練資源

如果您比較想要透過逐步指導來了解部署範圍,請參閱使用 Bicep 將資源部署到訂用帳戶、管理群組和租用戶

支援的資源

並非所有的資源類型都可部署至訂閱層級。 本節將列出支援的資源類型。

針對 Azure 藍圖,請使用:

針對 Azure 原則,請使用:

針對存取控制,請使用:

若要部署至資源群組的巢狀範本,請使用:

若要建立新的資源群組,請使用:

若要管理您的訂閱,請使用:

針對監視,請使用:

針對安全性,請使用:

其他支援的類型包括:

集合範圍

若要將範圍設為訂閱,請使用:

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,請使用不同的名稱或與先前該名稱部署相同的位置。

部署範圍

部署至訂閱時,您可以將資源部署至:

  • 來自作業的目標訂閱
  • 租用戶中的任何訂閱
  • 訂閱或其他訂閱內的資源群組
  • 訂閱的租用戶

延伸模組的範圍可以設為與部署目標不同的目標。

部署範本的使用者必須有指定範圍的存取權。

將範圍設為訂閱

若要將資源部署至目標訂閱,請使用 resource 關鍵字新增這些資源。

targetScope = 'subscription'

// resource group created in target subscription
resource exampleResource 'Microsoft.Resources/resourceGroups@2022-09-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 建立資源群組

租用戶的範圍

若要在租用戶建立資源,請新增模組。 使用 tenant 函式 (部分機器翻譯) 來設定其 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@2021-04-01' = {
  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@2022-06-01' = {
  name: policyName
  properties: {
    policyDefinitionId: policyDefinitionID
    parameters: policyParameters
  }
}

建立及指派原則定義

您可以在同一個 Bicep 檔案中定義和指派原則定義。

targetScope = 'subscription'

resource locationPolicy 'Microsoft.Authorization/policyDefinitions@2021-06-01' = {
  name: 'locationpolicy'
  properties: {
    policyType: 'Custom'
    parameters: {}
    policyRule: {
      if: {
        field: 'location'
        equals: 'northeurope'
      }
      then: {
        effect: 'deny'
      }
    }
  }
}

resource locationRestrict 'Microsoft.Authorization/policyAssignments@2022-06-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@2022-09-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
  }
}

下一步

若要了解其他範圍,請參閱: