How to use different Elastic IP address for outgoing traffic on the same EC2 instance

amazon ec2amazon-elastic-ip

I have an EC2 instance with multiple IPs assigned to it. I use IPs for different sites on the same server, it seems that EC2 instance uses the same IP for outgoing traffic.

For instance when I use curl to investigate my IP address using the proxy it gives me the public address not the one I used as proxy.

curl -x 52.4.95.169:3128 https://api.ipify.org/ # 52.4.95.169 is a an elastic IP assigned to the instance
34.12.45.235 # This is the response, the primary instance IP

This essentially defy the purpose of using elastic IPs for me, for security reasons I can't let anyone know that site-A and site-B on the same server.

Is there some way to use different IP for outgoing requests or I must create new instance for each site?

Best Answer

Whenever an outbound connection is established, unless the connection is explicitly told to open on a specific network interface, it will open on the "default" interface.

curl -x is not the command to use to specify a network interface. The -x command specifies a proxy to tunnel the connection through.

In your curl -x example, your connection is proxying through your own EC2 instance, but all on the "default" interface.

So you want to use the --interface parameter instead of -x. This tells curl to open the outbound connection on that interface rather than the default one.

You can use the network interface name:

  • curl --interface eth0
  • curl --interface eth1

or you can use the network interface's private IP address.

  • curl --interface 10.0.0.1
  • curl --interface 10.0.0.2

You cannot use the public IP address.

Update From Comments:

The original poster was using Squid to proxy requests. This article helped the user configure squid to proxy according to his requirements.

Setup squid to use multiple outgoing IP addresses