Search code examples
amazon-web-servicesterraformamazon-ecstargetamazon-elb

AWS registering a loadbalancer and ECS with a targetgroup


I am using terraform to register a target group with a LoadBalancer. As I understand it. I use the resource aws_lb_target_group_attachment with the vars

target_group_arn which is set to the arn of the target group"

target_id not sure what to set this to.

target_group_arn is the arn of the target group. But I am not sure what target_id is, looking at the docs it says

The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container.

So I am guessing its referring to the ECS container. My target is an instance. But when I look at my ECS cluster there is no ID on the console.

I have created the cluster, and I have created 3 service. Neither have an ID.

What is it that I am missing, or I need to create.

Thanks


Solution

  • I am using terraform to register a target group with a LoadBalancer. As I understand it. I use the resource aws_lb_target_group_attachment

    The aws_lb_target_group_attachment resource is for registering targets with the target group. You could use this to manually register ECS containers with the target group, however ECS can manage this target group relationship for you. By allowing ECS to manage it for you, any time a container is added, such as during auto-scaling, it will be automatically added to the load balancer by ECS.


    You will need to create an aws_lb, and an aws_lb_target_group. Then create an aws_lb_listener which is assigned to the aws_lb, and has an action that forwards requests to the aws_lb_target_group. The listener "connects" the target group to the load balancer.

    Now in your aws_ecs_service definition, you need to create a load_balancer block which tells ECS how to register a specific container with the target group. By doing it this way ECS will manage registering all instances of that container (in all the service's tasks) with the target group.