Skip to content

Commit ef3e5b5

Browse files
authored
For loops example (#15)
Basic vnet / subnet demo example.
1 parent 11915a7 commit ef3e5b5

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

bicep-examples/loops/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Azure Bicep - Iterative loops (for)
2+
3+
## Introduction
4+
5+
For loops in Azure Bicep can be a great way to simplify and minimise your Bicep templates, as they allow you to define multiple copies of a resource which helps reduce and avoids us repeating code in our Bicep templates.
6+
7+
Using a for loop, you can quickly deploy multiple resources, in this instance a virtual network resource with multiple subnets in one block.
8+
9+
You can go through the Microsoft Learn training module for this which is great [here](https://learn.microsoft.com/en-us/training/modules/build-flexible-bicep-templates-conditions-loops/).
10+
11+
## 📃 Benefits of using for loops in Bicep
12+
13+
1. ✅ DRY (Don't repeat yourself) - avoids repeating Bicep code uncessarily
14+
15+
2. ✅ Clean templates. By avoiding duplicating code, we can reduce large template files with lots of code lines
16+
17+
3. ✅ Best practice. It's good practice to use for loops in your Bicep templates for the reasons above as well as maintainability as you scale
18+
19+
## For Loop Example
20+
21+
In the basic example within the `main.bicep` file, we can using the `for` property to state for each `vnet` resource, loop through and create the virtual network with subnets.
22+
23+
As the variable (refer to the `main.bicep` file) also contains the required subnets, we're further looping the sub-property within the `vnet` to loop for each `subnet` within the `vnet` as it deploys.
24+
25+
```javascript
26+
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = [for vnet in vnets: {
27+
name: vnet.name
28+
location: location
29+
properties: {
30+
addressSpace: {
31+
addressPrefixes: [
32+
vnet.addressPrefix
33+
]
34+
}
35+
subnets: [for subnet in vnet.subnets: {
36+
name: subnet.name
37+
properties: {
38+
addressPrefix: subnet.subnetPrefix
39+
}
40+
}]
41+
}
42+
}]
43+
```
44+
45+
## 🚀 Deployment
46+
47+
> [!NOTE]
48+
> You need to have a resource group deployed before trying this out.
49+
50+
In VisualStudio Code open a terminal and run:
51+
52+
CLI
53+
54+
```bash
55+
az login
56+
az account set --subscription 'subscription name or id'
57+
az deployment group create -g 'your-rg' --confirm-with-what-if -f '.\main.bicep'
58+
```
59+
60+
or PowerShell
61+
62+
```powershell
63+
Connect-AzAccount
64+
Set-AzContext -Subscription "subscription name or id"
65+
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg -TemplateFile "main.bicep"
66+
```

bicep-examples/loops/main.bicep

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
targetScope = 'resourceGroup'
2+
3+
metadata name = 'Virtual Network with Subnet loop'
4+
metadata description = 'Showcasing Azure Bicep iterative loops - basic example'
5+
metadata owner = 'networks@example.com'
6+
7+
@description('Azure region for deployments chosen from the resource group.')
8+
param location string = resourceGroup().location
9+
10+
@description('The Virtual Network and subnet address spaces & names.')
11+
var vnets = [
12+
{
13+
name: 'vnet-uks-bicepify-dev'
14+
addressPrefix: '10.0.0.0/21'
15+
subnets: [
16+
{
17+
name: 'sql'
18+
subnetPrefix: '10.0.1.0/24'
19+
}
20+
{
21+
name: 'backend'
22+
subnetPrefix: '10.0.2.0/24'
23+
}
24+
{
25+
name: 'app-service'
26+
subnetPrefix: '10.0.3.0/26'
27+
}
28+
]
29+
}
30+
]
31+
32+
// Virtual Network with subnet loop
33+
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = [for vnet in vnets: {
34+
name: vnet.name
35+
location: location
36+
properties: {
37+
addressSpace: {
38+
addressPrefixes: [
39+
vnet.addressPrefix
40+
]
41+
}
42+
subnets: [for subnet in vnet.subnets: {
43+
name: subnet.name
44+
properties: {
45+
addressPrefix: subnet.subnetPrefix
46+
}
47+
}]
48+
}
49+
}]

0 commit comments

Comments
 (0)