DEV Community

Jayesh Nalawade
Jayesh Nalawade

Posted on • Originally published at jayeshdevops.hashnode.dev

Subnets and Route Tables in VPC – A Beginner to Intermediate Guide

Introduction 🚀

If you are diving into cloud networking, understanding Virtual Private Cloud (VPC), subnets, and route tables is crucial. These components allow you to manage how resources within your private cloud communicate and connect to the internet.

In this post, we’ll explore subnets and route tables, their role in AWS VPC (or any cloud platform), and walk through examples with some technical depth.


What is a VPC? 🏢

A Virtual Private Cloud (VPC) is a logically isolated network within the cloud, where you can run and secure your resources (e.g., EC2, databases). Think of it as a private data center in the cloud, with full control over IP addresses, routing rules, and internet access.

  • In AWS, each VPC spans a single region and can contain multiple subnets across different availability zones (AZs).

Subnets – Dividing the Network 🏠

What is a Subnet?

A subnet (Sub-Network) is a smaller network within a VPC, used to logically organize resources. Each subnet is tied to an availability zone and is defined by a range of IP addresses (CIDR block).

In AWS, subnets can be either:

  • Public Subnet: Allows direct communication with the internet via an Internet Gateway (IGW).
  • Private Subnet: Resources are not directly accessible from the internet.

Technical Example

Imagine a VPC with a CIDR block of 10.0.0.0/16 (providing 65,536 IPs). You can divide this into:

  • Public Subnet: 10.0.1.0/24 (256 IPs) with internet access through an Internet Gateway.
  • Private Subnet: 10.0.2.0/24 (256 IPs) with no direct internet access.

Each EC2 instance deployed in these subnets will receive an IP address from their respective ranges.


Public vs. Private Subnets Example

Public Subnet (10.0.1.0/24)

  • Hosts web servers that need internet access.
  • Instances receive both a private IP (for internal communication) and a public IP (for external access).
  • Outbound internet traffic is routed via the Internet Gateway (IGW).

Private Subnet (10.0.2.0/24)

  • Hosts databases or backend services that don’t need direct internet access.
  • Instances have only private IPs. For outbound internet traffic (e.g., software updates), they use a NAT Gateway.

Route Tables – Controlling Traffic Flow 🛣️

What is a Route Table?

A route table defines the traffic flow within and outside the VPC. It contains routing rules that specify:

  • Where traffic goes (Destination).
  • How traffic leaves (Target).

Each subnet in a VPC must be associated with a route table. If no custom route table is defined, it uses the main route table by default.


Components of a Route Table:

  • Destination: Defines the CIDR block of the target network (e.g., 0.0.0.0/0 for all IPs).
  • Target: The gateway or network interface where the traffic is routed (e.g., Internet Gateway, NAT Gateway).

Route Table Example

Route Table for Public Subnet

Destination (CIDR) Target Description
0.0.0.0/0 Internet Gateway (IGW) Routes outbound traffic to the internet.
10.0.0.0/16 local Allows internal communication within the VPC.

Explanation:

  • The public subnet route table sends internet-bound traffic through the Internet Gateway (IGW), enabling web servers to be accessed from the internet.
  • Local traffic between subnets within the VPC flows through the 10.0.0.0/16 route.

Route Table for Private Subnet

Destination (CIDR) Target Description
0.0.0.0/0 NAT Gateway Routes outbound traffic through the NAT Gateway.
10.0.0.0/16 local Allows internal communication within the VPC.

Explanation:

  • Outbound traffic from the private subnet (e.g., databases) goes through the NAT Gateway for internet access.
  • Inbound traffic from the internet is blocked.
  • Local traffic between subnets in the VPC flows freely using the 10.0.0.0/16 route.

How Subnets and Route Tables Work Together 🔗

Walkthrough Example:

Let’s say you’re running a web application with two tiers:

  1. Frontend Web Server in the Public Subnet (10.0.1.0/24).
  2. Database Server in the Private Subnet (10.0.2.0/24).

Here’s how the traffic flows:

  1. A user sends a request to your web server in the public subnet.
    • The route table directs traffic to the Internet Gateway (IGW).
  2. The web server queries the database in the private subnet.
    • The local route allows traffic between the two subnets.
  3. The database server needs to download updates from the internet.
    • The private subnet route table directs outbound traffic to the NAT Gateway.

Example Architecture Diagram 🌐

  • VPC CIDR Block: 10.0.0.0/16
  • Public Subnet: 10.0.1.0/24
    • Route Table: Internet Gateway (IGW) for internet access
    • Resource: Web Server (EC2) with a public IP
  • Private Subnet: 10.0.2.0/24
    • Route Table: NAT Gateway for outbound-only internet access
    • Resource: Database (RDS) with a private IP

Key Takeaways 📝

  • VPC is a private network in the cloud where you control networking rules.
  • Subnets divide the VPC into public and private sections with their own IP ranges.
  • Route tables manage how traffic flows between subnets and the internet.
  • Public subnets use an Internet Gateway (IGW) for internet traffic.
  • Private subnets use a NAT Gateway for outbound-only traffic.

Real-World Analogy

Think of the VPC as a gated housing community:

  • Public Subnet: Like a reception area open to visitors (web servers).
  • Private Subnet: Residential areas accessible only to residents (databases).
  • Route Table: A map showing which roads (or gateways) to use for communication.

Conclusion 💡

Understanding subnets and route tables is essential for building secure and scalable cloud architectures. By carefully designing your subnets and managing routes, you can ensure your applications run smoothly and securely in the cloud.

Now it’s your turn:

Try creating a VPC in AWS with both public and private subnets. Experiment with NAT Gateways and Internet Gateways to see how traffic flows!


CloudComputing #AWS #GCP #VPC #Networking #DevOps #Subnets #RouteTables #BeginnersGuide

Top comments (0)