How to create AWS NAT and Bastion Hosts in VPC

Jephe Wu -

Objective:  Use VPC wizard to create public and private subnets in different AZ and use NAT service for private subnets and access servers on private subnets through bastion host.


1. Use VPC wizard to create required subnets.

We will create the following VPC network and subnets: with subnets: public subnet A in ap-southeast-2a with Elastic IP 1 hosting company web server and NAT box private subnet C in ap-southeast-2a  hosting SQL server, access Internet through NAT box in public subnet A public subnet B in ap-southeast-2b with Elastic IP 2 hosting a bostion server which is Windows 2008 R2 base OS, jumphost for accessing SQL server in subnet C private subnet D in ap-southeast-2b hosting other servers or applications, accessing Internet through NAT box in public subnet A

Above wizard will only create subnet A and C and NAT box. 
After that, follow normal procedure to create Internet facing web server instance with elastic IP and automatic assigned or manually specified private IP e.g.

Eventually, we need to create 2 more subnet in another AZ manually to make it like this:

Network Diagram:
----Internet --- public subnet A (with  --- private subnet C ( with  [ ap-southeast-2a ]
----Internet ----pubic subnet B  (with ---- private subnet D  (with [ ap-southeast-2b ]

2. Configure NAT box
After finishing wizard, the NAT is up and running, you still need to configure the following to make it as masquerading NAT box for your entire VPC network, by default, it only allow the outbound Internet traffic.

  • check security group configuration of NAT box instance, by default, it doesn't allow the entire VPC for incoming traffic which will block Internet access for all private subnets.
Also, check other settings such as disable src/destination check, the routing table for private subnet hosts routing table (must be pointing to NAT box ENI).

You can also ssh into NAT box from Internet to check iptables rules and ip forwarding:

[root@ip-10-0-0-104 ec2-user]# iptables -L -v -n -t nat | grep 10.0
  531 31715 MASQUERADE  all  --  *      eth0 

[root@ip-10-0-0-104 ec2-user]# more /proc/sys/net/ipv4/ip_forward

[root@ip-10-0-0-104 ec2-user]# grep nameserver /etc/resolv.conf 

2.1 use NAT instance as port forwarding for RDS instance (e.g. to RDS Postgres)
Firstly make sure from Nat box e.g, you can telnet to e.g. port 5432, then run commands below on NAT box
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 5432 -j DNAT --to
iptables -A FORWARD -p tcp -d --dport 5432 -j ACCEPT (might be optional)
iptables -A POSTROUTING -t nat -p tcp -s -d --dport 5432 -j SNAT --to
Note: is the RDS internal ip and is NAT internal ip 
Now add the external application ip into the NAT box security group to allow connection.

3. Configure Bostion Windows Server in public subnet C

Create another 2 more subnets C and D in another AZ ap-southeast-2b,  replace public subnet C routing table from ENI of NAT box to igw so that we can access it from Internet as jumphost.

4. Create SQL server database host in private subnet B
configure security group setting to only allow the internal IP of Bostion Windows server or the whole private subnet

Notes for security group:
You can also specify another security group groupid in the customer IP part to quote the same security group settings. 

For either Linux or Windows server, if you remove your current connection source IP from security group while you are on the servers, the existing connection will be still okay, but if you exit ssh/rdp, the new connection won't be able to make. 

No comments:

Post a Comment