How to correctly setup multiple IP addresses pointing to one instance in EC2?

I have correctly working ec2 instance with 2 private ip addresses, and 2 EIP, connected to them, but I used a subinterface for second IP, not another network interface

Content of /etc/network/interfaces (debian wheezy)

auto lo
iface lo inet loopback
auto eth0 eth0:0
iface eth0 inet dhcp
post-up ifconfig eth0:0 172.31.xx.yy netmask 255.255.240.0 up

i dont remember the reason why I assigned an IP address to eth0:0 manually, but this setup works. Also tests with

curl --interface PRIVATE_IP ifconfig.me

shows, that outgoing traffic from eth0 and eth0:0 is really initiated from correct EIP.


More than a year later, I finally figured it out! Thanks to this article. In summary:

If you haven't already, run dhclient on the new interface:

# dhclient eth1

Then, figure out what your new private IP address is. You can either look in the EC2 console, or run

ip addr

You also need to know the gateway IP. In most cases it's your.ip.0.1, but just to make sure run:

ip route

And you should see something like:

default via 12.34.0.1 dev eth0

12.34.0.1 is the gateway in this case. For demonstration I'm going to assume the private ip is 12.34.56.78. Now run (as root):

ip rule add from 12.34.56.78 table 1000
ip route add default via 12.34.0.1 dev eth1 table 1000
ip route flush cache

To test your configuration:

curl --interface 12.34.56.78 ifconfig.me

Then, to make your changes permanent, add to your /etc/network/interfaces those commands and their oppposites:

auto eth1
iface eth1 inet dhcp
  up ip rule add from 12.34.56.78 table 1000
  up ip route add default via 12.34.0.1 dev eth1 table 1000
  down ip rule del from 12.34.56.78 table 1000
  down ip route del default via 12.34.0.1 dev eth1 table 1000

NOTE: Make sure that you have a snapshot of your instance before you change the persistent network settings, in case something breaks and you are not able to access your instance.