@@ -3,7 +3,7 @@ title: Iterative loops in Bicep
3
3
description : Use loops to iterate over collections in Bicep
4
4
ms.topic : conceptual
5
5
ms.custom : devx-track-bicep
6
- ms.date : 07/11 /2024
6
+ ms.date : 08/28 /2024
7
7
---
8
8
9
9
# Iterative loops in Bicep
@@ -99,7 +99,7 @@ The next example creates the number of storage accounts specified in the `storag
99
99
param location string = resourceGroup().location
100
100
param storageCount int = 2
101
101
102
- resource storageAcct 'Microsoft.Storage/storageAccounts@2023-04 -01' = [for i in range(0, storageCount): {
102
+ resource storageAcct 'Microsoft.Storage/storageAccounts@2023-05 -01' = [for i in range(0, storageCount): {
103
103
name: '${i}storage${uniqueString(resourceGroup().id)}'
104
104
location: location
105
105
sku: {
@@ -150,7 +150,7 @@ param storageNames array = [
150
150
'coho'
151
151
]
152
152
153
- resource storageAcct 'Microsoft.Storage/storageAccounts@2023-04 -01' = [for name in storageNames: {
153
+ resource storageAcct 'Microsoft.Storage/storageAccounts@2023-05 -01' = [for name in storageNames: {
154
154
name: '${name}${uniqueString(resourceGroup().id)}'
155
155
location: location
156
156
sku: {
@@ -213,7 +213,7 @@ var storageConfigurations = [
213
213
}
214
214
]
215
215
216
- resource storageAccountResources 'Microsoft.Storage/storageAccounts@2023-04 -01' = [for (config, i) in storageConfigurations: {
216
+ resource storageAccountResources 'Microsoft.Storage/storageAccounts@2023-05 -01' = [for (config, i) in storageConfigurations: {
217
217
name: '${storageAccountNamePrefix}${config.suffix}${i}'
218
218
location: resourceGroup().location
219
219
sku: {
@@ -315,7 +315,7 @@ To serially deploy instances of a resource, add the [batchSize decorator](./file
315
315
param location string = resourceGroup().location
316
316
317
317
@batchSize(2)
318
- resource storageAcct 'Microsoft.Storage/storageAccounts@2023-04 -01' = [for i in range(0, 4): {
318
+ resource storageAcct 'Microsoft.Storage/storageAccounts@2023-05 -01' = [for i in range(0, 4): {
319
319
name: '${i}storage${uniqueString(resourceGroup().id)}'
320
320
location: location
321
321
sku: {
@@ -331,33 +331,33 @@ The `batchSize` decorator is in the [sys namespace](bicep-functions.md#namespace
331
331
332
332
## Iteration for a child resource
333
333
334
- You can't use a loop for a nested child resource. To create more than one instance of a child resource, change the child resource to a top-level resource .
334
+ To create more than one instance of a child resource, both of the following Bicep files would work .
335
335
336
- For example, suppose you typically define a file service and file share as nested resources for a storage account.
336
+ ** Nested child resources**
337
337
338
338
``` bicep
339
- resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
339
+ param location string = resourceGroup().location
340
+
341
+ resource stg 'Microsoft.Storage/storageAccounts@2023-05-01' = {
340
342
name: 'examplestorage'
341
- location: resourceGroup(). location
343
+ location: location
342
344
kind: 'StorageV2'
343
345
sku: {
344
346
name: 'Standard_LRS'
345
347
}
346
348
resource service 'fileServices' = {
347
349
name: 'default'
348
- resource share 'shares' = {
349
- name: 'exampleshare'
350
- }
350
+ resource share 'shares' = [for i in range(0, 3): {
351
+ name: 'exampleshare${i} '
352
+ }]
351
353
}
352
354
}
353
355
```
354
356
355
- To create more than one file share, move it outside of the storage account. You define the relationship with the parent resource through the ` parent ` property.
356
-
357
- The following example shows how to create a storage account, file service, and more than one file share:
357
+ ** Top-level child resources**
358
358
359
359
``` bicep
360
- resource stg 'Microsoft.Storage/storageAccounts@2023-04 -01' = {
360
+ resource stg 'Microsoft.Storage/storageAccounts@2023-05 -01' = {
361
361
name: 'examplestorage'
362
362
location: resourceGroup().location
363
363
kind: 'StorageV2'
@@ -366,12 +366,12 @@ resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
366
366
}
367
367
}
368
368
369
- resource service 'Microsoft.Storage/storageAccounts/fileServices@2023-04 -01' = {
369
+ resource service 'Microsoft.Storage/storageAccounts/fileServices@2023-05 -01' = {
370
370
name: 'default'
371
371
parent: stg
372
372
}
373
373
374
- resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2023-04 -01' = [for i in range(0, 3): {
374
+ resource share 'Microsoft.Storage/storageAccounts/fileServices/shares@2023-05 -01' = [for i in range(0, 3): {
375
375
name: 'exampleshare${i}'
376
376
parent: service
377
377
}]
@@ -387,7 +387,7 @@ The outputs of the two samples in [Integer index](#integer-index) can be written
387
387
param location string = resourceGroup().location
388
388
param storageCount int = 2
389
389
390
- resource storageAcct 'Microsoft.Storage/storageAccounts@2023-04 -01' = [for i in range(0, storageCount): {
390
+ resource storageAcct 'Microsoft.Storage/storageAccounts@2023-05 -01' = [for i in range(0, storageCount): {
391
391
name: '${i}storage${uniqueString(resourceGroup().id)}'
392
392
location: location
393
393
sku: {
0 commit comments