- The customer wants to communicate with a single IP in our cloud environment, for any indisputable reason;
- They want to send requests to our single IP, but their systems also require that we connect to their environment for some reason, and they only allow that SAME IP as the connection source;
- To complicate things, we need to route different incoming requests to different resources inside our cloud environment;
Use a custom NAT for incoming and outgoing traffic. The incoming traffic (from internet) is forwarded to an LB. The LB uses a CNAME which may have multiples IPs, but our NAT uses only 1 IP.
This solution is not suitable to High Available environments as our NAT instance is a single point of failure. If the LB is under heavy use, it might scale out and this solution doesn't account for that in two aspects:
- It doesn't distribute requests to the different IPs of the LB;
- It doesn't scale up when needed - requires manual intervention;
If you have a better solution, feel free to contribute!
- Create a private subnet;
- Create a route table for the private subnet;
- Remove the IGW route from the new route table, but keep the VPC route;
- Launch a EC2 using the Amazon Linux AMI in which we'll configure as our NAT gateway. Put it in a public subnet as it needs internet access, and associate a role that has the IAM permissions specified in the
iam-policy-nat-gw.json
policy; - Disable the Source/Dest Check for this instance;
- Copy
10-nat-settings.conf
to/etc/sysctl.d/10-nat-settings.conf
; - Copy
configure-pat.sh
to/usr/local/sbin/configure-pat.sh
; - Copy
configure-dnat.sh
to/usr/local/sbin/configure-dnat.sh
; - Update the instance and install
jq
:
yum update && yum install -y jq
- Add the following lines to
/etc/rc.local
:
/usr/local/sbin/configure-pat.sh
/usr/local/sbin/configure-dnat.sh
- Adjust the permissions:
chmod +x /etc/rc.d/rc.local
chmod +x /usr/local/sbin/configure-pat.sh
chmod +x /usr/local/sbin/configure-dnat.sh
- Add a route to
0.0.0.0/0
pointing to the nat-gw instance's ENI; - Create your internal Load Balancer;
- Change the
LB_NAME
value in/usr/local/sbin/configure-dnat.sh
(line 4) to the name you gave to your Load Balancer; - When you boot the NAT gateway instance, it will automatically retrieve the Load Balancer configuration and configure itself to forward requests to the listener ports.
- Check if everything was configured properly:
grep vpc /var/logs/message
iptables -t nat -L