Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For loops example #15

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}]
}
}]
Loading