Home > OS >  How do I make a target group listen on a different port than instances being attached via an EC2 aut
How do I make a target group listen on a different port than instances being attached via an EC2 aut

Time:09-02

I'm setting up an autoscaling group with a launch template in AWS via Terraform.

When I do it like this, the Target Group expects traffic to come in at port 8080 and then it attaches the machines to the Target Group with port 8080.

I would like the Target Group to listen on port 80 but attach instances on port 8080.

resource "aws_lb_target_group" "this" { 
  // ... other config
  target_type = "instance"
  port = 8080
}

resource "aws_autoscaling_group" "this" {
  // ... other config
  target_group_arns = [aws_lb_target_group.this.arn]
}

CodePudding user response:

You should leave the target group alone. That setting just tells the target group what port it should connect to on the target instances.

If you want to be able to send traffic to this at port 80, you just need to setup the Load Balancer listener on port 80. The load balancer listener is the thing an external traffic source interacts with. The load balancer listener is where you set the port that will be exposed externally. The load balancer will accept traffic at the listener port, and then forward that traffic to one of the instances in the target group, on the port configured in the target group.

  • Related