Saturday, January 23, 2021

Manageable Infra scripts with Terraform for-each

While we are working on IT projects, you might get requirements such as provisioning hundreds of app services for a project. If you choose to do that task manually, that would get long time to complete the task. As a solution, we can use Infrastructure automation tools such as Azure CLI, ARM template, Terraform, etc. However, we need to write these Infa scripts well organized manner and manageable way. Otherwise, you will get into more trouble of maintaining long and repetitive scripts.

Terraform has several ways such as modules and loops to reduce number of lines of the script. In this blog, we will be discussing the for-each loop of the Terraform.

In the following sample we use main.tf and variable.tf files.  Three app service plans and three app services will be created using the script. Let's go thought the Main.tf file to understand how we define the for-each.