From ef3e5b55baf3f0dfab36cccfdb321742d01790ce Mon Sep 17 00:00:00 2001 From: Dan Rios <36534747+riosengineer@users.noreply.github.com> Date: Thu, 29 Feb 2024 16:23:15 +0000 Subject: [PATCH] For loops example (#15) Basic vnet / subnet demo example. --- bicep-examples/loops/README.md | 66 +++++++++++++++++++++++++++++++++ bicep-examples/loops/main.bicep | 49 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 bicep-examples/loops/README.md create mode 100644 bicep-examples/loops/main.bicep diff --git a/bicep-examples/loops/README.md b/bicep-examples/loops/README.md new file mode 100644 index 0000000..de6a7fb --- /dev/null +++ b/bicep-examples/loops/README.md @@ -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" +``` \ No newline at end of file diff --git a/bicep-examples/loops/main.bicep b/bicep-examples/loops/main.bicep new file mode 100644 index 0000000..fcefd03 --- /dev/null +++ b/bicep-examples/loops/main.bicep @@ -0,0 +1,49 @@ +targetScope = 'resourceGroup' + +metadata name = 'Virtual Network with Subnet loop' +metadata description = 'Showcasing Azure Bicep iterative loops - basic example' +metadata owner = 'networks@example.com' + +@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 + } + }] + } +}]