-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Azure Bicep - Iterative loops (for) | ||
|
||
## Introduction | ||
|
||
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. | ||
|
||
Using a for loop, you can quickly deploy multiple resources, in this instance a virtual network resource with multiple subnets in one block. | ||
|
||
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/). | ||
|
||
## 📃 Benefits of using for loops in Bicep | ||
|
||
1. ✅ DRY (Don't repeat yourself) - avoids repeating Bicep code uncessarily | ||
|
||
2. ✅ Clean templates. By avoiding duplicating code, we can reduce large template files with lots of code lines | ||
|
||
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 | ||
|
||
## For Loop Example | ||
|
||
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. | ||
|
||
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. | ||
|
||
```javascript | ||
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = [for vnet in vnets: { | ||
name: vnet.name | ||
location: location | ||
properties: { | ||
addressSpace: { | ||
addressPrefixes: [ | ||
vnet.addressPrefix | ||
] | ||
} | ||
subnets: [for subnet in vnet.subnets: { | ||
name: subnet.name | ||
properties: { | ||
addressPrefix: subnet.subnetPrefix | ||
} | ||
}] | ||
} | ||
}] | ||
``` | ||
|
||
## 🚀 Deployment | ||
|
||
> [!NOTE] | ||
> You need to have a resource group deployed before trying this out. | ||
In VisualStudio Code open a terminal and run: | ||
|
||
CLI | ||
|
||
```bash | ||
az login | ||
az account set --subscription 'subscription name or id' | ||
az deployment group create -g 'your-rg' --confirm-with-what-if -f '.\main.bicep' | ||
``` | ||
|
||
or PowerShell | ||
|
||
```powershell | ||
Connect-AzAccount | ||
Set-AzContext -Subscription "subscription name or id" | ||
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg -TemplateFile "main.bicep" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
targetScope = 'resourceGroup' | ||
|
||
metadata name = 'Virtual Network with Subnet loop' | ||
metadata description = 'Showcasing Azure Bicep iterative loops - basic example' | ||
metadata owner = '[email protected]' | ||
|
||
@description('Azure region for deployments chosen from the resource group.') | ||
param location string = resourceGroup().location | ||
|
||
@description('The Virtual Network and subnet address spaces & names.') | ||
var vnets = [ | ||
{ | ||
name: 'vnet-uks-bicepify-dev' | ||
addressPrefix: '10.0.0.0/21' | ||
subnets: [ | ||
{ | ||
name: 'sql' | ||
subnetPrefix: '10.0.1.0/24' | ||
} | ||
{ | ||
name: 'backend' | ||
subnetPrefix: '10.0.2.0/24' | ||
} | ||
{ | ||
name: 'app-service' | ||
subnetPrefix: '10.0.3.0/26' | ||
} | ||
] | ||
} | ||
] | ||
|
||
// Virtual Network with subnet loop | ||
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = [for vnet in vnets: { | ||
name: vnet.name | ||
location: location | ||
properties: { | ||
addressSpace: { | ||
addressPrefixes: [ | ||
vnet.addressPrefix | ||
] | ||
} | ||
subnets: [for subnet in vnet.subnets: { | ||
name: subnet.name | ||
properties: { | ||
addressPrefix: subnet.subnetPrefix | ||
} | ||
}] | ||
} | ||
}] |