This content originally appeared on DEV Community and was authored by Clinton Ogechi
A NAT Gateway is a key component in AWS networking, that allows instances in a private subnet to connect to the internet or other AWS services while preventing inbound traffic from the internet. Below are steps on how to implement it;
1. Create a VPC
- Log in to the AWS Management Console.
- Navigate to the VPC Dashboard.
- Click on Your VPCs in the left-hand menu.
- Click on Create VPC.
- Fill in the following details: Name tag:
MyVpc
IPv4 CIDR block:10.0.0.0/16
- Click on Create VPC.
2. Create Subnets
Create Public Subnet
- In the VPC Dashboard, click on Subnets.
- Click on Create subnet.
- Enter the following details: Name tag:
PublicSubnet
, VPC: SelectMyVpc
, Availability Zone: Choose one (e.g.,af-south-1a
), IPv4 CIDR block:10.0.1.0/24
. - Click on Create subnet.
Create Private Subnet
- In the VPC Dashboard, click on Subnets.
- Click on Create subnet again.
- Fill in the following details: Name tag:
PrivateSubnet
VPC: SelectMyVpc
Availability Zone: Choose one (e.g.,af-south-1b
) IPv4 CIDR block:10.0.2.0/24
- Click on Create subnet.
3. Create and Attach an Internet Gateway
- Go to the Internet Gateways section in the VPC Dashboard.
- Click on Create internet gateway.
- Fill in the following details:
Name tag:
MyInternetGateway
- Click Create internet gateway.
- Select the newly created Internet Gateway and click Actions > Attach to VPC.
- Select MyVpc and click Attach internet gateway.
4. Create a NAT Gateway
- Go to the NAT Gateways section in the VPC Dashboard.
- Click on Create NAT gateway.
- Fill in the following details:
Name tag:
MyNatGateway
Subnet: SelectPublicSubnet
Elastic IP allocation ID: Click on Allocate Elastic IP and then Allocate. - Click Create a NAT Gateway.
5. Create Route Tables
- Go to the Route Tables section in the VPC Dashboard.
- Click on Create route table.
- Fill in the details for the public route table:
Name tag:
PublicRouteTable
VPC: SelectMyVpc
- Click Create route table.
- Select the newly created route table
PublicRouteTable
and click on the Routes tab. - Click Edit routes > Add route.
- Set the Destination to
0.0.0.0/0
and Target to the Internet Gateway (MyInternetGateway
). - Click Save routes.
- Go back to Route Tables and create another route table for the private subnet:
Name tag:
PrivateRouteTable
VPC: SelectMyVpc
- Click Create route table.
- Select the newly created route table
PrivateRouteTable
and click on the Routes tab. - Click Edit routes > Add route.
- Set the Destination to
0.0.0.0/0
and Target to the NAT Gateway (MyInternetGateway
). - Click Save routes.
6. Associate Route Tables with Subnets
- Select the PublicRouteTable route table.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the PrivateRouteTable and click Save.
- Select the PrivateRouteTable route table.
- Click on the Subnet associations tab.
- Click Edit subnet associations.
- Select the PrivateRouteTable and click Save.
7. Launch Instances
- Go to the EC2 Dashboard in the AWS Management Console.
- Click on Launch Instance.
- Fill in the following details: Name: public-instance AMI: Select an AMI, e.g., Amazon Linux 2 Instance type: t2.micro (or any other type you prefer) Key pair: Create a new key pair or select an existing one
- Network settings:
VPC: Select
MyVpc
Subnet: SelectPublicSubnet
Auto-assign Public IP: Enable - Click Launch instance.
- Repeat the steps to launch another instance in the private subnet:
Name: private-instance
AMI: Select an AMI, e.g., Amazon Linux 2
Instance type: t2.micro (or any other type you prefer)
Key pair: Create a new key pair or select an existing one
Network settings:
VPC: Select
MyVpc
Subnet: SelectPrivateSubnet
Auto-assign Public IP: Disable
Verify the Configuration
Public Instance: Should have internet access directly.
Private Instance: Should have internet access through the NAT Gateway.**
By following these steps, you will have a VPC with one public subnet and one private subnet, each correctly configured with route tables and a NAT Gateway.
This content originally appeared on DEV Community and was authored by Clinton Ogechi