How to get instance id for my module ec2 when I create using for_each and I need instance_id for my module tgroup to assign or join new instance in specific target group.
My module ec2
resource "aws_instance" "ProductionServer" {
count = length(var.SubnetServer)
ami = module.CreateAMI.AMIId.id
instance_type = var.TypeServer
subnet_id = var.SubnetServer[count.index]
}
ec2/outputs.tf
output "InstanceId" {
value = aws_instance.ProductionServer
description = "get value id"
}
and my module tgroup
resource "aws_alb_target_group_attachment" "TgProdRegister" {
for_each = module.GetInstanceId.InstanceId.id
target_group_arn = var.TgroupArn
target_id = each.value
port = var.TgPort
depends_on = [ module.GetInstanceId ]
}
and GetInstanceId is refer to module ec2