Bootstrap FreeKB - Amazon Web Services (AWS) - List Network Interfaces using Terraform
Amazon Web Services (AWS) - List Network Interfaces using Terraform

Updated:   |  Amazon Web Services (AWS) articles

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── network_interfaces(directory)
│   ├── network_interfaces.tf
│   ├── outputs.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

And network_interfaces.tf could have something like this.

data "aws_network_interface" "network_interface" {
  id = "eni-0123456789abcdefg"
}

 

Or, you can use filter to find matching Network Intefaces. The filters must match one and only one Network Interface.

data "aws_network_interface" "network_interface" {
  filter {
    name   = "interface-type"
    values = ["network_load_balancer"]
  }
  filter {
    name   = "subnet-id"
    values = ["subnet-123456789012abdefg"]
  }
}

 

And outputs.tf could have something like this.

output "network_interface" {
  value = data.aws_network_interface.network_interface
}

 

You may need to reissue the terraform init command.

terraform init

 

The terraform refresh command can be used to see what Terraform will try to do.

terraform refresh

 

And something like this should be outputed.

network_interface = {
  "arn" = "arn:aws:ec2:us-east-1:123456789012:network-interface/eni-07a2b417b8527403c"
  "association" = tolist([
    {
      "allocation_id" = "eipalloc-0cf6b37af32290a41"
      "association_id" = "eipassoc-046dd87dc429e3ba3"
      "carrier_ip" = ""
      "customer_owned_ip" = ""
      "ip_owner_id" = "123456789012"
      "public_dns_name" = "ec2-50-16-63-227.compute-1.amazonaws.com"
      "public_ip" = "50.16.63.227"
    },
  ])
  "attachment" = tolist([
    {
      "attachment_id" = "ela-attach-0dcf0581cf6bbf483"
      "device_index" = 1
      "instance_id" = ""
      "instance_owner_id" = "amazon-aws"
    },
  ])
  "availability_zone" = "us-east-1d"
  "description" = "ELB net/network-load-balancer/19dd9f59a3a084d8"
  "filter" = toset(null) /* of object */
  "id" = "eni-07a2b417b8527403c"
  "interface_type" = "network_load_balancer"
  "ipv6_addresses" = toset([])
  "mac_address" = "0e:6a:5f:6b:27:f1"
  "outpost_arn" = ""
  "owner_id" = "123456789012"
  "private_dns_name" = "ip-172-31-47-140.ec2.internal"
  "private_ip" = "172.31.47.140"
  "private_ips" = tolist([
    "172.31.47.140",
  ])
  "requester_id" = "273749504915"
  "security_groups" = toset([])
  "subnet_id" = "subnet-03f11417780f6cdbc"
  "tags" = tomap({})
  "timeouts" = null /* object */
  "vpc_id" = "vpc-014d2fcfa335d3c01"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 59b07b in the box below so that we can be sure you are a human.