amazon-web-servicesterraformterraform-provider-awsterraform0.12+

Terraform AWS: How to use the Data Source to get the network interfaces with AZ info?


I would like to use the data source to get network interface with AZ info as follows:

primary_network_interface_id = {
  "us-west-2a" = "eni-00ad0ffc75524ebba"
  "us-west-2b" = "eni-0200c4f55429d5c14"
  "us-west-2c" = "eni-041c8aa9ed660271f"
}

I use data "aws_network_interfaces". The code as follows outputs the network interface without AZ info.

data "aws_network_interfaces" "example" {
  for_each = toset(data.terraform_remote_state.vpc.outputs.nat_subnet_ids)
  filter {
    name   = "subnet-id"
    values = ["${each.value}"]
  }
}

output "example" {
  value = data.aws_network_interfaces.example
}

Outputs:

example = {
  "subnet-03502515b27bc151f" = {
    "filter" = toset([
      {
        "name" = "subnet-id"
        "values" = tolist([
          "subnet-03502515b27bc151f",
        ])
      },
    ])
    "id" = "us-west-2"
    "ids" = tolist([
      "eni-0200c4f55429d5c14",
    ])
    "tags" = tomap(null) /* of string */
    "timeouts" = null /* object */
  }
  "subnet-0511908f70482931e" = {
    "filter" = toset([
      {
        "name" = "subnet-id"
        "values" = tolist([
          "subnet-0511908f70482931e",
        ])
      },
    ])
    "id" = "us-west-2"
    "ids" = tolist([
      "eni-041c8aa9ed660271f",
    ])
    "tags" = tomap(null) /* of string */
    "timeouts" = null /* object */
  }
  "subnet-0ad3f2d62cefad83a" = {
    "filter" = toset([
      {
        "name" = "subnet-id"
        "values" = tolist([
          "subnet-0ad3f2d62cefad83a",
        ])
      },
    ])
    "id" = "us-west-2"
    "ids" = tolist([
      "eni-00ad0ffc75524ebba",
    ])
    "tags" = tomap(null) /* of string */
    "timeouts" = null /* object */
  }
}

The data "aws_network_interface" has the availability zone info. But I have no clue how to use it. Need help.


Solution

  • So the trick is to iterate over the network interfaces returned by aws_network_interfaces and for each one, fetch its details using aws_network_interface. The availability zone information can then be extracted from these details.

    Here's a sample code:

    data "aws_network_interfaces" "example" {
      for_each = toset(data.terraform_remote_state.vpc.outputs.nat_subnet_ids)
      filter {
        name   = "subnet-id"
        values = [each.value]
      }
    }
    
    data "aws_network_interface" "example" {
      for_each = toset(flatten([for v in data.aws_network_interfaces.example : v.ids]))
      id = each.value
    }
    
    output "example" {
      value = { for k, v in data.aws_network_interface.example : k => v.availability_zone }
    }
    

    Specific names and identifiers used in the code might need to be adapted to your actual use case.

    It will output a map of network interface ids to availability zones, rather than subnet ids to network interface ids. If you need to maintain a relationship between subnet ids and network interface ids, you might need to construct a more complex data structure in your output.

    This will make additional API requests, one for each network interface. If you have a large number of interfaces, it could run into API rate limiting issues.