A Complete Guide to Virtual Network Interfaces for EC2 Instances
An Elastic Network Interface (ENI) is a virtual network interface that you can attach to an EC2 instance in a VPC. It serves as a logical networking component that represents a virtual network card and can include attributes such as IP addresses, security groups, and MAC addresses.
ENIs can have multiple IP addresses assigned to them:
# Example IP configuration Primary IPv4: 10.0.1.25 Secondary IPv4: 10.0.1.26, 10.0.1.27 Elastic IP: 54.123.45.67 (mapped to 10.0.1.25) IPv6: 2001:db8:1234:1a00::123
ENIs can be associated with security groups to control inbound and outbound traffic:
# Example security group rule Protocol: TCP Port Range: 443 Source: 0.0.0.0/0 Description: Allow HTTPS from anywhere
Additional properties of ENIs include:
# Example ENI attributes MAC: 02:1a:2b:3c:4d:5e Source/Dest Check: Enabled Description: "Web server primary interface" Device Index: 0 Delete on Termination: False
# Create a new ENI
aws ec2 create-network-interface \
--subnet-id subnet-12345678 \
--description "Web server secondary interface" \
--groups sg-12345678 \
--private-ip-address 10.0.1.25
# Create ENI with multiple private IP addresses
aws ec2 create-network-interface \
--subnet-id subnet-12345678 \
--description "Multi-IP interface" \
--groups sg-12345678 \
--private-ip-addresses \
PrivateIpAddress=10.0.1.25,Primary=true \
PrivateIpAddress=10.0.1.26 \
PrivateIpAddress=10.0.1.27
Attach additional ENIs when launching an EC2 instance:
Attach an existing ENI to a running or stopped instance:
Using AWS CLI:
# Attach ENI to running instance aws ec2 attach-network-interface \ --network-interface-id eni-12345678 \ --instance-id i-12345678 \ --device-index 1 # Detach ENI from instance aws ec2 detach-network-interface \ --attachment-id eni-attach-12345678
Add, remove, or modify IP addresses associated with an ENI:
# Assign secondary private IP address aws ec2 assign-private-ip-addresses \ --network-interface-id eni-12345678 \ --private-ip-addresses 10.0.1.28 # Assign automatically allocated IP addresses aws ec2 assign-private-ip-addresses \ --network-interface-id eni-12345678 \ --secondary-private-ip-address-count 2 # Unassign private IP address aws ec2 unassign-private-ip-addresses \ --network-interface-id eni-12345678 \ --private-ip-addresses 10.0.1.28 # Associate Elastic IP with ENI aws ec2 associate-address \ --network-interface-id eni-12345678 \ --private-ip-address 10.0.1.25 \ --allocation-id eipalloc-12345678
Change various ENI properties:
# Modify description aws ec2 modify-network-interface-attribute \ --network-interface-id eni-12345678 \ --description "Updated description" # Modify security groups aws ec2 modify-network-interface-attribute \ --network-interface-id eni-12345678 \ --groups sg-12345678 sg-87654321 # Enable/disable source/destination check aws ec2 modify-network-interface-attribute \ --network-interface-id eni-12345678 \ --no-source-dest-check # Change attachment delete-on-termination aws ec2 modify-network-interface-attribute \ --network-interface-id eni-12345678 \ --attachment AttachmentId=eni-attach-12345678,DeleteOnTermination=true
Create instances with interfaces in multiple subnets for specialized networking requirements:
# CloudFormation example for multi-homed instance
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-12345678
InstanceType: t3.large
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref PublicSubnet
GroupSet:
- !Ref WebSecurityGroup
AssociatePublicIpAddress: true
Tags:
- Key: Name
Value: Multi-homed Server
BackendInterface:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: !Ref PrivateSubnet
GroupSet:
- !Ref DatabaseSecurityGroup
Description: Interface for database access
SourceDestCheck: true
Tags:
- Key: Name
Value: DB Interface
InterfaceAttachment:
Type: AWS::EC2::NetworkInterfaceAttachment
Properties:
InstanceId: !Ref WebServerInstance
NetworkInterfaceId: !Ref BackendInterface
DeviceIndex: 1
Use ENIs to create network appliances like firewalls, load balancers, or NAT instances:
# Disable source/destination check for NAT instance aws ec2 modify-network-interface-attribute \ --network-interface-id eni-12345678 \ --no-source-dest-check # Route table configuration for NAT instance aws ec2 create-route \ --route-table-id rtb-12345678 \ --destination-cidr-block 0.0.0.0/0 \ --network-interface-id eni-12345678
Example network appliance architecture:
Use ENIs to implement IP mobility for high availability and failover scenarios:
# Detach ENI from failed instance aws ec2 detach-network-interface \ --attachment-id eni-attach-12345678 # Attach ENI to standby instance aws ec2 attach-network-interface \ --network-interface-id eni-12345678 \ --instance-id i-87654321 \ --device-index 1
Example architecture for high availability using ENIs:
Benefits:
The number of ENIs you can attach to an instance depends on the instance type:
| Instance Type | Max ENIs | IP Addresses per ENI |
|---|---|---|
| t3.micro | 2 | 2 |
| m5.large | 3 | 10 |
| c5.xlarge | 4 | 15 |
| r5.4xlarge | 8 | 30 |
Netflix uses ENIs extensively in their microservices architecture to implement network segmentation and security. They leverage multiple ENIs on their EC2 instances to separate different types of traffic (control plane vs. data plane) and apply different security policies to each interface. This helps them maintain strict security boundaries while allowing their services to communicate efficiently.
OpenAI uses ENIs to optimize network traffic between their AI training clusters. By configuring multiple ENIs on their high-performance computing instances, they can separate model training traffic from management traffic, ensuring that critical AI workloads have dedicated network paths. This helps them achieve the massive scale required for training large language models like GPT.
Prime Video's content delivery infrastructure uses ENIs with multiple IP addresses to handle high-volume streaming traffic. Their architecture leverages ENIs to implement failover mechanisms that can quickly redirect traffic if an instance becomes unhealthy, ensuring continuous streaming service for millions of users worldwide.
Audible uses ENIs to implement network segmentation in their multi-tier application architecture. By using separate ENIs for web, application, and database tiers, they can apply specific security groups to each layer, enhancing their security posture while maintaining the flexibility to scale each tier independently.
Use multiple ENIs to create network security zones on a single instance:
# Security group for public-facing ENI aws ec2 create-security-group \ --group-name web-sg \ --description "Web server security group" \ --vpc-id vpc-12345678 aws ec2 authorize-security-group-ingress \ --group-id sg-web \ --protocol tcp \ --port 443 \ --cidr 0.0.0.0/0 # Security group for internal ENI aws ec2 create-security-group \ --group-name app-sg \ --description "Application security group" \ --vpc-id vpc-12345678 aws ec2 authorize-security-group-ingress \ --group-id sg-app \ --protocol tcp \ --port 8080 \ --source-group sg-internal-lb
Implement high availability using ENI mobility:
#!/bin/bash
# Simple failover script
PRIMARY_INSTANCE="i-12345678"
STANDBY_INSTANCE="i-87654321"
ENI_ID="eni-12345678"
ATTACHMENT_ID="eni-attach-12345678"
# Check if primary is healthy
if ! aws ec2 describe-instance-status --instance-id $PRIMARY_INSTANCE --query "InstanceStatuses[0].InstanceStatus.Status" | grep -q "ok"; then
echo "Primary instance unhealthy, initiating failover"
# Detach ENI from primary
aws ec2 detach-network-interface --attachment-id $ATTACHMENT_ID
# Wait for detachment to complete
aws ec2 wait network-interface-available --network-interface-ids $ENI_ID
# Attach to standby
aws ec2 attach-network-interface \
--network-interface-id $ENI_ID \
--instance-id $STANDBY_INSTANCE \
--device-index 1
echo "Failover complete"
fi
Use ENIs with multiple IP addresses to host containers:
# Assign multiple IPs to an ENI aws ec2 assign-private-ip-addresses \ --network-interface-id eni-12345678 \ --secondary-private-ip-address-count 10 # Docker run with specific IP (on the instance) docker run --net=host --ip=10.0.1.25 nginx
Use ENIs to implement network monitoring and traffic inspection:
# Create traffic mirror target aws ec2 create-traffic-mirror-target \ --network-interface-id eni-12345678 \ --description "Security monitoring target" # Create traffic mirror filter aws ec2 create-traffic-mirror-filter \ --description "Monitor all traffic" # Add filter rule aws ec2 create-traffic-mirror-filter-rule \ --traffic-mirror-filter-id tmf-12345678 \ --traffic-direction ingress \ --rule-number 100 \ --rule-action accept \ --protocol 17 \ --source-cidr-block 10.0.0.0/16 \ --destination-cidr-block 0.0.0.0/0 # Create traffic mirror session aws ec2 create-traffic-mirror-session \ --network-interface-id eni-source \ --traffic-mirror-target-id tmt-12345678 \ --traffic-mirror-filter-id tmf-12345678 \ --session-number 1
| Feature | Elastic Network Interfaces | Elastic Load Balancing | Elastic IP Addresses | Elastic Fabric Adapter |
|---|---|---|---|---|
| Primary Use Case | Network interface management | Traffic distribution | Static public IP addressing | High-performance computing |
| Availability Zone Scope | Single AZ | Multiple AZs | Regional | Single AZ |
| IP Addressing | Multiple private & public IPs | Single DNS name | Single public IP | Uses ENI addressing |
| Security Controls | Security Groups | Security Groups & WAF | None (uses ENI/SG) | Security Groups |
| Failover Support | Manual (ENI movement) | Automatic | Manual reassignment | None |
| Performance | Instance-dependent | Auto-scaling | N/A | Ultra-high performance |