Bicep Loops
Bicep Loops

Azure Bicep – Understanding the loops

In this article, we are going to talk about how can we use loops in Azure bicep code.

Why loops are needed ?

As in other languages, loops in biceps can basically be used to avoid the repetitive code sections. We can create as many resources, modules, variables, etc. by using loops. Loops allow to dynamically control the number of copies we want to create via bicep file.

One of the challenge while creating multiple copies via loops is – how to define unique names for the resources ? For example, if we want to create multiple storage accounts, we need to make sure that each of the storage accounts has a unique name. One solution to this problem is to use loop index to create unique names. Another option is to create collection of objects and then we can pass names as one of the properties, thus allowing consumers to specify the names.

The Bicep language allows multiple ways to write loops. Some of the syntaxes are listed in the sections given below.

Using Integer Index

This is simplest form of the loop. There is an integer index and there is an upper limit (generally already known or derived before the loop begins). Then the index is incremented in every iteration. The index then can be used to modify some values or make them unique.

The code example given below shows an integer loop to create 5 storage accounts.

Iterate over Arrays

This kind of loops can be used when the scenario is – we want to create one resource for every element present in a collection. Basically, there will be a collection of objects. Each object can be as simple as a string or it can be complex object. This collection can be either input parameter or it can again be derived from some other objects.

The code example given below shows a loop. This loop iterates over a string array parameter. This array is supposed to contain the storage account names. Then the code iterates over the array and creates storage accounts.

Iterate over Dictionaries

As we know, a dictionary is basically a collection of key – value pairs, where key is supposed to be unique. We can also accept this type of parameters as input and then iterate over them to create the resources.

The bicep code snippet given below shows usage of dictionary for loop. The dictionary is defined the code itself. It contains name of the resource as key and the value of item is basically the pricing tier that we should use for that resource.

As you can see here, we need to use the function getItems to get the items one by one for each iteration.

Index and Items in collection

This type of loops are useful when the scenario is – we need to iterate over collection to create resources, but we also want to use index to derive few other values / resources.

Let’s have a look at code snippet given below. We get the storage account names in the form of an array. Then we iterate over that array using index and item pattern. Here, the storage account name is derived, by prefixing the index value.

Loop with Conditions

This type of loops are useful, when the multiple resources of same type need to be created, but each resource should be created only if certain conditions are satisfied.

Let’s take a simple example. Let’s say, we need to create 5 storage accounts. But those accounts should be created only if the environment name is “feature branch”. For other environments they should not be created. The code given below takes environment name as input parameter and then it uses integer index loop from 1 to 5. Then there is a condition to check if environment name is “feature branch”.

I hope this information is helpful. Let me know your thoughts.

Leave a Reply Cancel reply