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 Resource Manager 範本 (ARM 範本) JSON 提供的 apiProfile。 您也可以定義 Bicep 擴充性提供者資源。 如需詳細資訊,請參閱 Bicep 擴充性 Kubernetes 提供者 (部分機器翻譯)。

若要有條件地部署資源,請使用 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
  ...
}

Location

許多資源都需要位置。 您可以透過 IntelliSense 或範本參考,判斷資源是否需要位置。 下列範例會新增用於儲存體帳戶的位置參數。

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

一般來說,您會將位置設為參數,以便部署到不同的位置。

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

標籤

您可以在部署期間將標記套用至資源。 標記可協助您以邏輯方式組織已部署的資源。 如需以不同方式指定標記的範例,請參閱 ARM 範本標記

適用於 Azure 資源的受控識別

有些資源可支援 Azure 資源受控識別。 這些資源在資源宣告的根層級具有身分識別物件。

您可以使用系統指派或使用者指派的身分識別。

下列範例說明如何為 Azure Kubernetes Service 叢集設定系統指派的身分識別。

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 或 Bicep 資源參考,判斷哪些屬性可供使用,以及哪些屬性是必要的。 下列範例會設定儲存體帳戶的其餘屬性。

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

下一步