Can each.key exclude a specific value from the variable block?

Hello awesome Terraform community! Could someone share opinion on this?

In my data.tf file, I have

data aws_lb load_balancer_5xx {
  for_each = local.config.server.http_5xx_error
  name     = local.service_load_balancer[each.key]
}

In my configs.tf file, I have

environment = {
  amr = {
    service_1 = "service_1-lb"
    service_2 = "service_2-lb"
    service_3 = "service_3-lb"
  }
  eu = {
    service_1 = "service_1-lb"
    service_2 = "service_2-lb"
    service_3 = "service_3-lb"
  }
}
...
load_balancer_prefix = {
  service_1 = "${environment[var.region]["service_1"]}" 
  service_2 = "${environment[var.region]["service_2"]}" 
  service_3 = "${environment[var.region]["service_3"]}" 
}

Now service_3 is not deployed in EU, so the Terraform run gives me a load-balancer-not-found error.
For the “data aws_lb load_balancer_5xx” function in data.tf file, is there a way to do for each but excludes service_3 in eu region?

Thanks

I’m so used to reading the standard code-style for Terraform, with quotes around the resource type and name, that this looks really weird to my eyes now! :slight_smile:

Since you haven’t provided any information about the values of the local variables referenced here, it’s a little difficult to recommend specifics.

I guess you probably want something a bit like what is mentioned in this section of the docs: For Expressions - Configuration Language | Terraform | HashiCorp Developer

1 Like

Hi max, appreciate the help!
The values for for_each = local.config.server.http_5xx_error cannot be changed so I’m trying to add a condition to exclude service_3 in eu region from this function.

Updated:
Ended up with modifying for_each = local.config.server.http_5xx_error lol