Difference between revisions of "Create Custom NAT Instance in AWS VPC"

From Gejoreuy
Jump to navigation Jump to search
 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Purpose ==
 
== Purpose ==
  
Build a NAT instance using CentOS to propose internet access for private subnet in AWS VPC.  
+
Build a NAT using a Linux CentOS instance to propose internet access for private subnet in AWS VPC.  
<br>In this tutorial, we assume that we already have a VPC with public and prvate subnet like belo picture.
+
<br>This NAT instance will be run and needed by instances in private subnet to make connections to services outside of the subnet or to the public Internet.
 +
<br>An example may include downloading a software package, sending backup data to an external location, or applying system updates to servers on the private subnet.
  
 
== Preparation ==
 
== Preparation ==
 +
 +
Make sure we understand for public and private subnet in AWS VPC.
 +
<br> We already have a new instance in public subnet which will be set as NAT server. Let's call it nat-server which built by Linux CentOS 7.
 +
<br> We already have an instance in private subnet for a test. Let's call it test-server. We can build it by Linux or Windows. Whatever, just for a test to make sure it get internet access..
 +
<br>
 +
<br>In this tutorial, we assume that we already have a VPC with public and private subnet. And then will set its route like below picture.
 +
 +
[[File:20191114_custom_nat_aws.jpg]]
 +
 +
== Disable Source/Dest. Check for the NAT Server ==
 +
 +
In '''AWS EC2 Dashboard''' &rarr; '''Select the Instance''' &rarr; '''Action''' &rarr; '''Networking''' &rarr; '''Change Source/Dest. Check''' :
  
 
<pre>
 
<pre>
[root@nat-server ~]# yum update
+
Yes. Disable.
 
</pre>
 
</pre>
  
== Dissable Source/Dest. Check for the NAT Server ==
+
== Configure System ==  
  
== Configure System ==
+
First, update the system :
 +
 
 +
<pre>
 +
[root@nat-server ~]# yum update
 +
</pre>
  
 
In /etc/sysctl.conf enable ip forwarding :
 
In /etc/sysctl.conf enable ip forwarding :
Line 42: Line 59:
 
== Set Iptables for Masquerade ==
 
== Set Iptables for Masquerade ==
  
Issue iptables command below :
+
Issue iptables command below. Set the subnet based on subnet that we use in our VPC.
  
 
<pre>
 
<pre>
[centos@nat-server ~]$ iptables -t nat -A POSTROUTING -o eth0 -s 172.31.158.0/24 -j MASQUERADE
+
[root@nat-server ~]$ iptables -t nat -A POSTROUTING -o eth0 -s 172.31.2.0/24 -j MASQUERADE
 
</pre>
 
</pre>
  
Line 51: Line 68:
  
 
<pre>
 
<pre>
[centos@nat-server ~]$ vi /etc/rc.local
+
[root@nat-server ~]$ vi /etc/rc.local
 
</pre>
 
</pre>
  
And add this iptables command before "exit 0" :
+
And add this iptables command at the end of the file :
  
 
<pre>
 
<pre>
...
+
iptables -t nat -A POSTROUTING -o eth0 -s 172.31.2.0/24 -j MASQUERADE
 
 
iptables -t nat -A POSTROUTING -o eth0 -s 172.31.158.0/24 -j MASQUERADE
 
 
exit 0
 
exit 0
 
...
 
 
</pre>
 
</pre>
  
Run 'chmod +x /etc/rc.d/rc.local' to ensure the script will be executed during boot :
+
Ensure the script will be executed during boot :
  
 
<pre>
 
<pre>
[centos@nat-server ~]$ chmod +x /etc/rc.d/rc.local
+
[root@nat-server ~]$ chmod +x /etc/rc.d/rc.local
 
</pre>
 
</pre>
  
 
== Modify the NAT Instance Security Group ==
 
== Modify the NAT Instance Security Group ==
 +
 +
We can modify the security for our NAT server instance as our needs. It's up to you.
  
 
== Create Custom Route to Associate the Private Subnet ==
 
== Create Custom Route to Associate the Private Subnet ==
  
In AWS VPC Dashboard &raquo; Route Tables >> Create Route Table
+
In '''AWS VPC Dashboard''' &rarr; '''Route Tables''' &rarr; '''Create Route Table'''
  
PICTURE
+
<pre>
 +
Name tag : [name the route table]
 +
VPC : [choose the VPC]
 +
</pre>
  
Name tag : <name the route table>
+
In '''Route Table''' which early created &rarr; '''Routes''' &rarr; '''Edit Route''' &rarr; '''Add Route''' &rarr; '''Save Routes'''
VPC : <choose the VPC>
 
  
In Route Table which early created >> Routes >> Edit Route >> Add Route
+
<pre>
 +
Destination : 0.0.0.0/0
 +
Target : Instance [choose the nat server instance]
 +
</pre>
  
Destination : 0.0.0.0/0
+
Still in '''Route Table''' &rarr; '''Subnet Association''' &rarr; '''Edit Subnet Association''' &rarr; '''Select the Private Subnet''' &rarr; '''Save'''
Target : Instance <choose the nat instance>
+
 
 +
== Test from Instance Inside Private Subnet ==
 +
 
 +
Just create test-server in private subnet. Don't attach public ip there. And then test to ping Google or any server which located in internet.

Latest revision as of 20:40, 17 November 2022

Purpose

Build a NAT using a Linux CentOS instance to propose internet access for private subnet in AWS VPC.
This NAT instance will be run and needed by instances in private subnet to make connections to services outside of the subnet or to the public Internet.
An example may include downloading a software package, sending backup data to an external location, or applying system updates to servers on the private subnet.

Preparation

Make sure we understand for public and private subnet in AWS VPC.
We already have a new instance in public subnet which will be set as NAT server. Let's call it nat-server which built by Linux CentOS 7.
We already have an instance in private subnet for a test. Let's call it test-server. We can build it by Linux or Windows. Whatever, just for a test to make sure it get internet access..

In this tutorial, we assume that we already have a VPC with public and private subnet. And then will set its route like below picture.

20191114 custom nat aws.jpg

Disable Source/Dest. Check for the NAT Server

In AWS EC2 DashboardSelect the InstanceActionNetworkingChange Source/Dest. Check :

Yes. Disable.

Configure System

First, update the system :

[root@nat-server ~]# yum update

In /etc/sysctl.conf enable ip forwarding :

[root@nat-server ~]# vi /etc/sysctl.conf

Add this :

# For NAT Server
net.ipv4.ip_forward = 1

Reboot now for good measure :

[root@nat-server ~]# reboot

Test our config :

[centos@nat-server ~]$ cat /proc/sys/net/ipv4/ip_forward
1

Set Iptables for Masquerade

Issue iptables command below. Set the subnet based on subnet that we use in our VPC.

[root@nat-server ~]$ iptables -t nat -A POSTROUTING -o eth0 -s 172.31.2.0/24 -j MASQUERADE

Edit the /etc/rc.local file to make masquerade will automatically enable at boot time :

[root@nat-server ~]$ vi /etc/rc.local

And add this iptables command at the end of the file :

iptables -t nat -A POSTROUTING -o eth0 -s 172.31.2.0/24 -j MASQUERADE
exit 0

Ensure the script will be executed during boot :

[root@nat-server ~]$ chmod +x /etc/rc.d/rc.local

Modify the NAT Instance Security Group

We can modify the security for our NAT server instance as our needs. It's up to you.

Create Custom Route to Associate the Private Subnet

In AWS VPC DashboardRoute TablesCreate Route Table

Name tag : [name the route table]
VPC : [choose the VPC]

In Route Table which early created → RoutesEdit RouteAdd RouteSave Routes

Destination : 0.0.0.0/0
Target : Instance [choose the nat server instance]

Still in Route TableSubnet AssociationEdit Subnet AssociationSelect the Private SubnetSave

Test from Instance Inside Private Subnet

Just create test-server in private subnet. Don't attach public ip there. And then test to ping Google or any server which located in internet.