Every AWS workload you will ever run lives inside a virtual private cloud, whether you built it yourself or inherited the default one. The VPC is the network boundary for your EC2 instances, your databases, your load balancers and your Kubernetes clusters. If you do not understand it, every networking problem becomes a mystery: instances that cannot reach the internet, a load balancer that never becomes healthy, a database that refuses connections from the application sitting next to it.
This guide explains the VPC from the ground up, the way you will actually use it. You will learn how CIDR blocks carve up address space, what makes a subnet public or private, how route tables and gateways move traffic, and how security groups and network ACLs filter it. Each section ends with something you can run or check, and the final section is a troubleshooting checklist for the failures that appear in real environments and in interviews.
What Is an AWS VPC?
An Amazon Virtual Private Cloud is a logically isolated virtual network that you define inside AWS. It closely resembles a traditional network you would operate in your own data centre, with the benefits of AWS’s scalable infrastructure: you choose the IP address range, create subnets, configure route tables and gateways, and attach security controls. Resources you launch — EC2 instances, RDS databases, load balancers — run inside the subnets of your VPC, and traffic between them stays on AWS’s private network unless you deliberately route it elsewhere.
A VPC spans a whole Region, while each subnet inside it lives in a single Availability Zone. That split matters for high availability: you place subnets in at least two Availability Zones so that the loss of one zone does not take your whole application down, and you put a load balancer across them so traffic keeps flowing. A VPC is also isolated from every other VPC by default. Two VPCs cannot talk to each other, even in the same account, until you connect them explicitly with peering, a transit gateway or a PrivateLink endpoint.
Every AWS account comes with a default VPC in each Region, using the CIDR block 172.31.0.0/16, which provides 65,536 IP addresses. The default VPC is ready to use immediately: its subnets are public, it has an internet gateway attached, and instances launched into it get public IP addresses. It is convenient for a first experiment and a poor home for production, because it was not designed around your application — your subnets, your routing and your security rules should be deliberate choices, not defaults you inherited. AWS itself charges nothing extra for the VPC itself; the billable parts are components such as NAT gateways, traffic mirroring and VPC endpoints, which is why a careless NAT gateway layout is one of the easiest ways to inflate an AWS bill. The official overview, How Amazon VPC works, covers the same concepts — VPCs and subnets, default and non-default VPCs, route tables and the ways a VPC reaches the internet — and it is the page to keep open while you build your first VPC.
If networking fundamentals feel shaky, read our networking basics for DevOps engineers first — it covers IP addressing, CIDR notation and routing in plain language, and everything in this guide builds on it. If you are brand new to AWS itself, our AWS tutorial for beginners walks through accounts, the console and your first EC2 instance before you touch a VPC.
CIDR Blocks and IP Addressing
The first decision in any VPC is the CIDR block: the range of private IP addresses the VPC owns. AWS lets you choose a VPC CIDR between a /16 and a /28 netmask, using the private ranges defined for internal networks — 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. A /16 gives you 65,536 addresses; a /20 gives 4,096; a /24 gives 256. The block you choose sets a hard ceiling on everything inside the VPC, so choose with growth in mind rather than convenience.
The most common beginner mistake is picking a CIDR that is too small, or one that overlaps with another network you later need to connect to. A VPC peered with another VPC, or connected to an office network over a VPN, cannot have overlapping CIDR ranges — traffic cannot be routed when two networks claim the same addresses. Plan before you build: give each environment its own non-overlapping slice of the 10.0.0.0/8 space, for example 10.0.0.0/16 for production, 10.1.0.0/16 for staging and 10.2.0.0/16 for development. That scheme leaves room for dozens of environments and makes the address itself tell you which environment a resource belongs to.
Subnet CIDR blocks are carved out of the VPC block, and every subnet block must sit entirely inside it. A common layout for a 10.0.0.0/16 VPC is four /20 subnets — two public and two private, one pair per Availability Zone — which leaves most of the address space unused for future subnets. Size subnets generously: a /24 subnet holds 256 addresses, but AWS reserves five in every subnet (the network address, the VPC router, the DNS server, a reserved address and the broadcast address), leaving 251 usable. That sounds like plenty until an auto scaling group, an EKS cluster and a database subnet share one small block and you run out of addresses during a scale-out at the worst possible moment.
You can also add IPv6 to a VPC. AWS assigns a /56 IPv6 block from its own pool, and subnets take /64 slices of it. IPv6 addresses are globally unique rather than private, so the security model is different — an egress-only internet gateway replaces the NAT gateway for outbound-only IPv6 traffic — but for most teams IPv4 remains the working default, and you should only enable IPv6 when you have a concrete reason, such as an application that needs it or an address-exhaustion problem in a large VPC.
Here is a worked layout for a 10.0.0.0/16 VPC across two Availability Zones. It uses /20 subnets, which keeps the arithmetic simple and leaves most of the VPC’s address space free for future growth:
| Subnet | Availability Zone | CIDR block | Addresses | Route table |
|---|---|---|---|---|
| public-1a | eu-west-1a | 10.0.0.0/20 | 4,096 | Public — 0.0.0.0/0 to internet gateway |
| public-1b | eu-west-1b | 10.0.16.0/20 | 4,096 | Public — 0.0.0.0/0 to internet gateway |
| private-1a | eu-west-1a | 10.0.32.0/20 | 4,096 | Private — 0.0.0.0/0 to NAT gateway in 1a |
| private-1b | eu-west-1b | 10.0.48.0/20 | 4,096 | Private — 0.0.0.0/0 to NAT gateway in 1b |
| data-1a | eu-west-1a | 10.0.64.0/20 | 4,096 | Isolated — local route only |
| data-1b | eu-west-1b | 10.0.80.0/20 | 4,096 | Isolated — local route only |
Notice the data subnets have no route to the internet at all — not even through a NAT gateway. Databases belong in isolated subnets like these, because a database that cannot reach the internet cannot leak data to it, and nothing outside the VPC can reach it except through the application tier. Each private subnet points at the NAT gateway in its own Availability Zone, so a zone failure takes out one zone’s egress but not the other’s. Six /20 subnets consume only 24,576 of the VPC’s 65,536 addresses, leaving room for more subnets, more zones and more environments without re-addressing anything.
Subnets: Public vs Private
A subnet is a range of IP addresses inside your VPC, tied to one Availability Zone. Nothing about a subnet is inherently public or private — the distinction comes entirely from its route table. A subnet is public when its route table has a route to an internet gateway; it is private when it does not. That single fact explains most VPC confusion: you do not make a subnet public by giving instances public IPs, and you do not make it private by withholding them. The route table decides.
The standard production layout uses both kinds. Public subnets hold the things that must face the internet: load balancers, NAT gateways and bastion hosts. Private subnets hold everything else: application servers, databases, caches and worker nodes. Traffic reaches the private subnets through the load balancer, and the private resources reach the internet — for patches and package downloads — through the NAT gateway. Nothing in a private subnet is directly reachable from the internet, which removes a whole class of exposure before any firewall rule is written.
Create the VPC and its subnets with the CLI to see the shape of it. This example builds a 10.0.0.0/16 VPC with one public and one private subnet in a single Availability Zone; in production you would repeat the subnet pair in a second zone.
aws ec2 create-vpc --cidr-block 10.0.0.0/16 \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=demo-vpc}]'
aws ec2 create-subnet --vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.0.0/24 --availability-zone eu-west-1a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-public-1a}]'
aws ec2 create-subnet --vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.1.0/24 --availability-zone eu-west-1a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-private-1a}]'
Enable DNS hostnames on the VPC if you want instances to get public DNS names, and enable DNS support so that AWS’s DNS resolver answers queries for AWS service endpoints from inside the VPC. Both are on by default in the default VPC, but on a VPC you create yourself you must set them explicitly — a missing DNS hostname setting is a classic reason a new instance is unreachable by name even though its IP works fine.
Route Tables and Internet Gateways
A route table is a set of rules that decides where traffic leaving a subnet goes. Every VPC has a main route table, and you can create custom ones and associate them with individual subnets. Each route has a destination CIDR and a target: the local route, which covers traffic inside the VPC and cannot be changed or deleted, and routes you add yourself pointing at gateways, NAT devices, VPC endpoints or peered VPCs. The most specific matching route wins, so a route to 10.0.1.0/24 beats the default route to 0.0.0.0/0.
The internet gateway is the component that connects a VPC to the internet. It is a horizontally scaled, redundant gateway managed by AWS — you do not run it, patch it or size it. One internet gateway attaches to one VPC, and a public subnet’s route table sends internet-bound traffic (0.0.0.0/0) to it. An instance in a public subnet also needs a public IP address or an Elastic IP, because the internet gateway translates between the instance’s private address and its public one. Without a public IP, the instance can still receive traffic through a load balancer, but it cannot initiate outbound internet connections itself.
aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=demo-igw}]'
aws ec2 attach-internet-gateway --vpc-id vpc-0123456789abcdef0 \
--internet-gateway-id igw-0123456789abcdef0
aws ec2 create-route-table --vpc-id vpc-0123456789abcdef0 \
--tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=demo-public-rt}]'
aws ec2 create-route --route-table-id rtb-0123456789abcdef0 \
--destination-cidr-block 0.0.0.0/0 --gateway-id igw-0123456789abcdef0
aws ec2 associate-route-table --route-table-id rtb-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0
Two routing mistakes cause most outages. The first is associating the wrong route table with a subnet, which silently makes a public subnet private or vice versa — always check the subnet’s explicit association in the console rather than assuming. The second is a missing return route: traffic can flow out through a NAT gateway in one Availability Zone but the response has no route back, which looks exactly like a security group problem until you compare the route tables. When traffic is not flowing, read the route table first and the firewall second.
NAT Gateways and Egress
Resources in a private subnet still need to reach the internet for software updates, package registries and external APIs. The NAT gateway provides that outbound-only path: instances in private subnets can initiate connections to the internet, but nothing on the internet can initiate a connection to them. AWS manages the NAT gateway as a service, and you place it in a public subnet with an Elastic IP address, then point the private subnets’ route tables at it for 0.0.0.0/0.
NAT gateways come in two connectivity types. A public NAT gateway sits in a public subnet, has an Elastic IP, and routes traffic to the internet gateway — this is the type almost every tutorial means. A private NAT gateway has no Elastic IP and cannot reach the internet at all; it routes traffic to other VPCs or an on-premises network through a transit gateway or virtual private gateway. If you attach a private NAT gateway and route its traffic to an internet gateway, the internet gateway drops it. Choose the type by asking where the traffic needs to go, not by guessing. AWS documents both types, their considerations and their limitations on the NAT gateways page, including the rule that connections must always be initiated from inside the VPC — a NAT gateway never accepts an unsolicited inbound connection.
The cost of a NAT gateway is the part beginners underestimate. The gateway itself is billed hourly, and data processed through it is billed per gigabyte on top of ordinary data-transfer charges. A single NAT gateway shared across Availability Zones works, but it creates a cross-zone dependency and cross-zone traffic charges, so the recommended layout is one NAT gateway per Availability Zone, each serving the private subnets in its own zone. For a cost-sensitive development environment, a NAT instance — a small EC2 instance running NAT software — is cheaper and fully under your control, but you then own its availability, patching and bandwidth limits, which is why production systems use the managed gateway.
There is also an alternative that avoids NAT entirely for AWS services: VPC endpoints. A gateway endpoint for S3 or DynamoDB adds a route to your route table that sends traffic to the service over the AWS private network, with no gateway and no data-transfer charge for the endpoint itself. Interface endpoints use PrivateLink and place an elastic network interface inside your subnet, which does carry an hourly charge. If your private workloads only need S3 and DynamoDB, gateway endpoints remove the NAT gateway from that path completely.
Security Groups vs Network ACLs
Two firewalls protect a VPC, and they work at different layers. A security group is attached to a resource — an EC2 instance, a load balancer, a database — and filters traffic at the instance level. A network ACL is attached to a subnet and filters traffic at the subnet boundary. They are complementary, not interchangeable, and understanding how they differ is one of the most-tested topics in AWS networking interviews.
| Feature | Security group | Network ACL |
|---|---|---|
| Attached to | Resource (instance, load balancer, database) | Subnet |
| Stateful or stateless | Stateful — return traffic is automatically allowed | Stateless — return traffic needs an explicit rule |
| Rules | Allow rules only | Allow and deny rules, evaluated in number order |
| Default behaviour | All inbound denied, all outbound allowed | All traffic allowed (default NACL) |
| Typical use | Day-to-day access control per workload | Subnet-level guardrail, such as blocking a port range |
Stateful versus stateless is the distinction that causes real debugging pain. With a security group, allowing inbound traffic on port 443 automatically allows the response back out, because the group remembers the connection. With a network ACL, allowing inbound port 443 is not enough: you must also allow the outbound ephemeral ports (1024–65535) that the response uses, or the reply is dropped and the connection appears to hang. If a connection works from one subnet but not another, and the security groups are identical, the network ACL is the first place to look.
Write security group rules by reference, not by address, wherever possible. Instead of opening port 5432 to a CIDR block, allow it from the security group of the application servers — AWS resolves the reference dynamically, so instances can be replaced without rewriting rules. Keep the default deny posture: a new security group denies all inbound traffic, and you add only the rules the workload needs. Open 0.0.0.0/0 to a port only when the service genuinely faces the internet, and never to a database or an administration port. A public SSH port is the fastest way to collect automated login attempts, and it is the first thing an auditor will flag.
VPC Endpoints, Peering, Flow Logs and Troubleshooting
Connecting VPCs is a separate skill from building one. VPC peering connects two VPCs so they can route traffic privately using private IP addresses, but peering is not transitive: if VPC A is peered with B, and B is peered with C, A cannot reach C through B. Peering also cannot connect VPCs with overlapping CIDR blocks, which is the second reason to plan address space before you build. For more than a handful of VPCs, a transit gateway is the hub that replaces a mesh of peering connections with one central router, and AWS PrivateLink exposes a service in one VPC to another without any peering at all.
VPC Flow Logs record the IP traffic flowing to and from network interfaces in your VPC. They capture accepted and rejected traffic, and they are the fastest way to answer “is my traffic being blocked, and by what?” Enable flow logs on the VPC or the specific subnet you are debugging, send them to CloudWatch Logs or S3, and look for REJECT entries for the address and port you care about. A REJECT in the flow log with no matching security group or NACL rule usually means the route table is sending the traffic somewhere it should not go.
Run through this checklist when something in a VPC does not connect. First, is the instance running and in the right subnet? Second, does the subnet’s route table send the traffic to the right target — internet gateway for public, NAT gateway for private egress? Third, does the security group allow the traffic, and does the network ACL allow both directions? Fourth, does the instance have a public IP if it needs one, and is DNS resolution enabled on the VPC? Fifth, what do the flow logs say — ACCEPT, REJECT, or nothing at all, which points at routing rather than filtering? Work the list in order and you will find the fault instead of changing rules at random.
A concrete example ties it together. An application server in a private subnet cannot download packages, and the error is a connection timeout rather than a refusal. A timeout with no response usually means routing, not filtering, so you check the route table first: the private subnet’s route table has no 0.0.0.0/0 route at all, because the subnet was created but never associated with the private route table and fell back to the VPC’s main route table, which only has the local route. You associate the subnet with the private route table that points at the NAT gateway, and the downloads start working. If the flow log had shown REJECT instead, the same symptom would have pointed at the security group or the network ACL — and if the flow log showed nothing at all, the traffic was never leaving the instance, which would point at the instance’s own configuration. The log entry tells you which layer to fix, and that is why flow logs are worth enabling before you need them rather than after.
Kubernetes makes all of this concrete, because an EKS cluster is a VPC construct: the control plane and the worker nodes live in your subnets, and the cluster’s networking choices — public versus private endpoint, node subnets, security groups — are VPC choices. Our AWS EKS tutorial builds a cluster inside a VPC and shows how the networking decisions from this guide map onto a real workload.
Frequently Asked Questions
What is the difference between a public and a private subnet?
The route table decides. A public subnet has a route to an internet gateway; a private subnet does not. Instances in a public subnet usually have public IPs, but the route is what makes the subnet public, not the address.
How many IP addresses does a /24 subnet have?
A /24 holds 256 addresses, of which AWS reserves five — the network address, the VPC router, the DNS server, a reserved address and the broadcast address — leaving 251 usable. Size subnets with that reservation in mind, and remember that load balancers, NAT gateways and EKS nodes all consume addresses too.
Do I need a NAT gateway in every Availability Zone?
For production, yes. One NAT gateway per Availability Zone keeps each zone independent, so a zone failure does not cut off internet access for the others, and it avoids cross-zone traffic charges. A single shared gateway is acceptable for a development environment where cost matters more than availability.
What is the difference between a security group and a network ACL?
A security group is stateful, attached to a resource, and supports allow rules only. A network ACL is stateless, attached to a subnet, supports allow and deny rules evaluated in order, and requires explicit rules for return traffic. Use security groups for everyday access control and NACLs as a subnet-level guardrail.
Can two VPCs with the same CIDR block be peered?
No. VPC peering requires non-overlapping CIDR blocks, because routing cannot distinguish two networks that claim the same addresses. This is why address planning comes before building: overlapping ranges are painful to fix after resources exist.
Conclusion
A VPC is four ideas working together: an address range you choose, subnets that divide it by Availability Zone, route tables that decide where traffic goes, and firewalls that decide what gets through. Build one yourself with the CLI, put a load balancer in the public subnets and an application in the private ones, break it deliberately by removing a route, and fix it with the flow logs. That single exercise teaches more about AWS networking than any diagram, and it is the foundation for everything else on AWS — EKS clusters, RDS databases and serverless workloads all sit inside the network you just learned to build.



