Looped tftplTemplate used in merge statement

  targets = {
    my-target = local.target_nodes

coming from locals:

locals {
  my_nodes = [
    for n in range(5) : templatefile("./templates/my_template.tftpl", {
      index = n
    })
}

eventuelly being merged in tf module code:

eventbridge_targets = flatten([
    for index, rule in var.rules : [
      for target in var.targets[index] :
      merge(target, {
        "rule" = index
        "Name" = var.append_rule_postfix ? "${replace(index, "_", "-")}-rule" : index
      })
    ] if length(var.targets) != 0
  ])

the merge in the module fails with the error:

 Call to function "merge" failed: arguments must be maps or objects, got
│ "string".

the module code is not the issue here, (its a often used module terraform-aws-eventbridge/main.tf at 5a935234c6d7c528ed532e4b9ba7122bfe04dfd1 · terraform-aws-modules/terraform-aws-eventbridge · GitHub)

but as you can see i want to give it a generated list of targets

the tftpf contains valid HCL

[{ 
   name = "test${index}"
   ecs_target = {
          launch_type             = "FARGATE"
          task_count              = 1
          task_definition_arn     = .................
.......etc
 }, { <another target like above} ]

not sure what the proper implementation should be to avoid the error and give a valid structure / object to the module.
Help is appreciated(!)

Not sure if it’s your issue this time, but I’ve been bitten a few times by not using the spread operator (...) after merge(), so maybe see if adding that helps?

locals {
  regional_buckets = {
    "foo" = ["us-central1"],
    "bar" = ["us-west1"]
  }
}

output "test" {
  value = merge([
    for name, regions in local.regional_buckets : {
      for region in regions : "${name}-${region}" => {
        "region" = upper(region)
      }
    }
  ]...)
}

results in output

test = {
  "bar-us-west1" = {
    "region" = "US-WEST1"
  }
  "foo-us-central1" = {
    "region" = "US-CENTRAL1"
  }
}

Without spread operator:

│ Call to function "merge" failed: arguments must be maps or objects, got "tuple".