共用方式為

Bicep 诊断代码 - BCP144

尝试引用资源或模块集合(例如,使用 for-循环定义的集合)而不指定索引时,会发生此诊断。 Bicep 要求此类引用显式指示你引用的集合中的哪个资源或模块使用数组索引。

DESCRIPTION

此处目前不支持直接引用资源或模块集合。 请将数组索引器应用于表达式。

级别

错误

解决方案

若要解析 BCP144,请使用数组索引访问集合中的每个特定资源或模块。 而不是直接循环访问集合,而是循环访问输入数组,并使用索引引用相应的项。

例子

下面的示例引发诊断,因为它引用资源集合而不指定索引。

param names array = [
  'one'
  'two'
]

resource demo 'Microsoft.Storage/storageAccounts@2022-09-01' = [for name in names: {
  name: 'demo${name}'
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {}
}]

// ❌ This line triggers BCP144.
output storageNames array = [for r in demo: r.name]

若要解析 BCP144,请使用数组索引访问集合中的每个特定资源。

param names array = [
  'one'
  'two'
]

resource demo 'Microsoft.Storage/storageAccounts@2022-09-01' = [for name in names: {
  name: 'demo${name}'
  location: resourceGroup().location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {}
}]

// ✅ Correct usage with indexing.
output storageNames array = [for (name, i) in names: demo[i].name]

以下示例显示了模块集合的错误代码。

param locations array = [
  'chinaeast2'
  'chinanorth3'
]

module storageModule 'storage.bicep' = [for loc in locations: {
  name: 'storage-${loc}'
  params: {
    location: loc
  }
}]

// ❌ This line triggers BCP144.
output moduleOutputs array = [for m in storageModule: m.outputs.storageAccountName]

若要解析 BCP144,请使用数组索引访问集合中的每个特定模块。

param locations array = [
  'chinaeast2'
  'chinanorth3'
]

module storageModule 'storage.bicep' = [for loc in locations: {
  name: 'storage-${loc}'
  params: {
    location: loc
  }
}]

// ✅ Correct usage with indexing.
output moduleOutputs array = [for (loc, i) in locations: storageModule[i].outputs.storageAccountName]

后续步骤

有关 Bicep 诊断的详细信息,请参阅 Bicep 核心诊断