Skip to content

Commit

Permalink
For loops example (#15)
Browse files Browse the repository at this point in the history
Basic vnet / subnet demo example.
  • Loading branch information
riosengineer authored Feb 29, 2024
1 parent 11915a7 commit ef3e5b5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
66 changes: 66 additions & 0 deletions bicep-examples/loops/README.md
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"
```
49 changes: 49 additions & 0 deletions bicep-examples/loops/main.bicep
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
}
}]
}
}]

0 comments on commit ef3e5b5

Please sign in to comment.