資源群組部署搭配 Bicep 檔案

本文說明部署至資源群組時,如何以 Bicep 設定範圍。

支援的資源

大多數資源都可以部署至資源群組。 如需可用資源的清單,請參閱 ARM 範本參考

集合範圍

Bicep 檔案預設以資源群組為範圍。 如果要明確設定範圍,請使用:

targetScope = 'resourceGroup'

但是,沒必要將目標範圍設定為資源群組,因為預設即使用該範圍。

部署命令

若要部署至資源群組,請使用資源群組部署命令。

若為 Azure CLI,請使用 az deployment group create。 下列範例會部署範本來建立資源群組。 您在 --resource-group 參數中指定的資源群組是目標資源群組

az deployment group create \
  --name demoRGDeployment \
  --resource-group ExampleGroup \
  --template-file main.bicep \
  --parameters storageAccountType=Standard_GRS

有關用於部署 ARM 範本的部署命令和選項,如需詳細資訊,請參閱:

部署範圍

部署至資源群組時,您可以將資源部署至:

  • 部署作業的目標資源群組
  • 相同訂用帳戶或其他訂用帳戶中的其他資源群組
  • 租用戶中的任何訂用帳戶
  • 資源群組的租用戶

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

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

本節說明如何指定不同的範圍。 您可以將這些不同的範圍合併至單一範本中。

將範圍設定為目標資源群組

若要將資源部署至目標資源群組,請將這些資源新增至 Bicep 檔案。

// resource deployed to target resource group
resource exampleResource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  ...
}

如需範本的範例,請參閱部署至目標資源群組

將範圍設定為不同的資源群組

若要將資源部署至不是目標資源群組的資源群組,請新增模組。 使用 resourceGroup 函式 來設定該模組的 scope 屬性。

如果資源群組位於不同的訂用帳戶中,請提供訂用帳戶識別碼和資源群組的名稱。 如果資源群組與目前部署位於相同的訂用帳戶中,則只提供資源群組的名稱即可。 如果您在 resourceGroup 函式中未指定訂用帳戶,則會使用目前的訂用帳戶。

下列範例顯示的模組以不同訂用帳戶中的資源群組為目標。

param otherResourceGroup string
param otherSubscriptionID string

// module deployed to different subscription and resource group
module exampleModule 'module.bicep' = {
  name: 'otherSubAndRG'
  scope: resourceGroup(otherSubscriptionID, otherResourceGroup)
}

下一個範例顯示的模組以相同訂用帳戶中的資源群組為目標。

param otherResourceGroup string

// module deployed to resource group in the same subscription
module exampleModule 'module.bicep' = {
  name: 'otherRG'
  scope: resourceGroup(otherResourceGroup)
}

如需範本的範例,請參閱部署至多個資源群組

以訂用帳戶為範圍

若要將資源部署至訂用帳戶,請新增模組。 使用 subscription 函式來設定其 scope 屬性。

若要部署至目前的訂用帳戶,請使用不帶參數的 subscription 函式。


// module deployed at subscription level
module exampleModule 'module.bicep' = {
  name: 'deployToSub'
  scope: subscription()
}

若要部署至不同的訂用帳戶,請在 subscription 函式中指定該訂用帳戶識別碼作為參數。

param otherSubscriptionID string

// module deployed at subscription level but in a different subscription
module exampleModule 'module.bicep' = {
  name: 'deployToSub'
  scope: subscription(otherSubscriptionID)
}

如需範本的範例,請參閱使用 Bicep 建立資源群組

租用戶的範圍

若要在租用戶建立資源,請新增模組。 使用 tenant 函式 (部分機器翻譯) 來設定其 scope 屬性。

部署範本的使用者必須擁有在租用戶部署的必要存取權

下列範例包含一個部署至租用戶的模組。

// module deployed at tenant level
module exampleModule 'module.bicep' = {
  name: 'deployToTenant'
  scope: tenant()
}

除了使用模組外,您也可以將某些資源類型的範圍設為 tenant()。 下列範例在租用戶部署管理群組。

param mgName string = 'mg-${uniqueString(newGuid())}'

// ManagementGroup deployed at tenant
resource managementGroup 'Microsoft.Management/managementGroups@2020-05-01' = {
  scope: tenant()
  name: mgName
  properties: {}
}

output output string = mgName

如需詳細資訊,請參閱管理群組

部署至目標資源群組

若要在目標資源群組中部署資源,請在範本的 resources 區段中定義這些資源。 下列範本在部署作業所指定的資源群組中建立儲存體帳戶。

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Standard_ZRS'
  'Premium_LRS'
  'Premium_ZRS'
  'Standard_GZRS'
  'Standard_RAGZRS'
])
param storageSKU string = 'Standard_LRS'

param location string = resourceGroup().location

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

部署至多個資源群組

您可以在單一 Bicep 檔案中部署至多個資源群組。

注意

您可以在單一部署中部署至 800 個資源群組。 一般而言,此限制表示您可以部署至一個指定用於父代範本的資源群組,並且可在巢狀或連結的部署中部署至最多 799 個資源群組。 不過,如果父代範本只包含巢狀或連結的範本,本身未部署任何資源,則您可以在巢狀或連結的部署中包含最多 800 個資源群組。

下列範例會部署兩個儲存體帳戶。 第一個儲存體帳戶會部署到部署作業中指定的資源群組。 第二個儲存體帳戶會部署到 secondResourceGroupsecondSubscriptionID 參數指定的資源群組:

@maxLength(11)
param storagePrefix string

param firstStorageLocation string = resourceGroup().location

param secondResourceGroup string
param secondSubscriptionID string = ''
param secondStorageLocation string

var firstStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
var secondStorageName = '${storagePrefix}${uniqueString(secondSubscriptionID, secondResourceGroup)}'

module firstStorageAcct 'storage.bicep' = {
  name: 'storageModule1'
  params: {
    storageLocation: firstStorageLocation
    storageName: firstStorageName
  }
}

module secondStorageAcct 'storage.bicep' = {
  name: 'storageModule2'
  scope: resourceGroup(secondSubscriptionID, secondResourceGroup)
  params: {
    storageLocation: secondStorageLocation
    storageName: secondStorageName
  }
}

這兩個模組都使用名為 storage.bicep 的同一個 Bicep 檔案。

param storageLocation string
param storageName string

resource storageAcct 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: storageName
  location: storageLocation
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'Storage'
  properties: {}
}

建立資源群組

如需建立資源群組的詳細資訊,請參閱使用 Bicep 建立資源群組

下一步

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