How to use a conditional attribute inside a resource?

I’m creating Cloudwatch alarms, which have two conflicting attributes: statistic and extended_statistic. However, I’m creating them dynamically reading from a list of maps, resulting in the following code:

variables.tfvars:

    cloudwatch_alarms = {
      foo = {
        statistic         = "Average"
      }
      bar = {
        extended_statistic = "p60"
      }
    }

main.tf:

    resource "aws_cloudwatch_metric_alarm" "cloudwatch_alarms" {
      for_each                    = var.cloudwatch_alarms
      statistic                   = each.value["statistic"]
      extended_statistic          = each.value["extended_statistic"]
     // won't work, attribute conflict with "statistic"
    }

How could I have both attributes in my map and use them conditionally inside my resource?

You set the unused values to null (in your map) to conditionally set an attribute:

variable cloudwatch_alarms {
  default = {
    foo = {

      extended_statistic = null
      statistic = "Average"
    }
    bar = {
      statistic          = null
      extended_statistic = "p60"
    }
  }
}