你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 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 属性。

如果资源组位于其他订阅中,请提供该订阅 ID 和资源组名称。 如果资源组与当前部署位于同一订阅,请仅提供资源组名称。 如果未在 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 函数中指定该订阅 ID 作为参数。

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

有关详细信息,请参阅管理组

部署到目标资源组

若要在目标资源组部署资源,请在模板的“资源”部分定义这些资源。 以下模板会在部署操作中指定的资源组中创建一个存储帐户。

@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 创建资源组

后续步骤

若要了解其他范围,请参阅: