Follow-up on the topic - resolving ALB internal (/...
# office-hours
t
Follow-up on the topic - resolving ALB internal (/private) IPs inside VPC - Based on our last discussion I ran tests and was still never able to get anything other than public IP addresses from inside my VPC... I verified I was using the VPCs .2 resolver and that the DNS options were enabled for my VPC. (I did not re-test in a "default" VPC) I also did a bunch of googling, and searching thru forums. I did discover AWS provides some DNS prefixes for ALBs - the all.* prefix returns all IPs for a loadbalancer (perhaps more than what are currently active) and <regionalzoneid>.* prefix (like us-east-1c.abc.com) which resolves the ALBs IPs only for the given zone. I then filed a support request to try to get an answer on the ALB private address resolution and the AWS response was - "*Unfortunately, there are currently no DNS method you can use to get the private ip's of a public load balancer. The dns of ALB will resolve to public ips only.* *In order to get the private IP's, you can find them in the "network interfaces" tab from the ENI's of the ALB*" Based on the discussion in office hours I'm expecting the private/public resolution feature may only apply to EC2 instance DNS, and not to ALB DNS. Providing this info here with the hope it helps someone in the future. Thanks for the suggestions on trying to get this to work. Regards, Tim
a
Copy code
# Find the NLB by tag
data "aws_lb" "nlb" {
  tags = {
    "kubernetes.io/service-name" = ".........."
  }
}

# Find the NLB private IPs by searching/filtering the ENIs
locals {
  nlb_name = data.aws_lb.nlb.name
  nlb_arn  = data.aws_lb.nlb.arn
  nlb_id   = element(reverse(split("/", local.nlb_arn)), 0)
}

data "aws_network_interfaces" "nlb" {
  filter {
    name   = "description"
    values = [format("ELB net/%s/%s", local.nlb_name, local.nlb_id)]
  }
}

locals {
  nlb_network_interface_ids = sort(data.aws_network_interfaces.nlb.ids)
}

# Lookup a network interface in each AZ to find its private IP
data "aws_network_interface" "nlb" {
  count = length(var.availability_zones)

  id = local.nlb_network_interface_ids[count.index]
}

locals {
  nlb_network_interface_private_ips = data.aws_network_interface.nlb[*].private_ip
}

# Attach the NLB private IPs to the public ALB Target Group
resource "aws_lb_target_group_attachment" "nlb" {
  count = length(local.nlb_network_interface_private_ips)

  target_group_arn  = var.alb_target_group_arn
  target_id         = local.nlb_network_interface_private_ips[count.index]
  availability_zone = "all"
  port              = var.kubernetes_service_port
}
something like this works to find the private IPs of an internal NLB (can be used for ALB as well)
t
Nice example! I guess a risk is that if it scaled out we would need to re-run the terraform to collect new IPs? (99.9% of the cases that TF would be fine... and if my ALBs are scaling out I may have other problems ;))