Conditions - for each and boolean

Hello,
I would like to kindly ask you to give me help, please.

I have this condition:

  for_each  = {for service in var.services:  service.us_name => service}

and this condition as well:

  count     = var.ecs_autoscale ? 1 : 0

How to put it to for each condition, please?

Perhaps something like:

for_each  = var.ecs_autoscale ? {for service in var.services:  service.us_name => service} : {}

The idea of for_each is to pass it a mapping that has one element for each instance you want to declare. If you want to declare no instances, then the for_each value should be an empty mapping.

You can use your existing for expression to also filter out elements from the source collection, by adding the optional if clause:

  for_each = {
    for service in var.services : service.us_name => service
    if var.ecs_autoscale
  }

This for expression will only include elements where var.ecs_autoscale is true. Since this expression doesn’t refer to the local variable service it cannot make different decisions for each element, so in practice it will either keep all of the elements or filter them all out, and if it filters them all out then the result will be zero elements as you wanted.

Hello, thank you all for your support.