I need a specific architecture in AWS:
One virtual machine (AWS EC2) must:
- Accept new connections entering via the public IP (AWS Elastic IP) directly attached to the network interface (AWS ENI) in the virtual machine.
- Initiate new outgoing connections using another interface in another subnetwork wich is behind a NAT (AWS NAT Gateway)
I could easily configure the two interfaces in Ubuntu 22.04, and if I create a specific route, for example, using the NAT Gateway for routing traffic to the IP 8.8.8.8
, it works very well, and the connection successfully uses the NAT Gateway, while I still connected via SSH using the public IP in the other interface.
ip route add 8.8.8.8 via <private ip of the NAT gateway>
To reach the requirements, I need to route all new outgoing connections to the NAT Gateway, but when I do it, I lose access to the public IP, and can’t reconnect anymore.
ip route add default via <private ip of the NAT gateway>
How can I achieve this? I thought that the default behavior of Ubuntu was to return the traffic of incoming connections to the same interface and Gateway they arrived, but that doesn’t seem to be working.
I’ve already tried manually specifying the default route to use the NAT Gateway, but that makes any connection using the public IP not work.