Using an if statement within a for_each

Hello, I am trying to use an if statement within a for_each in a module with values passed down but it keep ignoring the if statement and trying to apply the values to all.

My code block is;

resource "azurerm_subnet" "this" {
  for_each                                       = { for subnet in var.virtual_network_settings.subnets : subnet.name => subnet }
  name                                           = each.value["name"]
  resource_group_name                            = var.resource_group_name
  virtual_network_name                           = azurerm_virtual_network.this.name
  address_prefixes                               = [each.value["cidr_block"]]
  service_endpoints                              = each.value["service_endpoints"]
  enforce_private_link_endpoint_network_policies = each.value["enforce_private_link_endpoint_network_policies"]
  enforce_private_link_service_network_policies  = each.value["enforce_private_link_service_network_policies"]
  dynamic "delegation" {
    for_each = [for subnet in var.virtual_network_settings.subnets : subnet 
      if subnet.delegation_name != "null"
    ]
    content {
      name = delegation.value.delegation_name
      service_delegation {
        name = delegation.value.service_delegation_name
      }
    }
  }
}

What am I missing here

Hi @slalomdavidsinclair,

Your condition here is subnet.delegation_name != "null", which will return true unless the delegation name is the string "null".

Perhaps you meant to check if the value is not null? If so, you should remove the quotes around null so Terraform will understand it as the null keyword rather than as a string containing those characters:

  if subnet.delegation_name != null

Thank you for the suggestion, I tried that and got the same result

In that case, it seems like there aren’t any elements of var.virtual_network_settings.subnets whose delegation_name is null. Perhaps the problem is in the definition of that variable’s value, rather than in the for expression that’s transforming it?

Figured it out, this was the solution

resource "azurerm_subnet" "this" {
  for_each                                       = { for subnet in var.virtual_network_settings.subnets : subnet.name => subnet }
  name                                           = each.value["name"]
  resource_group_name                            = var.resource_group_name
  virtual_network_name                           = azurerm_virtual_network.this.name
  address_prefixes                               = [each.value["cidr_block"]]
  service_endpoints                              = each.value["service_endpoints"]
  enforce_private_link_endpoint_network_policies = each.value["enforce_private_link_endpoint_network_policies"]
  enforce_private_link_service_network_policies  = each.value["enforce_private_link_service_network_policies"]
  dynamic "delegation" {
    for_each = each.value.delegation_name != null ? [1] : []
    content {
      name = each.value.delegation_name
      service_delegation {
        name = each.value.service_delegation_name
      }
    }
  }
}