
AWS VPC Explained: How Networking Works in the Cloud
Learn Amazon VPC from scratch! Explore CIDR blocks, public and private subnets, Internet Gateways, NAT Gateways, route tables, security groups, and how to build secure AWS network architectures.
AWS VPC Explained: How Networking Works in the Cloud
Introduction
Have you ever wondered how applications running on AWS communicate with each other while staying protected from unwanted internet traffic?
When you launch an EC2 instance, create a database, or deploy an application on AWS, networking plays a very important role.
AWS provides a service called Amazon Virtual Private Cloud (VPC) that allows you to create and manage your own virtual network in the cloud.
With VPC, you can control IP address ranges, create subnets, configure routing, and manage network access to your resources.
In this article, we will explore Amazon VPC from scratch, understand its important components, and see how they work together in a real-world application architecture.
1. What Is Amazon VPC?
Amazon VPC (Virtual Private Cloud) is a logically isolated virtual network within AWS where you can launch and manage cloud resources.
Think of it as your own private network inside the AWS cloud.
Within a VPC, you can deploy:
- EC2 instances
- RDS databases
- Application Load Balancers
- Private application servers
- Other AWS networking resources
For example, imagine you are building an online shopping application.
You might want:
- A web server that customers can access.
- An application server that handles business logic.
- A database that should not be directly accessible from the internet.
VPC allows you to design this network architecture and control how these resources communicate.
2. Understanding CIDR Blocks and IP Addresses
Before creating a VPC, you need to understand CIDR.
CIDR stands for Classless Inter-Domain Routing. It defines the IP address range available within a network.
For example:
10.0.0.0/16This CIDR block contains 65,536 IPv4 addresses.
Here are some examples:
CIDR Block | Total IPv4 Addresses |
|---|---|
10.0.0.0/16 | 65,536 |
10.0.1.0/24 | 256 |
10.0.2.0/24 | 256 |
192.168.1.0/24 | 256 |
AWS reserves five IPv4 addresses in every subnet, so the number available for resources is lower.
For example, if you create a VPC with
10.0.0.0/16 , you can divide it into smaller subnets such as:VPC: 10.0.0.0/16
Public Subnet: 10.0.1.0/24Private Subnet: 10.0.2.0/24Database Subnet: 10.0.3.0/24This makes network organization easier.
3. What Are Public and Private Subnets?
A subnet is a smaller IP address range inside a VPC.
Subnets help organize resources and control their network connectivity.
There are two commonly used subnet designs.
Public Subnet
A public subnet has a route that directs internet-bound traffic to an Internet Gateway.
It is commonly used for:
- Public-facing web servers
- Internet-facing load balancers
- Bastion hosts
An EC2 instance also needs suitable public addressing and security rules to communicate directly with the internet.
Private Subnet
A private subnet does not have a direct route to an Internet Gateway.
It is commonly used for:
- Application servers
- Databases
- Internal services
- Backend processing systems
Private resources can still initiate outbound internet connections through a NAT Gateway when configured.
However, they are not directly reachable through unsolicited inbound connections from the public internet using that NAT Gateway.
Real-world example:
In an online shopping application, customers communicate with the public-facing web layer, while the database remains in a private subnet.
This reduces the database's exposure to the internet.
4. What Is an Internet Gateway?
An Internet Gateway (IGW) is a VPC component that enables communication between resources in your VPC and the internet.
Suppose you launch an EC2 instance and want users to access your website.
You generally need:
- An Internet Gateway attached to your VPC.
- A route table with a route to the Internet Gateway.
- A public IPv4 address or another suitable public addressing configuration.
- Appropriate Security Group and Network ACL rules.
Example route table:
Destination | Target |
|---|---|
10.0.0.0/16 | local |
0.0.0.0/0 | Internet Gateway |
Here,
0.0.0.0/0 represents all IPv4 destinations not covered by a more specific route.The Internet Gateway allows the VPC to communicate with the internet, but it does not automatically make every resource publicly accessible.
5. What Is a NAT Gateway?
A NAT Gateway (Network Address Translation Gateway) allows resources in a private subnet to initiate connections to external services.
For example, imagine an EC2 instance in a private subnet needs to download software updates.
You want it to access the internet for outbound communication without giving it direct public internet access.
A NAT Gateway helps achieve this.
A typical public NAT Gateway setup works like this:
- Create a NAT Gateway in a public subnet.
- Associate an Elastic IP address with it.
- Configure the private subnet's route table to send internet-bound traffic to the NAT Gateway.
- Allow the required traffic through applicable security rules.
Example private route table:
Destination | Target |
|---|---|
10.0.0.0/16 | local |
0.0.0.0/0 | NAT Gateway |
Important: NAT Gateways have hourly and data-processing charges. Consider their cost and availability requirements before deploying them.
6. Understanding Route Tables
A route table determines where network traffic is directed.
Every subnet is associated with a route table, either explicitly or through the VPC's main route table.
For example, a public subnet route table might look like this:
Destination | Target |
|---|---|
10.0.0.0/16 | local |
0.0.0.0/0 | Internet Gateway |
A private subnet route table might look like this:
Destination | Target |
|---|---|
10.0.0.0/16 | local |
0.0.0.0/0 | NAT Gateway |
The local route allows communication within the VPC's address range.
The default route determines where other IPv4 traffic is sent.
Understanding route tables is essential when troubleshooting connectivity problems in AWS.
7. Security Groups vs Network ACLs
AWS provides two important networking security features: Security Groups and Network Access Control Lists (Network ACLs).
Both help control network traffic, but they work differently.
Feature | Security Groups | Network ACLs |
|---|---|---|
Applied to | Network interfaces | Subnets |
Type | Stateful | Stateless |
Rules | Allow rules | Allow and deny rules |
Return traffic | Automatically allowed for permitted connections | Must be explicitly allowed by applicable rules |
Rule evaluation | All applicable rules | Rules evaluated by rule number |
Security Groups
Security Groups act as virtual firewalls for network interfaces.
For example, an EC2 web server might have:
- HTTP: TCP port 80
- HTTPS: TCP port 443
- SSH: TCP port 22 from a trusted IP address only
Network ACLs
Network ACLs operate at the subnet level.
They support both allow and deny rules and can provide an additional layer of network protection.
Best practice: Use Security Groups to control access to individual resources and Network ACLs when you need subnet-level traffic filtering.
8. How EC2 Instances Communicate Inside a VPC
Imagine you have two EC2 instances:
Web Server:10.0.1.10
Database Server:10.0.2.20Both instances belong to the same VPC.
The VPC's local route allows communication between these subnets.
However, communication also depends on Security Groups, Network ACLs, and the application listening on the destination port.
For example, if your database uses MySQL on port 3306, you can configure its Security Group to allow TCP port 3306 from the web server's Security Group.
This is more secure than allowing database access from every IP address.
9. Build a Real-World Three-Tier AWS Architecture
Let's understand how these components work together in an actual application.
Imagine an online shopping platform with three layers.
Tier 1: Presentation Layer
This layer receives requests from customers.
It may contain:
- Application Load Balancer
- Public subnets
- Internet-facing services
Tier 2: Application Layer
This layer processes requests and runs business logic.
It may contain:
- EC2 instances
- Backend applications
- Private subnets
Tier 3: Database Layer
This layer stores application information.
It may contain:
- Amazon RDS
- MySQL or PostgreSQL
- Private database subnets
The basic request flow looks like this:
Internet Users | v Internet Gateway | v Application Load Balancer | v EC2 Application Servers Private Subnets | v Amazon RDS Private SubnetsFor better availability, production architectures commonly distribute resources across multiple Availability Zones.
This helps reduce the impact of an individual Availability Zone failure.
10. Common AWS VPC Mistakes Beginners Should Avoid
Here are some common mistakes when working with VPC:
- Choosing overlapping CIDR ranges when connecting multiple networks.
- Assuming that every subnet is automatically public.
- Forgetting to attach an Internet Gateway.
- Configuring incorrect route table entries.
- Allowing SSH access from anywhere.
- Exposing databases directly to the internet.
- Forgetting NAT Gateway charges.
- Confusing Security Groups with Network ACLs.
- Ignoring subnet availability across multiple Availability Zones.
- Not checking routing and security rules when troubleshooting connectivity.
Understanding these mistakes can help you design safer and more reliable AWS networks.
11. Practical AWS VPC Project for Beginners
Now let's turn the concepts into a practical exercise.
Project: Create a Secure AWS VPC Architecture
Try building the following infrastructure:
- One custom VPC.
- One public subnet.
- One private subnet.
- One Internet Gateway.
- One NAT Gateway.
- Two route tables.
- Two EC2 instances.
- Appropriate Security Groups.
Testing your architecture
After creating the infrastructure, test these questions:
- Can you access the public EC2 instance?
- Can the private EC2 instance access the internet through the NAT Gateway?
- Can the public EC2 instance communicate with the private EC2 instance on an allowed port?
- Is the private instance inaccessible through direct inbound internet access?
- Are the Security Groups allowing only the required traffic?
Try to understand why each connection succeeds or fails.
Cost warning: NAT Gateways, EC2 instances, public IPv4 addresses, and other resources can incur charges. Review current AWS pricing, monitor your billing, and clean up resources after completing your exercise. Deleting a NAT Gateway alone does not remove every resource associated with it.
Final Thoughts
Amazon VPC is one of the fundamental building blocks of AWS cloud infrastructure.
It gives you control over how your cloud resources communicate, how they connect to the internet, and how network traffic is protected.
Understanding VPC, subnets, gateways, route tables, and network security is essential for anyone learning AWS.
My recommendation for beginners is to start with a small VPC, create public and private subnets, launch EC2 instances, and experiment with routing and security rules.
The best way to understand cloud networking is to build it, test it, and troubleshoot it yourself.
Let's discuss!
Which AWS networking component do you find most interesting: VPC, Subnets, Internet Gateway, NAT Gateway, or Security Groups?
Share your thoughts and experiences in the comments.
#AWS #AmazonVPC #CloudComputing #AWSNetworking #AWSBeginners
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article