Understanding Terraform loops
In this section, we will be discussing different methods of Terraform loops. Like other programming languages, Terraform also supports some sorts of loops and this will help you to perform N number of Terraform operations very smoothly. Let's try to understand this with an example. Suppose you are working with your colleague, Mark, in one of the multinational companies (MNCs). You are both discussing one of the requirements – the need to deploy an Azure virtual network with 10 different subnets along with a Network Security Group (NSG) associated with all these subnets. You tell Mark that this can be easily done using Terraform loops rather than writing the same code block for the subnet again and again.
We will explain how effectively we can write our Terraform configuration code block using Terraform loops. The following loops are supported by Terraform:
count
: Looping over resourcesfor_each
: Looping over resources and inline...