βœ… VPC Setup with Public & Private Subnets, NAT Gateway, and IGW

VPC Architecture Overview

πŸ”§ VPC & Subnet Creation

  • Create a VPC
    • CIDR block: 11.0.0.0/16
  • Create Subnets
    • Public Subnet: 11.0.1.0/24
    • Private Subnet: 11.0.2.0/24
    • Ensure subnets are in the same Availability Zone as the NAT Gateway (for simplicity)

VPC and Subnet Creation

🌐 Create and Attach Internet Gateway (IGW)

  • Create IGW
    • Attach the IGW to your VPC
    • Required for public resources to access the internet

Internet Gateway

πŸ“œ Create and Associate Route Tables

  • Create 2 Route Tables
    • One for Public Subnet
    • One for Private Subnet

Route Tables

  • Associate Route Tables to Subnets
    • Public Route Table β†’ Public Subnet
    • Private Route Table β†’ Private Subnet

Route Table Association

🌍 Update Public Route Table

  • Add route to public route table:
    • Destination: 0.0.0.0/0
    • Target: Internet Gateway

Public Route Table

βš™οΈ Create and Configure NAT Gateway

  • Create NAT Gateway
    • Must be in Public Subnet
    • Requires Elastic IP
    • NAT Gateway is used by private instances for outbound internet access

NAT Gateway Creation

Important Notes

  • NAT Gateway is not used for inbound traffic
  • It is expensive (hourly + data usage)
  • It allows private subnet to reach internet for updates, downloads, etc.
  • Acts as Proxy for Private EC2 instance

πŸ“¦ Update Private Route Table

  • Add route to private route table:
    • Destination: 0.0.0.0/0
    • Target: NAT Gateway

Private Route Table

πŸ–₯️ Launch EC2 Instances

  • Launch Public EC2 Instance
    • Subnet: Public
    • Auto-assign Public IP: βœ… Yes
  • Launch Private EC2 Instance
    • Subnet: Private
    • Auto-assign Public IP: ❌ No

EC2 Instances

πŸ” Security Groups Configuration

  • Public EC2 SG
    • Inbound: Allow SSH (port 22) from your IP address
  • Private EC2 SG
    • Inbound: Allow SSH from Public EC2’s private IP or CIDR 11.0.1.0/24

Tip

Follow the Principle of Least Privilege β€” allow only what’s needed

Security Groups

πŸ”— SSH into Private EC2

  • SSH into Public EC2 from your local machine:
    • Use the public IP and .pem key

SSH to Public EC2

SSH Connection

  • SSH into Private EC2 from inside Public EC2:

    • Use private IP of the private EC2 and the same .pem key
  • This is known as a Bastion Host (Jump Box) setup

  • Run: curl ipinfo.io

    • It shows the Elastic IP of the NAT Gateway, confirming NAT works

EC2 Instances

NAT Verification

IP Info Output

IMP: EC2 stays private (Uses NAT IP Proof)

NAT IP Proof

🌐 Internet Access Behavior

  • Public EC2
    • Has both private and public IP
    • Uses IGW for internet access
  • Private EC2
    • Has only private IP
    • Uses NAT Gateway in public subnet for outbound-only internet access
    • Cannot be reached from internet directly

πŸ” IGW vs NAT Gateway

FeatureInternet Gateway (IGW)NAT Gateway (NAT GW)
UsedIn Public SubnetsPrivate Subnets
Internet AccessInbound + OutboundOutbound only
IP NeededNoYes (Elastic IP)
CostFreePaid
Accept Incoming Conn.YesNo
Public IP Req.EC2 needs Public IPEC2 stays private (uses NAT IP)

🧠 Must-Know Concepts

  • Elastic IP
    • A static public IP in AWS, used with NAT Gateways or EC2
  • Bastion Host (Jump Server)
    • Public instance used to SSH into private instances
  • Security Group vs NACL
    • SG is stateful (response traffic allowed automatically)
    • NACL is stateless (explicit rules for both inbound and outbound)

Concepts Diagram

⚠️ Best Practices

Warning

  • Avoid giving private EC2 a public IP
  • Restrict SSH access using security groups
  • Monitor NAT Gateway usage to manage costs
  • Use NAT Instance (EC2-based) for cheaper alternative (but less scalable)