Unable to call the output IP of eni to the input of ELB target id in terraform

615 Views Asked by At

I'm new to terraform, trying to get the IPs of NLB using the below code. I got two IPS as output.

data "aws_network_interfaces" "this" {

  filter {
    name = "description"
    values = ["ELB net/${aws_lb.example.name}/*"]
}

  filter {
    name = "vpc-id"
    values = ["${aws_vpc.example.id}"]
  }

  filter {
    name = "status"
    values = ["in-use"]
  }


  filter {

    name = "attachment.status"
    values = ["attached"]
  }
}

locals {
  nlb_interface_ids = "${flatten(["${data.aws_network_interfaces.this.ids}"])}"
}

data "aws_network_interface" "ifs" {
  count = 2
  id = "${local.nlb_interface_ids[count.index]}"
}

output "aws_lb_network_interface_ips" {
  value = "${flatten([data.aws_network_interface.ifs.*.private_ips])}"
}

I want to use the output of network interface IPs to the input of each target id(one IP each in target id) in the below code. I'm getting the error on .terraform/modules/aws_lb-public.aws_alb/main.tf line 142, in resource "aws_lb_target_group_attachment" "this": │ 142: target_id = each.value.target_id │ ├──────────────── │ │ each.value.target_id is tuple with 2 elements

I think the way i reference the output IP there is some mistake. Appreciate if anyone can help me.

module "aws_lb-public" {
 source = "./xxxx" 
  name = "abc"
  load_balancer_type = "application"
  internal      = true
  vpc_id             = aws_vpc.example.id
  subnets = ["aws_subnet.example.id","aws_subnet.example.id"]
  security_groups    = [aws_security_group.example.id]
  target_groups = [
    {
      name_prefix      = "t1"
      backend_protocol = "HTTP"
      backend_port     = 80
      target_type      = "ip"
      targets = [
        {
        target_id =  "${flatten([data.aws_network_interface.ifs.[0].private_ips])}"
          port = 80
} 
  ] 
}

  ]
    http_tcp_listeners = [

{      
      port               = 80`           
      protocol           = "HTTP"
      target_group_index = 0
 }
] 
}
````````````````````````````````````````````````````````````````````
1

There are 1 best solutions below

0
Marcin On

You have to iterate over your IPs:

targets = [ for ip in flatten([data.aws_network_interface.ifs.[0].private_ips]):
        {
           target_id = ip
           port = 80
        }
  ]