In terraform there is no actual if else condition block. But still you can use if condition to filter map or list of objects to get the required keys or values. In below example I have scenario where I wanted to create Resource Group in Azure to defined locations only. Incase no data is provided or match it should not deploy anything (It should be empty loop) same like count in terraform, if 0 value is provided.
In below example I have map of object for Resource Group names and Location as values. While deploying resources they should be created based on the filtered value.
- for key, value in var.resource_groups: In this first part of code, it iterates over the var.resource_groups map (hash table), where key characterizes the resource group name and value signifies the location in the provided variable type map.
- key => value: This produces a key-value pair loop - for each item that passes the condition. The key is the name of the resource group, and the value is the location.
- if value == "East US": This is the if condition that filters the map. Only those resource groups where the value (location) equals "East US" will be included in the subsequent map.
provider "azurerm" { features {} } variable "resource_groups" { description = "A map of resource group names to locations." type = map(string) default = { "rg1" = "East US" "rg2" = "West Europe" } } resource "azurerm_resource_group" "rg" { # Filter the map to only include those with a specific condition. # Example: Only create resource groups in "East US" for_each = { for key, value in var.resource_groups : key => value if value == "East US" } #if value == "East US" || value == "Central US" # Multiple conditions name = each.key location = each.value } output "resource_group_names" { value = [for rg in azurerm_resource_group.rg : rg.name] } ###################################### # provider "azurerm" { # features{} # } # variable "resource_groups" { # description = "A map of resource group names to locations." # type = map(string) # default = {} # Default is an empty map, meaning no resource groups will be created. # } # resource "azurerm_resource_group" "rg" { # for_each = var.resource_groups # name = each.key # location = each.value # } # output "resource_group_names" { # value = [for rg in azurerm_resource_group.rg : rg.name] # }
Download this foreach_empty_loop_examples.zip or this script is also available on github.com.
In the below example result is same from different for_each and for loop logic. Instead here I am providing list of objects in the variable incase if I want to supply more information to resource block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
provider "azurerm" { features{} } variable "resource_groups" { description = "A list of maps containing resource group names and locations." type = list(object({ name = string location = string })) default = [] # Empty list means no resource groups will be created. } resource "azurerm_resource_group" "rg" { for_each = { for idx, rg in var.resource_groups : rg.name => rg if rg.location == "West US" } name = each.value.name location = each.value.location } output "resource_group_names" { value = [for rg in azurerm_resource_group.rg : rg.name] } #for_each = toset([for os in [var.function_app_os] : os if os == "Linux"]) # locals { # filtered_os = toset([for os in [var.function_app_os] : os if os == "Linux"]) # } |
Useful Articles
Terraform variable precedence and priority
Terraform passing different credentials to different subscriptions with provider alias
Using terraform to clone a virtual machine on VMware vSphere infrastructure
Terraform module clone VMware vSphere Linux and Windows virtual machine
Terraform VMware vSphere Virtual Machine customization clone failed on Windows
Terraform VMware vSphere Virtual Machine cloning Operating system not found
Terraform refactoring moved block example
Terraform create Azure Virtual Machines from map of objects
Terraform variable validation example
Configure Azure Storage Account Blob as Terraform backend to store tfstate file Examples of most used general purpose terraform functions
Create storage account and Service Principal using PowerShell for Terraform Azure Backend
Unlocking TF State File on Azure backend with PowerShell and Terraform Force-Unlock Command
Terraform passing different credentials to different subscriptions with provider alias