0

I'm trying to set up ECS with dynamic port mapping, Application Load Balancer and Auto Scaling Group. It was all working fine, until I changed hostPort in the task definition to 0.

Now in the target groups, targets are being registered with the container port, for example 80 or 8080 instead of dynamically allocated port and because of that health checks are failing since the application does not work on this host port. After that, target with correct port is registered, but since the other targets'(with the same instance ID) health checks failed, the instance goes into draining state.

For now, I've changed health check type to EC2, I still have two registered targets, one unhealthy with container port and one healthy with dynamically allocated port, but the instance is running.

I have all ports opened in my EC2 security group, I'm using default network mode(which in this case is bridge) and I have traffic port as my setting in the target group. I'm also using spot instances in my ASG but I don't think that should make any difference. What can be the problem here?

Thank you for your help!

Flyce
  • 1
  • 1

1 Answers1

0

To "force" target group to use dynamic container port, you have to set "traffic port" in healthcheck information.

Supposing you're using terraform:

resource aws_alb_target_group this {
  name = "webapi"
  port = 80 # this is the "public" port, that means, container port mapped to outside
  protocol = "HTTP"
  target_type = "instance" # this also plays a role. You have to put instance, otherwise you'll need to inform port in healthcheck
  vpc_id = var.vpc_id
  health_check {
    enabled = true
    path = "/api/version"
    interval = 30
    protocol = "HTTP"
    healthy_threshold = 3
    unhealthy_threshold = 3
    port = "traffic-port" # this is the default value
  }
}