Resource: aws_network_interface

Provides an Elastic network interface (ENI) resource.

Example Usage

resource "aws_network_interface" "test" {
  subnet_id       = aws_subnet.public_a.id
  private_ips     = ["10.0.0.50"]
  security_groups = [aws_security_group.web.id]

  attachment {
    instance     = aws_instance.test.id
    device_index = 1
  }
}

Example of Managing Multiple IPs on a Network Interface

By default, private IPs are managed through the private_ips and private_ips_count arguments which manage IPs as a set of IPs that are configured without regard to order. For a new network interface, the same primary IP address is consistently selected from a given set of addresses, regardless of the order provided. However, modifications of the set of addresses of an existing interface will not alter the current primary IP address unless it has been removed from the set.

In order to manage the private IPs as a sequentially ordered list, configure private_ip_list_enabled to true and use private_ip_list to manage the IPs. This will disable the private_ips and private_ips_count settings, which must be removed from the config file but are still exported. Note that changing the first address of private_ip_list, which is the primary, always requires a new interface.

If you are managing a specific set or list of IPs, instead of just using private_ips_count, this is a potential workflow for also leveraging private_ips_count to have AWS automatically assign additional IP addresses:

  1. Comment out private_ips, private_ip_list, private_ip_list_enabled in your configuration
  2. Set the desired private_ips_count (count of the number of secondaries, the primary is not included)
  3. Apply to assign the extra IPs
  4. Remove private_ips_count and restore your settings from the first step
  5. Add the new IPs to your current settings
  6. Apply again to update the stored state

This process can also be used to remove IP addresses in addition to the option of manually removing them. Adding IP addresses in a manually is more difficult because it requires knowledge of which addresses are available.

Argument Reference

The following arguments are required:

The following arguments are optional:

Attachment

The attachment block supports the following:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Network Interfaces using the id. For example:

import {
  to = aws_network_interface.test
  id = "eni-e5aa89a3"
}

Using terraform import, import Network Interfaces using the id. For example:

% terraform import aws_network_interface.test eni-e5aa89a3