Skip to content

Commit 7505181

Browse files
Merge pull request #285733 from mumian/0828-iterate-child
Iterate child resources
2 parents a6bccd4 + f2a649a commit 7505181

File tree

1 file changed

+19
-19
lines changed
  • articles/azure-resource-manager/bicep

1 file changed

+19
-19
lines changed

articles/azure-resource-manager/bicep/loops.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Iterative loops in Bicep
33
description: Use loops to iterate over collections in Bicep
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 07/11/2024
6+
ms.date: 08/28/2024
77
---
88

99
# Iterative loops in Bicep
@@ -99,7 +99,7 @@ The next example creates the number of storage accounts specified in the `storag
9999
param location string = resourceGroup().location
100100
param storageCount int = 2
101101
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): {
103103
name: '${i}storage${uniqueString(resourceGroup().id)}'
104104
location: location
105105
sku: {
@@ -150,7 +150,7 @@ param storageNames array = [
150150
'coho'
151151
]
152152
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: {
154154
name: '${name}${uniqueString(resourceGroup().id)}'
155155
location: location
156156
sku: {
@@ -213,7 +213,7 @@ var storageConfigurations = [
213213
}
214214
]
215215
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: {
217217
name: '${storageAccountNamePrefix}${config.suffix}${i}'
218218
location: resourceGroup().location
219219
sku: {
@@ -315,7 +315,7 @@ To serially deploy instances of a resource, add the [batchSize decorator](./file
315315
param location string = resourceGroup().location
316316
317317
@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): {
319319
name: '${i}storage${uniqueString(resourceGroup().id)}'
320320
location: location
321321
sku: {
@@ -331,33 +331,33 @@ The `batchSize` decorator is in the [sys namespace](bicep-functions.md#namespace
331331

332332
## Iteration for a child resource
333333

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.
335335

336-
For example, suppose you typically define a file service and file share as nested resources for a storage account.
336+
**Nested child resources**
337337

338338
```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' = {
340342
name: 'examplestorage'
341-
location: resourceGroup().location
343+
location: location
342344
kind: 'StorageV2'
343345
sku: {
344346
name: 'Standard_LRS'
345347
}
346348
resource service 'fileServices' = {
347349
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+
}]
351353
}
352354
}
353355
```
354356

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**
358358

359359
```bicep
360-
resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
360+
resource stg 'Microsoft.Storage/storageAccounts@2023-05-01' = {
361361
name: 'examplestorage'
362362
location: resourceGroup().location
363363
kind: 'StorageV2'
@@ -366,12 +366,12 @@ resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' = {
366366
}
367367
}
368368
369-
resource service 'Microsoft.Storage/storageAccounts/fileServices@2023-04-01' = {
369+
resource service 'Microsoft.Storage/storageAccounts/fileServices@2023-05-01' = {
370370
name: 'default'
371371
parent: stg
372372
}
373373
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): {
375375
name: 'exampleshare${i}'
376376
parent: service
377377
}]
@@ -387,7 +387,7 @@ The outputs of the two samples in [Integer index](#integer-index) can be written
387387
param location string = resourceGroup().location
388388
param storageCount int = 2
389389
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): {
391391
name: '${i}storage${uniqueString(resourceGroup().id)}'
392392
location: location
393393
sku: {

0 commit comments

Comments
 (0)