No history yet

Advanced Subnetting

Smart IP Allocation

When you create a Virtual Private Cloud (VPC), one of the first decisions you make is choosing a primary CIDR block. This choice is more important than it seems. It's like laying the foundation for a new city. If you don't plan for growth and proper zoning, you'll run into major problems down the road.

A common mistake is picking a CIDR block that only meets your immediate needs. Instead, think about the future. Will you need to connect this VPC with other VPCs or your on-premises network later? If so, you must ensure your CIDR blocks don't overlap. Overlapping IP ranges can create complex routing issues that are difficult to fix.

For maximum flexibility, it's often best to choose a CIDR block from the 10.0.0.0/8 range specified in RFC 1918. This gives you a massive pool of over 16 million private IP addresses to work with, making it easier to avoid conflicts and leaving plenty of room for future expansion.

Pro Tip: Always plan your VPC address space as if you'll need to connect it to other networks. Choosing a unique, non-overlapping CIDR block from the start saves a lot of headaches later.

Sizing Your Subnets

Once you have your VPC's primary CIDR block, the next step is to carve it up into smaller networks called subnets. Sizing these subnets correctly is a balancing act. If you make them too large, you'll waste IP addresses, which are a finite resource. If you make them too small, you'll run out of space as your application scales.

When calculating the size of a subnet, remember that AWS reserves five IP addresses within every subnet you create.

AWS reserves the first four IP addresses and the last IP address of every subnet for its own use. This means a /24 subnet, which has 256 total addresses, only provides 251 usable IPs for your resources.

Consider the purpose of each subnet. A subnet for a small cluster of web servers might not need as many addresses as a subnet for a fleet of auto-scaling application servers. A good strategy is to create subnets of different sizes based on their function. For example, you might use /27 subnets for NAT gateways (which only need one IP per availability zone) but /24 subnets for your main application workloads.

Here’s a quick reference for common subnet sizes and their usable IP addresses:

CIDR BlockTotal IPsUsable IPs
/273227
/266459
/25128123
/24256251
/221,0241,019
/204,0964,091

This shows how quickly the number of available addresses grows. Planning your subnet sizes carefully ensures you have enough room to scale without being wasteful.

Public and Private Subnets

The distinction between a public and a private subnet isn't about the subnet itself, but about how it's configured to route traffic. This configuration is the cornerstone of a secure and layered VPC architecture.

A subnet is considered public if its associated route table has a route to an Internet Gateway (IGW). This allows resources within that subnet, like web servers, to have direct access to and from the internet. When you launch an EC2 instance in a public subnet and assign it a public IP address, it can be reached from anywhere online.

Conversely, a subnet is private if its route table does not have a direct route to an Internet Gateway. Resources in a private subnet, such as databases or backend services, cannot be directly accessed from the internet. This isolation is a critical security measure.

VPC (Virtual Private Cloud) Design: Create separate public and private subnets to isolate critical resources from external exposure.

But what if your private resources need to access the internet for software updates or to call external APIs? You can't add a route to the IGW without making the subnet public. The solution is a Network Address Translation (NAT) Gateway.

You place a NAT Gateway in a public subnet and then add a route from your private subnet's route table to the NAT Gateway for all internet-bound traffic (0.0.0.0/0). This way, instances in the private subnet can initiate outbound connections to the internet, but the internet cannot initiate connections back to them, keeping them secure and isolated.

Quiz Questions 1/5

What is the most critical reason to carefully plan your VPC's primary CIDR block and avoid overlaps with other networks?

Quiz Questions 2/5

A subnet is considered 'private' if its route table lacks a direct route to an Internet Gateway.

Mastering these allocation and configuration strategies allows you to build VPCs that are secure, scalable, and efficient.