Setup NAT on site to site software VPN in AWS

Site to Site VPN creates a private tunnel with a remote destination for transferring data securely. AWS provides managed VPN solution but if you want to configure NAT on top of it, you need to provision additional instance and make it NAT compatible. AWS provides a documentation on how to configure NAT for managed VPN

However, if the VPN solution is not managed then you can follow this tutorial to setup NAT traversing on your custom VPN CIDR. For this, I will use Openswan, an IPSec VPN for Linux.

We will use NAT to eliminate CIDR overlapping with a remote destination and alter the source IP.



Suppose in remote/customer end, the CIDR block is First, we will connect with that CIDR block from our AWS side.

In AWS, create a new VPC with CIDR

Create an internet gateway (IGW) & attach it with that VPC.

Create two subnets: & One for the general instances and another one dedicated to NAT instance.

Now provision two EC2 instances in those subnets. I used Amazon Linux 2 as AMI and t2.micro as instance type. Also attach elastic IP addresses in both instances. Make sure to disable source and destination check of the instance that you provisioned in subnet because we will use that instance as NAT.

As for the security group of NAT instance, allow UDP port 4500 & 500 and TCP 4500 from remote public IP.

Now create two route tables. For the first route table, associate the subnet

Create the following routes. Note: Here eni-* is the network interface id of the NAT instance and destination is the remote private subnet. So all incoming traffic from subnet will be transferred to the NAT instance for manipulating the packets.

Again create another route table for which will use for NAT instance. Associate the subnets and add the routes:

Topology setup is done.

Now from remote end, gather the following info:

  • Remote public IP (Exm:
  • Remote private subnet (Exm:
  • Pre shared key (Exm: xKDJG/Ay28XdokbtC0gIMzDLdJJo)

Setting up VPN in instance

SSH into the NAT instance. Install Openswan and iptables packages

sudo yum install openswan -y
sudo yum install iptables-services -y
sudo systemctl enable iptables
sudo systemctl start iptables
sudo iptables --flush
sudo service iptables save

Modify the kernel parameters in order to allow IP forwarding and disable redirects. Edit /etc/sysctl.conf file and add below lines:

net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0

net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.eth0.accept_redirects = 0

Save and exit. Reload the rules.

sudo sysctl -p /etc/sysctl.conf

Edit /etc/ipsec.conf file and uncomment the following line if not already

include /etc/ipsec.d/*.conf

Now for configuring tunnel, always consider your side as left/east and remote end as right/west. Create a file in /etc/ipsec.d/aws-vpn.conf and paste the following:

conn Tunnel1

At line 6, leftid=EIP of NAT instance and for line 7, place the public IP of remote gateway.

Also at line 15, place the EIP of the NAT instance and in line 16, place the private subnet of remote end. Save and exit.

Now for authentication, create a file in /etc/ipsec.d/aws-vpn.secrets, put the below line:

remote-public-IP NAT-EIP: PSK "pre-shared-key"
## Example PSK "xKDJG/Ay28XdokbtC0gIMzDLdJJo"

Before starting the tunnel, share your source IP to the remote end and tell them to add that IP as rightsubnet. In our example, we use as source IP. It can be any public IP address. Start the tunnel afterwards.

sudo systemctl enable ipsec.service
sudo systemctl start ipsec
sudo systemctl status ipsec

Tunnel1 is up.

Configuring NAT traversal

I want to send traffic from NAT instance to remote as if it’s coming from public IP Of course it can be any IP of your choice.

In NAT instance, add an iptables rule. Configure the remote private subnet as destination and the source IP of your choice.

sudo iptables -t nat -A POSTROUTING -d -j SNAT --to-source
sudo service iptables save

Now we will test the setup with tcpdump tool. For this, run below command in NAT instance.

sudo yum install tcpdump -y
sudo tcpdump -i any icmp -n

SSH into another instance which you provisioned in subnet Ping to the remote private IP of the host from that instance. Assuming the remote private IP is Ping result should success and you can see the tcpdump result in NAT instance.

[[email protected] ~]# sudo tcpdump -i any icmp -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
13:33:49.373891 IP > ICMP echo request, id 4273, seq 591, length 64
13:33:49.374867 IP > ICMP echo reply, id 4273, seq 591, length 64

As you can see, traffic coming from is being translated to

Also, if tcpdump command run in remote end, the following output will be generated.

[[email protected] ~]# sudo tcpdump icmp -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
13:43:20.255914 IP > ICMP echo request, id 4273, seq 1161, length 64
13:43:21.256418 IP > ICMP echo request, id 4273, seq 1162, length 64
13:43:21.257498 IP > ICMP echo request, id 4273, seq 1162, length 64
13:43:21.259818 IP > ICMP echo request, id 4273, seq 1162, length 64

Traffics are indeed coming from source

Leave a Reply

Your email address will not be published. Required fields are marked *