Bicep 中的资源声明

本文介绍用于将资源添加到 Bicep 文件的语法。 一个 Bicep 文件中最多可以有 800 个资源。 有关详细信息,请参阅模板限制

声明

使用 resource 关键字添加资源声明。 设置资源的符号名称。 符号名称与资源名称不同。 使用符号名称可以引用 Bicep 文件其他部分中的资源。

resource <symbolic-name> '<full-type-name>@<api-version>' = {
  <resource-properties>
}

因此,存储帐户的声明开头可以为:

resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  ...
}

符号名称区分大小写。 可能包含字母、数字和下划线 (_)。 不能以数字开头。 资源不能与参数、变量或模块同名。

有关可用的资源类型和版本,请参阅 Bicep 资源参考。 Bicep 不支持在 Azure 资源管理器模板(ARM 模板)JSON 中可用的 apiProfile

若要按条件部署资源,请使用 if 语法。 有关详细信息,请参阅 Bicep 中的条件部署

resource <symbolic-name> '<full-type-name>@<api-version>' = if (condition) {
  <resource-properties>
}

若要部署一个资源的多个实例,请使用 for 语法。 可使用 batchSize 修饰器指定是串行部署实例还是并行部署实例。 有关详细信息,请参阅 Bicep 中的迭代循环

@batchSize(int) // optional decorator for serial deployment
resource <symbolic-name> '<full-type-name>@<api-version>' = [for <item> in <collection>: {
  <properties-to-repeat>
}]

还可以对资源属性使用 for 语法来创建数组。

resource <symbolic-name> '<full-type-name>@<api-version>' = {
  properties: {
    <array-property>: [for <item> in <collection>: <value-to-repeat>]
  }
}

资源名称

每个资源都有一个名称。 设置资源名称时,请注意资源名称的规则和限制

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: 'examplestorage'
  ...
}

通常,你会将名称设置为参数,以便可以在部署过程中传入不同的值。

@minLength(3)
@maxLength(24)
param storageAccountName string

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: storageAccountName
  ...
}

位置

许多资源需要一个位置。 可以通过 Intellisense 来确定资源是否需要位置。 以下示例添加用于存储帐户的位置参数。

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: 'examplestorage'
  location: 'chinaeast'
  ...
}

通常,你会将位置设置为参数,以便可以部署到不同的位置。

param location string = resourceGroup().location

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: 'examplestorage'
  location: location
  ...
}

不同位置支持的资源类型不一样。 若要获取 Azure 服务支持的位置,请参阅可用产品(按区域)。 若要获取资源类型支持的位置,请使用 Azure PowerShell 或 Azure CLI。

((Get-AzResourceProvider -ProviderNamespace Microsoft.Batch).ResourceTypes `
  | Where-Object ResourceTypeName -eq batchAccounts).Locations

Tags

可以在部署期间对资源应用标记。 可以通过标记对部署的资源进行逻辑组织。 有关指定标记的不同方法的示例,请参阅 ARM 模板标记

Azure 资源的托管标识

某些资源支持 Azure 资源托管标识。 这些资源在资源声明的根级别具有标识对象。

可以使用系统分配的或用户分配的标识。

以下示例演示如何为 Azure Kubernetes 服务群集配置系统分配的标识。

resource aks 'Microsoft.ContainerService/managedClusters@2020-09-01' = {
  name: clusterName
  location: location
  tags: tags
  identity: {
    type: 'SystemAssigned'
  }

下一个示例显示如何为虚拟机配置用户分配的身份。

param userAssignedIdentity string

resource vm 'Microsoft.Compute/virtualMachines@2020-06-01' = {
  name: vmName
  location: location
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${userAssignedIdentity}': {}
    }
  }

特定于资源的属性

上述属性对于大多数资源类型都是通用的。 设置这些值后,需要设置特定于所部署的资源类型的属性。

使用 Intellisense 来确定哪些属性可用以及哪些属性是必需的。 下面的示例将为存储帐户设置其余属性。

resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
  name: 'examplestorage'
  location: 'chinaeast'
  sku: {
    name: 'Standard_LRS'
    tier: 'Standard'
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }
}

后续步骤