srikanth Nakka
srikanth Nakka

Reputation: 81

Change Primary Elastic Network Interface(ENI) between two EC2 Instances

Can we change Primary Elastic Network Interface from Ec2 Instance to another Ec2?

I'm able to swap the secondary ENI's between the Ec2 Instances, but not able to change the primary ENI from one Ec2 instance to the other.

Is it possible to change the primary ENI?

Upvotes: 8

Views: 20899

Answers (3)

user1302884
user1302884

Reputation: 843

Not sure if this exactly answers your question. I would like to make a distinction here between two terms. Primary/Secondary and Preferred. A secondary interface can be made the preferred one.

Let's say there are two EC2 instances A and B. Each having a primary ENI - ENI_A and ENI_B which are the preferred ENIs used by the respective operating system of A and B to route traffic. Now one needs to create another ENI, lets say ENI_C (eth1), which can be used for migration. This ENI_C can be de-attached from instance A and attached to another instance B or vice versa (as you rightly do).

ENI_C has to be made the preferred network interface to route traffic whenever it is attached to any instance.

This can be achieved by creating a new routing table. Adding a route there to the default gateway and specify a condition when to use the new routing table.

Example:

echo "1   test" >> /etc/iproute2/rt_tables

ip rule add from 10.1.0.0/24 table test                # When to use test routing table

ip route add default via 10.1.0.1 dev eth1 table test  # The default gateway

You can also achieve the desired effect by adding a high priority default route through ENI_C by setting metric value to lower value than 100

route add -net default gw 10.1.0.1 netmask 0.0.0.0 dev eth1 metric 50  

Change the IPs above accordingly.

To test if the ENI_C (eth1) is your preferred NIC for sending traffic lets see which NIC is used to send traffic for example to Google DNS (8.8.8.8)

ip route get 8.8.8.8

Upvotes: 1

Chris
Chris

Reputation: 101

Instead of moving a primary ENI, you can swap out almost everything else (eg block volumes, user data & attributes).

Like in my case I wanted to replace my old instance with a Dockerized version of it, but wanted to reuse its old IP address.

I managed to do this by stopping both the old & new instance. Deattached the block volumes on both instances. Attached the new block volume onto the old instance. Updated the old instance's user data to match that of the new one. Updated the old instance type to match that of the new one. And I also had to enable ENA support by executing the following on a different host:

aws ec2 modify-instance-attribute --instance-id <instance id> --ena-support

And then I started the old instance again.

Upvotes: 1

Michael - sqlbot
Michael - sqlbot

Reputation: 179284

This isn't possible. The Primary ENI is locked to the instance for the lifetime of the instance, even if the instance is stopped.

Every instance in a VPC has a default network interface, called the primary network interface (eth0). You cannot detach a primary network interface from an instance.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html

Upvotes: 9

Related Questions