Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform fore_each for loop filter with if condition example

In this HashiCorp Terraform configuration example article, I am using if condition to filter list of variable objects. Here in this configuration I am have multiple list of resource group, Their name and location are different in the variable objects as mentioned below. 

I want to deploy only resource group from WestUS location. Inside the loop, I am using if condition to filter the location. Which will only deploy resource group for given WestUS location.

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

variable "rginfo" {
    type = list(object({
        name = string
        location = string
    }))
    default = [
        {
            name = "west_rg"
            location = "WestUs"
        },
        {
            name = "east_rg"
            location = "EastUs"
        }
    ]
}

resource "azurerm_resource_group" "example_error" {
    for_each = {
        for rg in var.rginfo : rg.name => rg
        if rg.location == "WestUs" # && contains(keys(rg), "rg.location")
    }
  name     = each.value.name
  location = each.value.location
}

This is my plan, where it will only add one resource where location is WestUS.

terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.example_error["west_rg"] will be created
  + resource "azurerm_resource_group" "example_error" {
      + id       = (known after apply)
      + location = "westus"
      + name     = "west_rg"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

This is the screenshot of the terraform plan.

Terraform Microsoft Azure for_each for resource group deployment configuration each key each value azurerm_resource_group contains function condition if.png

Useful Articles
Terraform testing local variables and output csv file without resource Part 1
Terraform testing variable map object values without resource configuration part 2
Terraform foreach module output to show only required results
Hashicorp Terraform map and object inside module and variable example
Terraform one module deploy null or multiple resources based on input
Terraform A reference to a resource type must be followed by at least one attribute access, specifying the resource name
Create an Azure virtual machine scale set and load balancer using Terraform
Azure Terraform fixed Availibility Zones on Virtual Machine Scale Set
Writing and Using Terraform modules
Terraform Using one module variable in another module
Hashicorp Terraform dynamic block with example
Terraform for_each loop on map example
Terraform for_each loop on resource example
Terraform manage similar resources with for_each loop inside modules
Importing existing resources into Terraform - Step by Step
Importing already created module Infrastructure into Terraform and update state file
Conditionally create resources in Terraform

Go Back

Comment

Blog Search

Page Views

12278993

Follow me on Blogarama