11

When I try and associate a second Elastic IP to a small EC2 instance the currently assigned Elastic IP is disassociated.

Do I need to use a VPC?

Ian Warburton
  • 319
  • 2
  • 4
  • 12
  • This is a really good setp by step tutorial on EIP and network interfaces. From creating and adding network interface to associating it with second EIP and creating permanent ip routes. https://www.lisenet.com/2014/create-and-attach-a-second-elastic-network-interface-with-eip-to-ec2-vpc-instance/ – Marko Benko Aug 21 '15 at 11:58

2 Answers2

9

Classic EC2 instances can only have a single Elastic IP address associated with them. To get multiple IP addresses, you must use VPC and setup multiple network interfaces on your instance.

NOTE: sometime between 2012 and 2014 AWS changed so that a single ENI can have multiple EIPs assigned to it. This answer, and the question, are no longer relevant as a result.

Shaun Wilson
  • 103
  • 4
Matt Houser
  • 9,709
  • 1
  • 26
  • 25
  • Am I able to keep the current Elastic IP that I currently have assigned to my EC2 instance when using a new VPC? – Ian Warburton Nov 08 '12 at 17:13
  • 1
    Classic Elastic IP addresses cannot be assigned to a VPC instance. – Matt Houser Nov 09 '12 at 02:42
  • Of note: you might not necessarily need multiple network interfaces; if you still have room left in the existing network interface(s), you can just add new private IP address(es) in those, and then assign new Elastic IP addresses to those new private addresses. Then once the network interfaces are full, then you need to add new interfaces. – Guillaume Boudreau Aug 15 '14 at 18:29
  • it's worth noting that at the time the question was written AWS EC2 only allowed one EIP per ENI, requiring multiple ENIs for multiple EIPs. Presently, one ENI can support multiple EIPs. As such, some advice you find on serverfault on this subject is now obsolete, as is the case for this answer. – Shaun Wilson Apr 14 '15 at 17:22
  • The comment above by @MattHouser is incorrect today. YOU CAN MOVE IP ADDRESS between ec2CLASSIC and VPC, they allow this now – jitbit Dec 06 '17 at 21:10
4

To get 2 elastic ip's associated to one ec2 instance you need to provide a different route for the second ip. This involves to set a new ip rule which specifies what route should be used.

For example, there is an instance with two network interfaces (lets named eth0 and eth1), each one having one internal ip (172.31.4.255 and 172.48.55.23) which translate to his associated elastic ip's.

You need to specify for the ip of eth1(172.48.55.23) to take a different route:

 ip rule add from 172.48.55.23 table default

Then, associate his default route to that rule:

 ip route add default via 172.48.0.1 dev eth1 table default

And flush the cache:

ip route flush cache

You can dig for a "formal" explanation from this article

dvdvck
  • 141
  • 3
  • Thank you! Running Plesk on an EC2 and needed a second IP address - simple yea? ha! I've been at it for hours and these 3 commands finally solved my routing issues. – dtbaker Jun 14 '14 at 11:57
  • What if there is 2 ip address on the one interface? – Petah Aug 08 '17 at 02:19
  • Would making the second NIC (eth1) the default route cause the first NIC (eth0) to become inaccessible (since traffic in through eth0 would be routed out through eth1)? – TSG Sep 18 '21 at 23:30