[AWS] Get the IP adresses of a VPC endpoint using terraform

Hi,

In my AWS project i want to have a list of the IP adresses of a VPC endpoint using terraform.

Terraform provides a data_source to get the Network interfaces of the endpoint :

data "aws_vpc_endpoint" "my_endpoint" {
  vpc_id       = my_vpc
  service_name = "my_service"
}

data.aws_vpc_endpoint.my_endpoint.network_interface_ids

and it provides also a data source to get the IP of an ENI :

data "aws_network_interface" "bar" { 
  id = "eni-01234567" 
}
data.aws_network_interface.bar.private_ip

The network_interface_ids datasource will return in my case 2 ENI because i’m deploying in 2 AZ.

Now what i want is to have the data_source of the ENI (where i get the IP) to be dynamic. I want it to loop over the ENI.

Any way to do that ?

Ok i find how to do it. Hope it can help :

data "aws_network_interface" "apigw_endpoint_eni" {
  for_each = data.aws_vpc_endpoint.api.network_interface_ids
  id       = each.value
}

resource "aws_lb_target_group_attachment" "apigw_endpoint_eip" {
  for_each         = data.aws_network_interface.apigw_endpoint_eni
  target_group_arn = aws_lb_target_group.ip.arn
  target_id        = each.value.private_ip
  port             = 443
}