The Journey of a Packet Inside an AWS VPC: What Really Happens When Your Application Connects to the Internet
Have you ever wondered what actually happens when an EC2 instance communicates with the internet?
When I first started learning AWS networking, I memorized terms like VPC, subnet, route table, Internet Gateway, NAT Gateway, and security group. But knowing the definitions wasn't enough. The real understanding came when I started asking:
“What happens to the packet?”
In this article, we'll follow a packet from an EC2 instance inside an AWS VPC to the internet and back. By the end, the different VPC components should feel less like separate AWS services and more like parts of one complete journey.
1. First, Where Does Our Packet Live?
Let's start with a simple AWS environment.
Imagine we have:
An AWS Region
A VPC: 10.0.0.0/16
A public subnet: 10.0.1.0/24
An EC2 instance
An Internet Gateway
A route table
A security group
Our architecture looks roughly like this:
INTERNET
|
|
Internet Gateway
|
|
Route Table
|
|
Public Subnet
10.0.1.0/24
|
|
EC2 Instance
10.0.1.10
2. The EC2 Instance Creates a Packet
Our application first needs to communicate with the destination.
For HTTPS traffic, it normally uses:
Protocol: TCP
Destination Port: 443
The operating system creates network packets containing information such as:
Source IP: EC2 private IP
Destination IP: Destination server IP
Protocol: TCP
Destination: Port 443
But the EC2 instance doesn't simply throw the packet onto the internet.
It first needs to determine:
“Where should I send this packet?”
That's where the route table comes in.
3. The Route Table Is the Packet's GPS
Every subnet in a VPC is associated with a route table.
A simplified public subnet route table might look like:
Destination Target
10.0.0.0/16 local
0.0.0.0/0 Internet Gateway
The first route means:
Traffic destined for another resource inside the VPC should stay within the VPC.
The second route means:
For destinations that aren't inside the VPC, send the traffic toward the Internet Gateway.
The
0.0.0.0/0 route is known as the default route.So if our EC2 instance wants to communicate with an internet destination, the route table says:
EC2
↓
Is destination inside 10.0.0.0/16?
↓
No
↓
Use 0.0.0.0/0
↓
Internet Gateway
This is one of the most important concepts to understand when learning AWS networking.
4. The Security Group Checks the Traffic
Before traffic can reach the EC2 instance, AWS evaluates the relevant security controls.
An EC2 security group might have an outbound rule such as:
Type: HTTPS
Protocol: TCP
Port: 443
Destination: 0.0.0.0/0
This allows the instance to initiate HTTPS connections to external destinations.
Security groups are stateful.
That means if an allowed connection is established, the return traffic is automatically allowed as part of that connection; you don't need to create a separate inbound rule just for the response traffic.
This is different from a stateless network ACL, which evaluates inbound and outbound traffic independently.
5. The Packet Reaches the Internet Gateway
Now our packet has a destination outside the VPC.
The route table tells AWS:
0.0.0.0/0 → Internet Gateway
The Internet Gateway (IGW) provides the connection between the VPC and the internet.
But here's an important point:
Simply attaching an Internet Gateway to a VPC does NOT automatically make an EC2 instance internet-accessible.
Several things need to line up.
For an EC2 instance in a public subnet to communicate directly with the internet, you generally need:
- A route to the Internet Gateway.
- An appropriate public IPv4 address or Elastic IP.
- Appropriate security group rules.
- Network ACLs that don't block the traffic.
So "public subnet" doesn't mean the subnet itself is magically connected to the internet.
It's the route configuration and addressing that make the path possible.
6. What Is a Public Subnet?
This is a common interview question.
A subnet is considered public when its route table contains a route to an Internet Gateway.
For example:
10.0.0.0/16 → local
0.0.0.0/0 → igw-xxxxxxxx
A private subnet typically does not have a direct route to an Internet Gateway for internet-bound traffic.
For example:
10.0.0.0/16 → local
0.0.0.0/0 → NAT Gateway
So the difference isn't simply:
"Public subnet = internet."
The important difference is how the subnet routes traffic.
7. What About the EC2 Private IP?
Here's another important concept.
An EC2 instance inside a VPC has a private IP address, for example:
10.0.1.10
Private IP addresses are used for communication inside the VPC and connected networks.
If the EC2 instance needs to communicate directly with the public internet, it also needs an appropriate public addressing mechanism, such as a public IPv4 address or Elastic IP.
AWS handles the translation between the instance's private addressing and its public-facing connectivity as part of the VPC networking infrastructure.
So conceptually:
EC2
Private IP
10.0.1.10
|
↓
AWS VPC networking
|
↓
Public connectivity
|
↓
Internet
8. The Packet Reaches the Destination
Once the packet leaves the VPC and travels across the internet, it eventually reaches the destination server.
The destination receives the request and sends a response.
For example:
EC2 → HTTPS Request → Internet → Server
Server → HTTPS Response → Internet → EC2
But how does the response know where to go?
That's where the connection state and AWS networking mechanisms become important.
Because the EC2 instance initiated the connection and the traffic was permitted, the response can return through the established connection.
The packet's journey isn't complete until that response reaches the application on the EC2 instance.
9. What Changes If the EC2 Instance Is in a Private Subnet?
Now let's make the architecture more realistic.
Suppose our EC2 instance doesn't have a public IP.
Instead, it's inside a private subnet:
INTERNET
|
|
Internet Gateway
|
|
NAT Gateway
|
|
Private Subnet
|
|
EC2 Instance
10.0.2.10
The EC2 instance still wants to access the internet.
But it doesn't have a direct path through an Internet Gateway.
So what can it use?
A NAT Gateway.
10. The NAT Gateway
NAT stands for Network Address Translation.
A NAT Gateway allows resources in a private subnet to initiate connections to destinations outside the VPC without making those private resources directly reachable from the internet.
The routing might look like:
Private EC2
|
↓
Private Subnet Route Table
|
↓
NAT Gateway
|
↓
Internet Gateway
|
↓
Internet
For example:
10.0.2.10
|
| HTTPS request
↓
NAT Gateway
|
| Translated traffic
↓
Internet
The important idea is:
Private instances can initiate outbound internet connections through a NAT Gateway, while the internet cannot directly initiate a new connection to those instances through the NAT Gateway.
This is one reason private subnets are commonly used for backend application servers and other resources that shouldn't have direct inbound internet access.
11. What Does the Route Table Look Like?
For our private subnet, we might have:
Destination Target
10.0.0.0/16 local
0.0.0.0/0 NAT Gateway
Now imagine the EC2 instance wants to access a public website.
The packet follows:
EC2
↓
Private Subnet
↓
Route Table
↓
0.0.0.0/0
↓
NAT Gateway
↓
Internet Gateway
↓
Internet
↓
Destination
That's the key difference between a public and private subnet.
12. Where Do Network ACLs Fit?
We've talked about security groups, but there's another layer:
Network Access Control Lists (NACLs).
A NACL operates at the subnet level.
You can think about the difference like this:
Security Group
↓
Controls traffic associated with ENIs/resources
NACL
↓
Controls traffic at the subnet boundary
Another important difference:
Security groups are stateful. NACLs are stateless.
So when troubleshooting connectivity, you may need to check:
Security group rules
NACL rules
Route tables
Internet Gateway
NAT Gateway
IP addressing
DNS
13. The Complete Journey
Let's put everything together.
Public EC2 architecture
INTERNET
|
|
Internet Gateway
|
|
Route Table
|
|
Public Subnet
|
|
Security Group
|
|
EC2 Instance
The journey:
EC2
↓
Security Group
↓
Route Table
↓
Internet Gateway
↓
Internet
↓
Destination Server
And the response comes back through the established connection.
14. Private EC2 Architecture
Now compare that with a private subnet:
INTERNET
|
|
Internet Gateway
|
|
NAT Gateway
|
|
Private Subnet
|
|
Security Group
|
|
EC2 Instance
The outbound journey becomes:
EC2
↓
Security Group
↓
Private Route Table
↓
NAT Gateway
↓
Internet Gateway
↓
Internet
↓
Destination
This architecture is extremely common in real AWS environments.
15. What Happens If Something Is Misconfigured?
This is where understanding the packet journey becomes extremely useful.
Suppose your EC2 instance can't access the internet.
Instead of randomly changing settings, follow the packet.
Step 1 — Check the instance
Does the EC2 instance have the expected private/public IP configuration?
Step 2 — Check the security group
Is outbound traffic allowed?
For HTTPS:
TCP
443
Destination: 0.0.0.0/0
Step 3 — Check the route table
For a public subnet:
0.0.0.0/0 → Internet Gateway
For a private subnet:
0.0.0.0/0 → NAT Gateway
Step 4 — Check the Internet Gateway
Is the Internet Gateway attached to the VPC?
Step 5 — If using a private subnet
Is the NAT Gateway available and correctly routed?
Step 6 — Check NACLs
Are inbound or outbound rules blocking the traffic?
Step 7 — Check DNS
If you can reach an IP address but cannot resolve a domain name, the problem may be DNS rather than routing.
This approach is much more effective than simply restarting the instance and hoping for the best.
16. The Biggest Lesson I Learned
When I first studied AWS networking, I tried to memorize every AWS networking component individually.
But that wasn't the best way to understand it.
Instead, I started asking one question:
Where is my packet now, and where does it need to go next?
That question connects everything.
The:
Subnet determines where the resource lives.
Route table determines where traffic should go.
Security group controls allowed traffic at the resource level.
NACL controls traffic at the subnet boundary.
Internet Gateway provides the VPC's path to the internet.
NAT Gateway provides outbound internet connectivity for private resources.
Once you think about the packet's journey, these components become much easier to understand.
Conclusion
AWS networking can seem complicated because there are so many terms to learn.
But you don't necessarily need to memorize everything at once.
Start with one packet.
Ask:
Where does it start?
What is its destination?
Which route does it take?
Is the traffic allowed?
Does it need an Internet Gateway or NAT Gateway?
How does the response get back?
Once you understand that journey, the AWS VPC becomes much less mysterious.
And that's the way I now think about AWS networking:
Don't just memorize the components. Follow the packet.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article