Diving into resource iterations
Sometimes, you need to create multiple resources of the same type with the same template. Since templates are reusable, it is expected from them to be able to provide the ability to do so. In Azure Bicep, you can add loops to resource declarations and dynamically define how many you want to create. Let's learn how this approach can help you avoid repeating syntax within the template.
Note
Currently, Bicep loops cannot work with negative numbers or go above 800 iterations.
Apart from resource declaration, you can apply loops to modules
, properties
, and outputs
, which you will learn more about in Chapter 8, Defining Modules and Utilizing Outputs, of this book. Let's start by looking at syntax.
Syntax
You can use an index in a range to create loops:
resource <resource-symbolic-name> '<resource-type>@<api-version>' = [for <index> in range(<start>, <stop>): { <resource...