VPC Subnetting: Purpose and How to Implement It
Why VPC subnets exist and how to build them: /16 to /28 sizes, 5 reserved IPs, public vs private routing, CIDR planning, AWS CLI and Terraform examples.
Subnetting in a VPC splits the VPC's IP address range into smaller blocks, each tied to one Availability Zone. Subnets let you place resources in separate network zones, control routing per zone through route tables, apply network ACLs, and spread workloads across zones for availability. You implement them by planning CIDR blocks, creating subnets, and attaching route tables.
This guide covers why subnets exist, how AWS sizes and reserves addresses, how to plan a CIDR layout that will not paint you into a corner, and how to build the result with the AWS CLI and Terraform. For the design patterns of public versus private tiers in more depth, see our companion post on designing public and private subnets in an AWS VPC. For the basics of the VPC itself, start with what a VPC is and how it secures AWS resources.
Key takeaways
- A subnet is a range of IP addresses inside a VPC, and each subnet lives in exactly one Availability Zone.
- AWS allows IPv4 subnet sizes from /28 to /16, and reserves five addresses in every subnet: the first four and the last one.
- Subnets in a VPC cannot overlap. Plan the full layout before creating the first one, including room for growth.
- Whether a subnet is "public" or "private" depends on its route table, not on a setting. A public subnet has a route to an internet gateway.
- Use at least two Availability Zones for anything that matters, with a matching subnet per tier in each zone.
- Define subnets in infrastructure as code so CIDR choices are reviewed and repeatable.
What subnetting does inside a VPC
When you create a VPC you give it a CIDR block, such as 10.0.0.0/16, which holds 65,536 addresses. You cannot launch an EC2 instance, a database or a load balancer into the VPC as a whole. Every one of those resources goes into a subnet, and the subnet decides which Availability Zone it runs in and which route table controls its traffic.
That gives subnetting four practical jobs. The first is placement: a subnet pins resources to one Availability Zone, so spreading subnets across zones is how you spread a workload across zones. The second is routing: each subnet is associated with one route table, so you can give internet access to some subnets and keep others internal. The third is filtering: network ACLs attach to subnets and filter traffic at the subnet boundary, in addition to the security groups on each resource. The fourth is organization: separate subnets for web, application and data tiers make it obvious where things live and simplify rules and audits.
The general benefits of subnetting, such as smaller broadcast domains and clearer security zones, come from classic networking. Our post on how subnetting improves network efficiency and security covers that background. In a VPC, AWS does not support broadcast at all, so the main benefits are placement, routing control and segmentation.
How AWS sizes subnets
Allowed sizes
An IPv4 subnet in a VPC can be anywhere from a /28 netmask to a /16 netmask. A /28 holds 16 addresses and a /16 holds 65,536. The subnet's CIDR block can equal the VPC block, which creates a single subnet, or be a subset of it. If you enter a CIDR that is not in canonical form through the CLI or API, AWS normalizes it, so 100.68.0.18/18 becomes 100.68.0.0/18.
For IPv6, if the VPC has an IPv6 block, subnet netmasks range from /44 to /64 in steps of /4. Most designs use /64 subnets.
The five reserved addresses
AWS reserves the first four addresses and the last address in every subnet. In a subnet of 10.0.0.0/24 they are:
10.0.0.0: the network address.10.0.0.1: the VPC router.10.0.0.2: reserved by AWS; the Amazon DNS server sits at the base of the VPC range plus two.10.0.0.3: reserved for future use.10.0.0.255: the broadcast address, reserved even though VPCs do not support broadcast.
So a /24 gives you 251 usable addresses, and a /28 gives you only 11. That difference matters for small subnets. A /28 that hosts a few load balancer nodes, NAT gateways or interface endpoints can run out sooner than you expect, because each of those consumes addresses.
Quick sizing table
| Prefix | Total addresses | Usable in a VPC subnet | Typical use |
|---|---|---|---|
| /28 | 16 | 11 | Very small utility subnets; rarely enough |
| /27 | 32 | 27 | Small public subnets for load balancers and NAT |
| /24 | 256 | 251 | Common default for application tiers |
| /22 | 1,024 | 1,019 | Larger app tiers |
| /20 | 4,096 | 4,091 | Container and Kubernetes pod-heavy subnets |
| /16 | 65,536 | 65,531 | Whole VPC as one subnet; rare |
Public, private and isolated subnets
AWS has no checkbox that makes a subnet public. The label describes routing. A public subnet's route table has a default route, 0.0.0.0/0, pointing to an internet gateway attached to the VPC. Resources in it can reach the internet directly if they have a public IP address. Load balancers that face the internet, bastion hosts and NAT gateways live here.
A private subnet has no route to the internet gateway. If its resources need outbound internet access for updates or external APIs, its default route points to a NAT gateway in a public subnet, which lets connections out but not in. Application servers and containers usually sit here.
An isolated subnet has no default route to the internet at all. Databases and internal-only services often go here, reaching AWS services such as S3 or DynamoDB through gateway endpoints or other services through interface endpoints. Our post on route tables and internet gateways walks through the routing in detail.
Every subnet must be associated with a route table. If you do not associate one explicitly, the subnet uses the VPC's main route table. A common safety practice is to keep the main route table private, with only the local route, so a new subnet never becomes public by accident.
Planning the CIDR layout
Most subnet trouble starts with a rushed CIDR plan. Once a subnet exists you cannot resize it, and VPC peering, Transit Gateway and VPN links all fail or need workarounds when address ranges overlap. Spend the time up front.
Step 1: choose a VPC range that will not collide
Use private ranges from RFC 1918, such as 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16. Keep a central register of which range each VPC, office and data centre uses, so a future peering or hybrid connection does not hit an overlap. Avoid the default 172.31.0.0/16 that default VPCs use for anything you may later connect. Our guide on the consequences of poor IP address management shows how overlaps cause pain later.
Step 2: decide tiers and zones
Count the tiers you need, for example public, application and data, and the number of Availability Zones, usually two or three. Multiply them. Three tiers across three zones means nine subnets.
Step 3: size each tier for growth
Public subnets usually need few addresses. Application subnets need more, and subnets for Amazon EKS need many, because by default the VPC CNI plugin gives each pod an address from the subnet. Leave unused space in the VPC so you can add a tier later without re-addressing everything.
Step 4: write it down
A simple layout for a 10.20.0.0/16 VPC across two zones might look like this:
| Subnet | Zone | CIDR | Usable |
|---|---|---|---|
| public-a | ap-south-1a | 10.20.0.0/24 | 251 |
| public-b | ap-south-1b | 10.20.1.0/24 | 251 |
| app-a | ap-south-1a | 10.20.16.0/20 | 4,091 |
| app-b | ap-south-1b | 10.20.32.0/20 | 4,091 |
| data-a | ap-south-1a | 10.20.48.0/24 | 251 |
| data-b | ap-south-1b | 10.20.49.0/24 | 251 |
The gaps between blocks are deliberate. They leave room for a third zone or a new tier. If you need help with the arithmetic, our post on calculating subnets walks through the method, and a subnet calculator removes guesswork.
Implementing subnets with the AWS CLI
The CLI is useful for learning what each piece does. The commands below create a VPC, two subnets, an internet gateway and a public route table. Replace the IDs with those returned by each step.
# VPC
aws ec2 create-vpc --cidr-block 10.20.0.0/16 \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=demo-vpc}]'
# One public and one private subnet in the same zone
aws ec2 create-subnet --vpc-id vpc-0abc --cidr-block 10.20.0.0/24 \
--availability-zone ap-south-1a
aws ec2 create-subnet --vpc-id vpc-0abc --cidr-block 10.20.16.0/20 \
--availability-zone ap-south-1a
# Internet gateway and a public route table
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway --internet-gateway-id igw-0abc --vpc-id vpc-0abc
aws ec2 create-route-table --vpc-id vpc-0abc
aws ec2 create-route --route-table-id rtb-0pub \
--destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc
aws ec2 associate-route-table --route-table-id rtb-0pub --subnet-id subnet-0pub
The private subnet keeps the main route table, which only has the local route. To give it outbound internet access, create a NAT gateway in the public subnet and add a 0.0.0.0/0 route to it in a separate private route table.
Implementing subnets with Terraform
For anything beyond a lab, define the network in code. Terraform's cidrsubnet function calculates subnet blocks from the VPC block, which avoids typing mistakes. The example below creates one public and one private subnet per zone. It was written against Terraform 1.17.2 and the AWS provider.
variable "azs" {
default = ["ap-south-1a", "ap-south-1b"]
}
resource "aws_vpc" "main" {
cidr_block = "10.20.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = { Name = "demo-vpc" }
}
resource "aws_subnet" "public" {
count = length(var.azs)
vpc_id = aws_vpc.main.id
availability_zone = var.azs[count.index]
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
map_public_ip_on_launch = true
tags = { Name = "public-${var.azs[count.index]}" }
}
resource "aws_subnet" "private" {
count = length(var.azs)
vpc_id = aws_vpc.main.id
availability_zone = var.azs[count.index]
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index + 1)
tags = { Name = "private-${var.azs[count.index]}" }
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}
resource "aws_route_table_association" "public" {
count = length(var.azs)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
Here cidrsubnet("10.20.0.0/16", 8, 0) returns 10.20.0.0/24, and cidrsubnet("10.20.0.0/16", 4, 1) returns 10.20.16.0/20. Run terraform plan to see the calculated blocks before you apply. Many teams use the community VPC module instead of writing every resource, which is fine as long as someone on the team understands what it creates.
Planning for growth and special cases
A few workloads deserve extra thought when you size subnets, because they consume addresses in ways that are easy to miss.
Kubernetes on EKS
With the default Amazon VPC CNI plugin, pods receive IP addresses from the subnet, just like EC2 instances. A cluster of modest size can therefore use thousands of addresses, and a burst of new pods during a deployment can exhaust a /24 quickly. Teams running EKS usually give worker node subnets /20 or larger blocks, or add a secondary CIDR block to the VPC and place pods in dedicated subnets from that range. Plan this before creating the cluster, because changing the subnets of a running node group means replacing nodes.
Load balancers, NAT gateways and endpoints
Application Load Balancers place nodes in each subnet you select and may need additional addresses as they scale. Interface VPC endpoints create a network interface in each subnet you choose. NAT gateways take one address each. Small public subnets fill up faster than people expect, so a /27 or /26 is a safer minimum than a /28 for subnets that host these shared components.
Dual-stack and IPv6
If you add an IPv6 block to the VPC, each subnet can also get a /64 IPv6 range, which removes address pressure entirely for IPv6 traffic. Resources can then have both IPv4 and IPv6 addresses. IPv6 addresses in a VPC are globally unique, so outbound-only access for private resources uses an egress-only internet gateway rather than a NAT gateway. Adding IPv6 later is possible, but deciding early keeps route tables and security rules consistent.
Adding space later
If a VPC runs out of room, you can associate secondary IPv4 CIDR blocks and create new subnets from them. That is a useful escape hatch, not a plan. Secondary ranges must not overlap with anything you peer with, and having subnets scattered across several unrelated ranges makes rules and documentation harder to follow.
Security controls at the subnet level
Subnets give you a place to apply network ACLs, which are stateless allow and deny rules evaluated in number order at the subnet boundary. Security groups, by contrast, are stateful and attach to individual network interfaces. Most teams rely on security groups for fine-grained rules and use network ACLs as a coarse second layer, for example to block a known bad range or to stop the data tier from talking to the internet even if a security group is misconfigured. Our post on securing VPCs with NACLs and security groups compares the two in detail.
Turn on VPC Flow Logs at the VPC or subnet level so you can see accepted and rejected traffic when troubleshooting or auditing. Keep databases in subnets with no internet route, and reach AWS services through endpoints instead of NAT where possible.
Common mistakes to avoid
A few mistakes show up repeatedly in reviews. Subnets that are too small, especially for EKS, run out of addresses during a scale-out. Overlapping ranges between VPCs block peering later. Single-zone designs put every subnet in one Availability Zone, so one zone outage takes everything down. Associating the main route table with an internet route makes every new subnet public by default. Forgetting that a NAT gateway lives in one zone means private subnets in other zones lose outbound access if that zone fails, so production designs usually run one NAT gateway per zone.
Subnet planning is a core skill for the AWS Solutions Architect Associate exam and for real DevOps work on AWS. Our AWS Solutions Architect Associate training builds multi-zone VPCs in labs, in the Kharadi, Pune classroom or live online.
Official references
- Amazon VPC User Guide: Subnet CIDR blocks
- Amazon VPC User Guide: Subnets for your VPC
- Amazon VPC User Guide: Route tables
- Terraform documentation: cidrsubnet function
FAQs
Why do we need subnets in a VPC?
Every resource must launch into a subnet. Subnets pin resources to an Availability Zone, attach them to a route table that controls internet access, and let you apply network ACLs, so they are how you build separate tiers and spread workloads across zones.
How many IP addresses does AWS reserve in each subnet?
Five: the first four addresses (network address, VPC router, DNS, and one reserved for future use) and the last address. A /24 subnet therefore has 251 usable addresses.
What is the smallest and largest subnet allowed in a VPC?
For IPv4, the smallest is a /28 with 16 addresses (11 usable) and the largest is a /16 with 65,536 addresses. IPv6 subnets range from /44 to /64.
What makes a subnet public in AWS?
Its route table. A subnet is public when its route table has a route to an internet gateway. Resources still need a public IP address to be reachable from the internet.
Can a subnet span multiple Availability Zones?
No. Each subnet lives in exactly one Availability Zone. For high availability, create a subnet for each tier in at least two zones.
Can I resize a subnet after creating it?
No. You would need to create a new subnet with the new range and move resources into it. You can add secondary CIDR blocks to the VPC to create more subnets, which is why leaving space during planning helps.
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0