
Inside an AWS VPC: How Subnets, Route Tables, NAT Gateways, and Security Groups Work Together
Understand how AWS networking works by exploring VPCs, public and private subnets, route tables, Internet Gateways, NAT Gateways, Security Groups and VPC endpoints. Learn how these components work together to build secure, isolated and cost-conscious cloud architectures.
Inside an AWS VPC: How Subnets, Route Tables, NAT Gateways, and Security Groups Work Together
Introduction
When deploying an application to the cloud, getting the application running is only one part of the process. Another important question is: how should its components communicate securely?
Consider a typical web application. Users need to access its frontend and backend, but the database should not be directly exposed to the public internet. The backend needs database access, and some services may need outbound internet connectivity for updates or external APIs.
This is where Amazon Virtual Private Cloud (Amazon VPC) becomes important.
An Amazon VPC provides a logically isolated network in AWS where developers can define IP address ranges, subnets, routing and network security rules.
In this article, we'll explore the core components of a VPC and understand how they work together through a practical application architecture.
1. What is an Amazon VPC?
Amazon VPC allows you to create a virtual network dedicated to your AWS account.
You control important networking decisions, including:
- IP address ranges.
- Subnet configuration.
- Routing rules.
- Network connectivity.
- Security controls.
For example, consider a VPC with the IPv4 CIDR block:
10.0.0.0/16 This provides a private IP address range that can be divided into smaller subnet ranges.
A simplified network layout could be:
Component | CIDR range | Purpose |
|---|---|---|
VPC | 10.0.0.0/16 | Main network |
Public subnet | 10.0.1.0/24 | Internet-facing resources |
Private application subnet | 10.0.2.0/24 | Backend services |
Private database subnet | 10.0.3.0/24 | Database resources |
Planning CIDR ranges carefully helps avoid address conflicts and makes future expansion easier.
2. Public and private subnets
A subnet is a range of IP addresses within a VPC.
Subnets are commonly classified as public or private based on their route tables.
Public subnet
A public subnet has a route to an Internet Gateway. Resources also need suitable public IP addressing and security rules to communicate directly with the internet.
Typical resources include:
- Internet-facing load balancers.
- Public application endpoints.
- Bastion hosts, where appropriate.
Private subnet
A private subnet does not have a direct route to an Internet Gateway.
Typical resources include:
- Backend application servers.
- Internal services.
- Databases.
An important distinction: a subnet is not public simply because its name contains the word "public". Its routing configuration determines its internet connectivity.
3. Internet Gateway: connecting a VPC to the internet
An Internet Gateway (IGW) is a VPC component that enables communication between resources in a VPC and the internet.
To make a subnet public, its associated route table generally needs a route such as:
0.0.0.0/0 → Internet Gateway However, this route alone does not automatically make every resource publicly accessible.
The resource also needs appropriate public addressing and security permissions.
For example, an EC2 instance with a public IPv4 address, a route through the IGW and an appropriate Security Group rule can receive internet traffic.
4. Route tables: deciding where traffic goes
A route table contains rules that determine how network traffic is directed.
Every subnet is associated with a route table, either explicitly or through the VPC's main route table.
A public subnet might have:
Destination | Target |
|---|---|
10.0.0.0/16 | Local |
0.0.0.0/0 | Internet Gateway |
A private application subnet might have:
Destination | Target |
|---|---|
10.0.0.0/16 | Local |
0.0.0.0/0 | NAT Gateway |
The local route allows resources within the VPC to communicate according to their security rules.
The default route directs traffic destined for other networks to the configured gateway.
Understanding route tables is essential when troubleshooting connectivity problems.
5. NAT Gateway: outbound connectivity for private resources
What if a backend server in a private subnet needs to download software updates or contact an external API?
Giving it a public IP address is not always the right solution.
A NAT Gateway allows resources in private subnets to initiate outbound internet connections without allowing unsolicited inbound internet connections to those private resources.
A typical configuration looks like this:
- Create a NAT Gateway in a public subnet.
- Associate an Elastic IP with the NAT Gateway.
- Configure the private subnet's default route to use the NAT Gateway.
- Ensure the public subnet has a route to the Internet Gateway.
One important cost consideration is that NAT Gateways have hourly and data-processing charges. For small student projects, these costs can be significant compared with the workload.
Where appropriate, VPC endpoints can provide private connectivity to supported AWS services without sending that traffic through a NAT Gateway.
6. Security Groups vs Network ACLs
AWS provides multiple network security controls. Two important ones are Security Groups and Network Access Control Lists (Network ACLs).
Security Groups
- Operate at the network interface level.
- Are stateful.
- Support allow rules.
- Automatically allow response traffic for permitted connections.
Network ACLs
- Operate at the subnet level.
- Are stateless.
- Support allow and deny rules.
- Require inbound and outbound rules to be configured separately.
For most application architectures, Security Groups are the primary resource-level network control.
For example:
Resource | Inbound rule |
|---|---|
Application load balancer | HTTPS from approved public sources |
Backend service | Application port from the load balancer's Security Group |
PostgreSQL database | TCP 5432 from the backend Security Group |
This creates a controlled communication path rather than exposing every component to the internet.
7. Designing a secure application architecture
Let's apply these concepts to a typical web application.
The architecture could contain:
- A public frontend or load balancer.
- Backend application services in private subnets.
- An RDS PostgreSQL database in private database subnets.
- A NAT Gateway for required outbound connectivity.
- Security Groups controlling communication between components.
The request flow would be:
- A user sends an HTTPS request.
- The public-facing load balancer receives the request.
- The load balancer forwards it to the backend service.
- The backend communicates with the private database.
- The database returns the result to the backend.
- The backend sends the response through the load balancer.
The database does not need a public IP address or direct internet access for this architecture.
A useful security principle is to allow only the communication each component actually needs.
8. VPC Endpoints: private access to AWS services
A VPC Endpoint enables private connectivity between resources in a VPC and supported AWS services without requiring an Internet Gateway or NAT Gateway for that traffic.
Two common types are:
Gateway endpoints
Used for services such as Amazon S3 and DynamoDB.
Interface endpoints
Use AWS PrivateLink and create network interfaces in selected subnets to connect privately to supported services.
For example, a backend application accessing an S3 bucket may use an S3 Gateway Endpoint.
This can reduce NAT Gateway data-processing costs and keep supported service traffic within the AWS network.
However, endpoints have their own pricing and configuration considerations, so they should be selected based on actual requirements.
9. Common networking mistakes
Some mistakes can make an otherwise correctly deployed application inaccessible or unnecessarily expensive.
Mistake 1: Making the database publicly accessible
A database should generally be placed in private database subnets with inbound access restricted to the application Security Group.
Mistake 2: Confusing public subnets with public resources
A public subnet does not automatically expose every resource. Public addressing and security rules also matter.
Mistake 3: Forgetting route table associations
A correctly configured route table is not useful if the intended subnet is associated with another route table.
Mistake 4: Overly permissive Security Groups
Opening database ports or administrative ports to
0.0.0.0/0 unnecessarily increases exposure.Mistake 5: Ignoring NAT Gateway costs
NAT Gateways can introduce recurring charges even when application traffic is low.
Mistake 6: Overlooking Availability Zones
Production architectures should consider subnet placement across multiple Availability Zones to improve resilience.
10. Cost-conscious networking for student projects
Networking decisions can affect AWS bills just as much as compute and storage choices.
Some practical recommendations include:
- Avoid creating NAT Gateways unless private resources actually need outbound internet access.
- Evaluate VPC endpoints for supported AWS services.
- Use appropriate subnet and Availability Zone designs.
- Remove unused Elastic IP addresses and networking resources.
- Review AWS billing and cost reports regularly.
- Set AWS Budgets and cost alerts before experimenting.
AWS pricing and Free Tier eligibility can change, so always verify current pricing before deploying resources.
Conclusion
Amazon VPC is more than a place to launch EC2 instances. It provides the networking foundation that determines how application components communicate, which resources are reachable and how traffic is controlled.
Understanding subnets, route tables, Internet Gateways, NAT Gateways, Security Groups and VPC endpoints helps developers move beyond simply deploying applications to designing secure and maintainable cloud infrastructure.
My biggest takeaway is that good cloud networking is not about making everything accessible. It is about making the right connections available while keeping everything else isolated.
Further Reading
- Amazon VPC documentation: https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html
- VPC route tables: https://docs.aws.amazon.com/vpc/latest/userguide/VPC\_Route\_Tables.html
- Internet Gateways: https://docs.aws.amazon.com/vpc/latest/userguide/VPC\_Internet\_Gateway.html
Note: The architecture described here is an educational reference design. It is not a claim that these AWS networking components are currently deployed in a production application.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article