AWS Builder Center

Amazon VPC Explained: Subnets, Gateways, and Network Security

Every EC2 instance, database, and many other AWS resources live inside a network. That network is Amazon VPC (Virtual Private Cloud): your own logically isolated section of AWS, where you control the IP ranges, routing, and what can talk to what. Understanding a few pieces makes most AWS networking questions much easier.

The building blocks

  • VPC: your private network, defined by an IP range in CIDR notation, such as  10.0.0.0/16 .
  • Subnet: a slice of the VPC’s IP range. Each subnet lives in a single Availability Zone.
  • Route table: rules that say where network traffic goes.
  • Internet Gateway (IGW): connects your VPC to the internet.
  • NAT Gateway: lets private resources reach the internet without being reachable from it.
Every Region comes with a default VPC, which is why you can launch an EC2 instance without setting up any networking. For real projects, it’s better to build your own.

Public vs. private subnets

The difference comes down to routing:
  • A public subnet has a route to an Internet Gateway, so its resources can be reached from the internet (if they also have a public IP and the security rules allow it).
  • A private subnet has no direct route to the Internet Gateway. Its resources can’t be reached from outside.
A typical layout puts load balancers in public subnets and application servers and databases in private ones. If a private resource needs to download updates, add a NAT Gateway in a public subnet and route outbound traffic through it. NAT Gateways charge by the hour and by data processed, so they’re worth keeping an eye on.

Security groups vs. network ACLs

You get two layers of traffic control:

Security group
Network ACL
Applies to
Instance or network interface
Subnet
State
Stateful
Stateless
Rules
Allow only
Allow and deny
Evaluation
All rules together
In numbered order
Stateful means return traffic is allowed automatically. With stateless network ACLs, you must allow both directions yourself. Most setups rely mainly on security groups and leave network ACLs at their defaults.

Reaching AWS services privately

By default, a private instance reaching S3 would go out through a NAT Gateway. VPC endpoints keep that traffic on the AWS network instead:
  • Gateway endpoints support S3 and DynamoDB, and they’re free.
  • Interface endpoints (powered by AWS PrivateLink) support many other services and charge by the hour and by data.
Using a gateway endpoint for S3 can cut NAT costs and keep traffic off the public internet.

Connecting VPCs

VPC peering connects two VPCs so they can talk using private IPs. It isn’t transitive: if A is peered with B and B with C, A still can’t reach C. For many VPCs, look at AWS Transit Gateway. And make sure the CIDR ranges of connected VPCs don’t overlap.

Visibility

Turn on VPC Flow Logs to record accepted and rejected traffic. They’re useful for troubleshooting “why can’t these two things connect?” and for security reviews.

Common mistakes

  • Choosing a CIDR range too small, or one that overlaps with another network you’ll need to connect later.
  • Putting databases in public subnets.
  • Forgetting the route table, so a “public” subnet has no route to the internet.
  • Leaving NAT Gateways running in test environments.

Conclusion

A VPC is a few simple ideas working together: an IP range, subnets, route tables, gateways, and two layers of firewall rules. Put internet-facing pieces in public subnets, keep everything else private, and use endpoints to reach AWS services safely. Delete your test VPC resources, especially NAT Gateways, when you’re done.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article