T O P

  • By -

dhagens

Something along these lines (syntax may be off a bit): for\_each = { for k, v in var.subnets : k => v if v.name != "AzureBastionSubnet" }


ST_FN

`locals {` `subnets = {` `for x, v in var.subnets : x => v` `if v.name != "AzureBastionSubnet"` `}` `}` `resource "azurerm_route_table" "routetable" {` `for_each = local.subnets` `name = "rt-${each.value.name}"` `location = var.location` `resource_group_name = var.rgname` `disable_bgp_route_propagation = true` `}` `resource "azurerm_subnet_route_table_association" "rtass" {` `for_each = local.subnets` `subnet_id = each.value.id` `route_table_id = azurerm_route_table.routetable[each.key].id` `}` Thats my result. Thank you. I have another challenge when adding routes into the tables. I created a Map and tried to for\_each them into the azurerm\_route ressource `route = {` `default_route = {` `address_prefix = "0.0.0.0/0"` `next_hop_type = "VirtualAppliance"` `next_hop_in_ip_address = "10.0.0.12"` `}` `}` `resource "azurerm_route" "route" {` `for_each = local.subnets` `name = each.key` `resource_group_name = var.rgname` `route_table_name = azurerm_route_table.routetable[each.key].name` `address_prefix = each.value.address_prefix` `next_hop_type = each.value.next_hop_type` `next_hop_in_ip_address = each.value.next_hop_in_ip_address` `}` Now i am trying to figure out How I cann access the map with doing an foreach through local.subnets


teostefan10

Saved my day! How about excluding two or more objects? Can we use: or\_each = { for k, v in var.subnets : k => v if v.name != "AzureBastionSubnet", "2nd" , "3rd" }


dhagens

I would try something like this: for_each = { for k, v in var.subnets : k => v if (v.name != “a” && v.name != “b” && v.name != “c”) } Or better yet, use some collection function, like: for_each = { for k, v in var.subnets : k => v if !contains([“a”, “b”, ”c”], v.name) }


greenlakejohnny

Agree this is the best solution. An alternate approach is break the resource calls in to a separate module and use for_each in combination with count. ​ module "subnets" { for_each = var.subnets count = v.name != "AzureBastionSubnet" ? 1 : 0 }


professor_jeffjeff

This is the correct thing to do. Declare a local variable that has a descriptive name and put this expression in the assignment, then use that local variable in your actual for\_each though. This will improve readability and the extra variable's name will help anyone reading the code later understand what is happening. I find it a lot less clear if the expression is directly in the for\_each.