β Day 14 of My Cloud Journey βοΈ β EC2 in Private Subnet + NAT Gateway π
Today was all about designing secure and scalable network architecture in AWS by deploying an EC2 instance in a private subnet and enabling it to access the internet using a NAT Gateway. This is a classic real-world setup often used in production environments to protect backend servers while still allowing them to fetch updates or communicate outward.
π§± What Weβre Building Today
We're creating a VPC architecture with:
πΉ Public Subnet: For internet-facing resources (e.g., NAT Gateway)
πΉ Private Subnet: For internal EC2 instances (no direct internet access)
πΉ NAT Gateway: So EC2 in private subnet can reach internet without exposing its IP
πΉ Internet Gateway (IGW): Attached to the VPC for public subnet internet access
πΉ Route Tables: To control traffic flow in both public and private subnets
π Architecture Diagram
βββββββββββββββββββββ VPC βββββββββββββββββββββββ
β β
β βββββββββββββ Public Subnet ββββββββββ β
β β β β
β β EC2 (Optional Bastion) β β
β β NAT Gateway β β
β ββββββββββββββββββββββββββββββββββββββ β
β β
β βββββββββββββ Private Subnet βββββββββ β
β β β β
β β EC2 Instance (No Public IP) β β
β ββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββ
π Why Use NAT Gateway?
Security: EC2 in private subnet is not exposed to the internet
Outbound-only: Instance can reach the internet but canβt be reached from it
Great for: App servers, backend microservices, databases (for updates, APIs, etc.)
π§ Step-by-Step Setup
Step 1: Create a VPC
- CIDR:
10.0.0.0/16
Step 2: Create Two Subnets
Public Subnet:
10.0.1.0/24(Enable auto-assign public IP)Private Subnet:
10.0.2.0/24
Step 3: Create and Attach Internet Gateway
IGW allows the public subnet to connect to the internet
Attach IGW to the VPC
Step 4: Create Route Tables
Public Route Table:
Target:
0.0.0.0/0β IGWAssociate with public subnet
Private Route Table:
Target:
0.0.0.0/0β NAT Gateway (next step)Associate with private subnet
Step 5: Create NAT Gateway in Public Subnet
Allocate a new Elastic IP
Place NAT Gateway in the public subnet
NAT Gateway enables instances in private subnet to access the internet
Step 6: Launch EC2 Instances
Public EC2 (Optional):
- To act as a bastion or simply to test NAT functionality
Private EC2:
In private subnet
No public IP
Step 7: Test the Setup
SSH into public EC2 (if used), then connect to private EC2
From private EC2:
Run
pinggoogle.com(should work if ICMP is open)Run
curlhttps://amazon.comoryum updateto verify outbound internet
π‘οΈ Security Considerations
Use Security Groups:
Public EC2: Allow SSH from your IP
Private EC2: Allow only traffic from public EC2 (or internal apps)
Limit NAT traffic with NACLs if needed
Monitor NAT Gateway usage in VPC Flow Logs
π§ Key Learnings
NAT Gateway acts as an outbound proxy for private instances
Internet Gateway works only when an instance has a public IP
Route tables play a crucial role in controlling subnet connectivity
Best practice: Use Private EC2s for app servers, Public only for ALBs or bastion
π Common Use Cases
| Use Case | NAT Gateway Setup? |
| Backend services calling external APIs | β Yes |
| Private EC2 downloading OS updates | β Yes |
| Hosting a public website | β Needs IGW |
| Database servers with no internet | β No NAT needed |
π Whatβs Next?
Day 15: Iβll dive into launching a web server (Apache/NGINX) on EC2
#Day14 #AWS #VPC #NATGateway #EC2 #CloudSecurity #90DaysOfCloud #shubhamlondhe




