Multiple IPs and ENIs on EC2 in a VPC within the same Subnet

July 3, 2017

Die Amazon Anleitung sagt nichts darüber wie man mehrere IPs auf einem Ubuntu 14.04/16.04 auf einer EC2 Instanz im gleichen Subnet konfiguriert.

Nach Zuweisung eines zusätzlichen Netzwerkinterfaces auf die EC2 Instanz und dem Mapping der EIPs auf das entsprechende Interface ist die 2te öffentliche IP erstmal nicht erreichbar. Es fehlt die richtige Rückroute für die 2te IP auf dem 2ten Netzwerkinterface.

In der Datei /etc/iproute2/rt_tables wird die Zeile “2 eth1_rt” angehängt und eth1 mit der richtigen internen IP hochfahren. Dies sollte so aussehen:

   ubuntu@ip-172-31-53-91:~$ ifconfig
   eth0      Link encap:Ethernet  HWaddr 0a:14:06:40:f8:1a  
             inet addr:172.31.53.91  Bcast:172.31.53.255  Mask:255.255.255.0
             inet6 addr: fe80::814:6ff:fe40:f81a/64 Scope:Link
             UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
             RX packets:248 errors:0 dropped:0 overruns:0 frame:0
             TX packets:264 errors:0 dropped:0 overruns:0 carrier:0
             collisions:0 txqueuelen:1000 
             RX bytes:26256 (26.2 KB)  TX bytes:31010 (31.0 KB)

    eth1      Link encap:Ethernet  HWaddr 0a:be:ea:4b:0d:32  
             inet addr:172.31.53.20  Bcast:172.31.53.255  Mask:255.255.255.0
             inet6 addr: fe80::8be:eaff:fe4b:d32/64 Scope:Link
             UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
             RX packets:21 errors:0 dropped:0 overruns:0 frame:0
             TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
             collisions:0 txqueuelen:1000 
             RX bytes:1096 (1.0 KB)  TX bytes:1242 (1.2 KB)

Jetzt testweise die Routen setzen, damit bei einem Fehler nur die Instanz gerebootet werden muss (!)

   ip route add default via 172.31.53.1 dev eth1 table eth1_rt
   ip rule add to 172.31.53.20/32 lookup eth1_rt prio 1000
   ip rule add from 172.31.53.20/32 lookup eth1_rt prio 1000

Test von aussen auf einen offenen Port:

   nmap 52.19.xx.xx -p 80

wenn alles gut funktioniert, kann man die Routen fest in der Config verankern:

    cat /etc/network/interface
    # The loopback network interface
    auto lo
    iface lo inet loopback

    auto eth1
    iface eth1 inet static
        address 172.31.53.20 
        netmask 255.255.255.0
        network 172.31.53.0
        broadcast 172.31.53.255 
        up ip route add default via 172.31.53.1 dev eth1 table eth1_rt
        up ip rule add to 172.31.53.20/32 lookup eth1_rt prio 1000
        up ip rule add from 172.31.53.20/32 lookup eth1_rt prio 1000

    # Source interfaces
    # Please check /etc/network/interfaces.d before changing this file
    # as interfaces may have been defined in /etc/network/interfaces.d
    # See LP: #1262951
    source /etc/network/interfaces.d/*.cfg

Am Ende einmal neu booten um zu sehen ob alles ordentlich startet.