Terraform Functions - Part 1 - For_Each
In this blog series we will explore some of the common Terraform functions that will be useful to know when deploying resources to Azure. With working demos you can follow along and deploy resources to your own Azure subscriptions and learn how these functions behave. Knowing about these features and combining the use of them in your Terraform configurations will help as you make more advanced deployment templates and modules. This will be a four part series covering the following;
- Basic for_each demo
- Basic count demo
- Conditional expression demo
- Advanced count and conditional expression combined
- Clone the repo TerraformDemos
- Open the folder in Visual studio code
- Authenticate to Azure
- az login - complete authentication process in the browser
- If you have access to a number of subscriptions run this command to confirm which one you are active working on, this will show 'IsDefault' as true.
- az account list --output table
- If you need to change this use this command and replace that value with your subscription number
- az account set --subscription 000000000-00000000000-00000000-0000000
- At the end of each lab we carry out a cleanup task to remove any resources we have deployed during the demo.
Each folder is a self contained demonstration which you can run through with to help understand these Terraform functions. We are now ready to get started.
The for_each function is new in version 0.12 of Terraform, this can be used to iterate through a list or map. This is an alternative to the count function. In the count function we can append a number to the name of resources so you can build web1, web2, web3, etc. What if we want the names to be different? Also, what if we were dealing with a different resource? For example, we have a list of users we want to assign RBAC permissions to. With the for_each function we could create a list of users in a variable and iterate through this to deploy role assignments for each user.
In this demo we deploy multiple web application resources, you will see how we do this using count function in an upcoming demo. We use a list of app names and deploy multiple Web Apps using for_each to iterate through the list of names.
- Review the code in \app-service-foreach-demo\variables.tf
- See that variable webapps has three names configured, two of them are currently commented out
- Review the code in \app-service-foreach-demo\main.tf
- Check the resource azurerm_app_service
- We have a for_each value, this references var.webapps the list we have in our variable file
- for_each = var.webapps
- name = each.value
- One Web App Service Plan
- One Web Application called dog-fe-web-1
- #web2 = "be-web-2"
- #web3 = "demo-web-3"
- One Web App Service Plan
- Three Web Applications fe-web-1, be-web-2, demo-web-3
I hope this has helped you get started using the new for_each function and you can see where this may be useful in your deployments. Next time we will explore how we can achieve a similar goal using the count function.