|
| 1 | +--- |
| 2 | +title: BCP144 |
| 3 | +description: Directly referencing a resource or module collection isn't currently supported here. Apply an array indexer to the expression. |
| 4 | +ms.topic: reference |
| 5 | +ms.custom: devx-track-bicep |
| 6 | +ms.date: 07/16/2025 |
| 7 | +--- |
| 8 | + |
| 9 | +# Bicep diagnostic code - BCP144 |
| 10 | + |
| 11 | +This diagnostic occurs when you try to reference a resource or module collection (for example, one defined using a `for`-loop) without specifying an index. Bicep requires that such references explicitly indicate which resource or module in the collection you're referring to using an array index. |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +Directly referencing a resource or module collection isn't currently supported here. Apply an array indexer to the expression. |
| 16 | + |
| 17 | +## Level |
| 18 | + |
| 19 | +Error |
| 20 | + |
| 21 | +## Solutions |
| 22 | + |
| 23 | +To resolve BCP144, use an array index to access each specific resource or module in the collection. Instead of looping over the collection directly, loop over the input array and use the index to reference the corresponding item. |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +The following example raises the diagnostic because it references a resource collection without specifying an index. |
| 28 | + |
| 29 | +```bicep |
| 30 | +param names array = [ |
| 31 | + 'one' |
| 32 | + 'two' |
| 33 | +] |
| 34 | +
|
| 35 | +resource demo 'Microsoft.Storage/storageAccounts@2022-09-01' = [for name in names: { |
| 36 | + name: 'demo${name}' |
| 37 | + location: resourceGroup().location |
| 38 | + sku: { |
| 39 | + name: 'Standard_LRS' |
| 40 | + } |
| 41 | + kind: 'StorageV2' |
| 42 | + properties: {} |
| 43 | +}] |
| 44 | +
|
| 45 | +// ❌ This line triggers BCP144. |
| 46 | +output storageNames array = [for r in demo: r.name] |
| 47 | +``` |
| 48 | + |
| 49 | +To resolve BCP144, use an array index to access each specific resource in the collection. |
| 50 | + |
| 51 | +```bicep |
| 52 | +param names array = [ |
| 53 | + 'one' |
| 54 | + 'two' |
| 55 | +] |
| 56 | +
|
| 57 | +resource demo 'Microsoft.Storage/storageAccounts@2022-09-01' = [for name in names: { |
| 58 | + name: 'demo${name}' |
| 59 | + location: resourceGroup().location |
| 60 | + sku: { |
| 61 | + name: 'Standard_LRS' |
| 62 | + } |
| 63 | + kind: 'StorageV2' |
| 64 | + properties: {} |
| 65 | +}] |
| 66 | +
|
| 67 | +// ✅ Correct usage with indexing. |
| 68 | +output storageNames array = [for (name, i) in names: demo[i].name] |
| 69 | +``` |
| 70 | + |
| 71 | +The following example shows the error code with a module collection. |
| 72 | + |
| 73 | +```bicep |
| 74 | +param locations array = [ |
| 75 | + 'eastus' |
| 76 | + 'westus' |
| 77 | +] |
| 78 | +
|
| 79 | +module storageModule 'storage.bicep' = [for loc in locations: { |
| 80 | + name: 'storage-${loc}' |
| 81 | + params: { |
| 82 | + location: loc |
| 83 | + } |
| 84 | +}] |
| 85 | +
|
| 86 | +// ❌ This line triggers BCP144. |
| 87 | +output moduleOutputs array = [for m in storageModule: m.outputs.storageAccountName] |
| 88 | +``` |
| 89 | + |
| 90 | +To resolve BCP144, use an array index to access each specific module in the collection. |
| 91 | + |
| 92 | +```bicep |
| 93 | +param locations array = [ |
| 94 | + 'eastus' |
| 95 | + 'westus' |
| 96 | +] |
| 97 | +
|
| 98 | +module storageModule 'storage.bicep' = [for loc in locations: { |
| 99 | + name: 'storage-${loc}' |
| 100 | + params: { |
| 101 | + location: loc |
| 102 | + } |
| 103 | +}] |
| 104 | +
|
| 105 | +// ✅ Correct usage with indexing. |
| 106 | +output moduleOutputs array = [for (loc, i) in locations: storageModule[i].outputs.storageAccountName] |
| 107 | +``` |
| 108 | + |
| 109 | +## Next steps |
| 110 | + |
| 111 | +For more information about Bicep diagnostics, see [Bicep core diagnostics](../bicep-core-diagnostics.md). |
0 commit comments