在 Bicep 中設定子資源的名稱和類型

子資源是只存在於另一個資源內容中的資源。 例如,虛擬機器擴充功能 (英文) 無法在沒有虛擬機器 (英文) 的情況下存在。 擴充功能資源是虛擬機器的子資源。

每個父資源只接受特定的資源類型做為子資源。 資源類型的階層可在 Bicep 資源參考 (英文) 中取得。

此文章說明您可宣告子資源的各種方式。

訓練資源

如果您比較想要透過逐步指導來了解子資源,請參閱使用 Bicep 部署子資源和延伸模組資源

名稱和類型模式

在 Bicep 中,在父資源的內外皆可指定子資源。 您為資源名稱和資源類型所提供的值,會根據您宣告子資源的方式而有所不同。 不過,全名和類型一律會解析為相同的模式。

子資源的全名使用下列模式:

{parent-resource-name}/{child-resource-name}

如果階層中的層級多於兩層,請保留重複的父代名稱:

{parent-resource-name}/{child-level1-resource-name}/{child-level2-resource-name}

子資源的完整類型使用下列模式:

{resource-provider-namespace}/{parent-resource-type}/{child-resource-type}

如果階層中的層級多於兩層,請保留重複的父資源類型:

{resource-provider-namespace}/{parent-resource-type}/{child-level1-resource-type}/{child-level2-resource-type}

若計算 / 字元之間的區段數量,您會發現類型中的區段數量總是比名稱中的區段數量多一個。

在父資源內

下列範例顯示父資源的資源屬性內所包括的子資源。

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

  resource <child-resource-symbolic-name> '<child-resource-type>' = {
    <child-resource-properties>
  }
}

巢狀資源宣告必須出現在父資源最上層的語法中。 宣告能以任意深度巢狀化,只要每個層級都是其父資源的子類型即可。

在父資源類型內定義時,請將類型和名稱值格式化為無斜線的單一區段。 下列範例顯示儲存體帳戶具有用於檔案服務的子資源,而該檔案服務則具有用於檔案共用的子資源。 檔案服務的名稱已設定為 default,且其類型已設定為 fileServices。 檔案共用的名稱已設定為 exampleshare,且其類型已設定為 shares

resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: 'examplestorage'
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }

  resource service 'fileServices' = {
    name: 'default'

    resource share 'shares' = {
      name: 'exampleshare'
    }
  }
}

完整的資源類型仍為 Microsoft.Storage/storageAccounts/fileServicesMicrosoft.Storage/storageAccounts/fileServices/shares。 您不需提供 Microsoft.Storage/storageAccounts/,因為系統會從父資源類型與版本取得。 巢狀資源可以選擇性地使用 <segment>@<version> 語法來宣告 API 版本。 如果巢狀資源省略了 API 版本,即會使用父資源的 API 版本。 如果巢狀資源指定了 API 版本,則會使用指定的 API 版本。

子資源名稱會設定為 defaultexampleshare,但全名會包括父代名稱。 您不需提供 examplestoragedefault,因為系統會從父資源取得。

巢狀資源可以存取其父資源的屬性。 在相同父資源主體內宣告的其他資源可以使用符號名稱來彼此參考。 父資源可能無法存取其包含資源的屬性,而嘗試這麼做會造成循環相依性。

若要參考父資源外部的巢狀資源,則必須使用包含資源名稱和 :: 運算子來限定。 例如,若要從子資源輸出屬性:

output childAddressPrefix string = VNet1::VNet1_Subnet1.properties.addressPrefix

父資源外部

下列範例展示了父資源外部的子資源。 如果父資源並未部署在相同範本中,或是您想要使用迴圈來建立多個子資源,您可以使用此方法。 指定子系上的父代屬性,並將值設定為父代的符號名稱。 使用此語法時,您仍然需要宣告完整的資源類型,但子資源的名稱就只用子系的名稱。

resource <parent-resource-symbolic-name> '<resource-type>@<api-version>' = {
  name: 'myParent'
  <parent-resource-properties>
}

resource <child-resource-symbolic-name> '<child-resource-type>@<api-version>' = {
  parent: <parent-resource-symbolic-name>
  name: 'myChild'
  <child-resource-properties>
}

在父資源外部定義時,您會將類型格式化,並使用斜線來包括父類型和名稱。

下列範例顯示完全在根層級定義的儲存體帳戶、檔案服務和檔案共用。

resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: 'examplestorage'
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
}

resource service 'Microsoft.Storage/storageAccounts/fileServices@2022-09-01' = {
  name: 'default'
  parent: storage
}

resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2022-09-01' = {
  name: 'exampleshare'
  parent: service
}

參考子資源符號名稱的方式,與參考父代的方式相同。

父代外部的完整資源名稱

當您在父代外部宣告子資源時,也可以使用完整的資源名稱和類型。 您不需在子資源上設定父屬性。 因為無法推斷相依性,所以您必須明確地設定相依性。

resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: 'examplestorage'
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_LRS'
  }
}

resource service 'Microsoft.Storage/storageAccounts/fileServices@2022-09-01' = {
  name: 'examplestorage/default'
  dependsOn: [
    storage
  ]
}

resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2022-09-01' = {
  name: 'examplestorage/default/exampleshare'
  dependsOn: [
    service
  ]
}

重要

不建議設定完整的資源名稱和類型。 與其他方法相比,這個方法在類型上不夠安全。 如需詳細資訊,請參閱 Linter 規則:使用父屬性

下一步