How do I reference an ECS container ID as a target_id in an ALB target group attachment?

Hi,

I am trying to attach an ECS Service (thus its container task) to a Target Group on an ALB.

The documentation at https://www.terraform.io/docs/providers/aws/r/lb_target_group_attachment.html#argument-reference states that with aws_alb_target_group_attachment, the target_id argument is Required, but that it may be ‘the Instance ID for an instance, or the container ID for an ECS container’.

Does anyone know how I obtain the container ID to tell Terraform to map this Service’s container to the target group?

I know that another way is to use a separate listener per Service, wherein I can then define the ‘default’ target group, and it should Just Work, like what happened for this person:

However, I don’t want a separate Listener on my ALB per Service, as they’ll each need a different port.

I am trying to do:

ALB -> Listener port 443 -> Target Group A -> Service A -> Task 1
ALB -> Listener port 443 -> Target Group B -> Service B -> Task 1

where the ALB is the same in both cases

… and then will use Target Group Rules to forward traffic to the relevant Target Group (and thus container) based on different hostnames (that all resolve to the ALB).

Try as I might, I can’t figure out how to define the container ID as the target_id argument to aws_lb_target_group_attachment in Terraform language.

Help much appreciated!

I solved it in a different way - I associate my Service’s ALB Listener Rules to the Target Group, which automatically associated the Target Group with the ALB and then I didn’t need to use aws_lb_target_group_attachment at all.