In this setup, is it possible to attach all 12 instance all together in aws_lb_target_group_attachment. I tried count, but it is throwing error saying we cannot use both. Then I tried target_id = [for ids in aws_instance.instance : ids]
, here also error as it is tuple and string is expected. Even tried to convert each value to string. Is there any way to do it.
count = length(data.aws_ami_ids.api_mainapp_ami.*.ids)
ami = element(data.aws_ami_ids.api_mainapp_ami.*.ids[count.index], 12)
instance_type = "t3.xlarge"
associate_public_ip_address = true
vpc_security_group_ids = [var.secgroup]
subnet_id = "${element(var.vpc_subnets_priv_id, count.index)}"
tags = {
Name = "${var.component}-${count.index+1}"
}
}
LB.....
variable "port_forwarding_config" {
default = {
8900 = "TCP"
7879 = "TCP"
80 = "TCP"
9690 = "TCP"
}
}
resource "aws_lb_target_group_attachment" "tg-attachment" {
for_each = var.port_forwarding_config
target_group_arn = "${aws_lb_target_group.tg[each.key].arn}"
port = each.key
target_id = "${aws_instance.instance[1].id}"
}```