For_each aws_instance aws_lb_target_group_attachment

I’m trying to utilize terraform to create four aws instance with for_each, attach with existing aws_lb_target_groups and then register instances using the resource: aws_lb_target_group_attachment.
excluding the test02 instance in the web_target_group map.

Below you find the configuration files.

Please advise.

Thanks

Michael

main.tf
      data "cloudinit_config" "example" {
        for_each = var.servers
      
        part {
          filename     = "common.sh"
          content_type = "text/x-shellscript"
          content = templatefile("${path.cwd}/scripts/common.sh", {
          })
        }
        part {
          filename     = "configure.sh"
          content_type = "text/x-shellscript"
          content = templatefile("/home/ec2-user/stage2/scripts/${each.value.appl_name}", {
          })
        }
      }
      
      resource "aws_instance" "server" {
        for_each = var.servers
        ami = each.value.ami_id
        instance_type = each.value.instance_type
        subnet_id = each.value.subnet_id
        security_groups = [ "sg-04b403faf33f968ad" ]
        key_name = var.key_name
      
      user_data = data.cloudinit_config.example[each.key].rendered
      
        tags = {
          Name = "${each.value.environment}-server"
        }
      }
      #
      #
      #
      resource "aws_lb_target_group_attachment" "web_tg" {
        for_each = { for k in compact([for k, v in var.web_target_group: v.condition ? k : ""]): k => var.web_target_group[k] }
        target_group_arn = aws_lb_target_group.test.arn
        target_id        = aws_instance.test.id
        port             = 80
      }
      
      output "instances" {
        value = tomap({
          for k, inst in aws_instance.server : k => {
            name              = "${keys(var.servers)[*]}"
            id                = inst.id
          }
      })
    }
#
#
variables.tf
variable "servers" {
  description = "Map of servers names to configuration."
  type        = map(any)

  default = {
    client-webapp = {
      ami_id                  = "ami-06640050dc3f556bb",
      instance_type           = "t2.micro",
      subnet_id               = "subnet-02ef6d33efadfef5a"
      environment             = "GritfyApp"
      appl_name               = "GritfyApp-test.tpl"
    },
    internal-webapp = {
      ami_id                  = "ami-057b76da478f6105c",
      instance_type           = "t3.micro",
      subnet_id               = "subnet-02ef6d33efadfef5a"
      environment             = "GrityWeb-dev"
      appl_name               = "GrityWeb-dev.tpl"
    },
    west-webapp = {
      ami_id                  = "ami-057b76da478f6105c",
      instance_type           = "t3.micro",
      subnet_id               = "subnet-02ef6d33efadfef5a"
      environment             = "west-dev"
      appl_name               = "GrityWeb-dev.tpl"
    },
    east-webapp = {
      ami_id                  = "ami-057b76da478f6105c",
      instance_type           = "t3.micro",
      subnet_id               = "subnet-02ef6d33efadfef5a"
      environment             = "east-dev"
      appl_name               = "GrityWeb-dev.tpl"
    }
  }
}

variable "web_target_group" {
   description = "Map of target groups."
      type = map(object({
         target_group     = string
                 target_group_arn = string
         port             = number
         condition        = bool
      }))
    default = {
        test01 = {
                   port = 22
                   target_group = "target01"
                   target_group_arn = "aws_lb_target_group.test01.arn"
                   condition = true
                },
                test02 = {
                   port = 22
                   target_group = "target02"
                   target_group_arn = "aws_lb_target_group.test02.arn"
                   condition = false
                },
                test03 = {
                   port = 22
                   target_group = "target03"
                   target_group_arn = "aws_lb_target_group.test03.arn"
                   condition = true
                },
                test04 = {
                   port = 22
                   target_group = "target04"
                   target_group_arn = "aws_lb_target_group.test04.arn"
                   condition = true
                },
    }
}

variable "aws_region" {
  description = "AWS region for all resources."
  type        = string
  default     = "us-east-2"
}

variable "key_name" {
  description = "Key Name"
  type        = string
  default     = "em-test"
}

variable "security_group_id" {
  description = "ID of existing security group whose rules we will manage"
  type        = string
  default     = "sg-04b403faf33f968ad"
}