DEV Community

Cover image for Advanced VPC Networking: Detailed Breakdown
Sushant Gaurav
Sushant Gaurav

Posted on

Advanced VPC Networking: Detailed Breakdown

Amazon Web Services (AWS) VPC (Virtual Private Cloud) allows you to build a custom network environment where you can define and control various aspects of your infrastructure. In this article, we will dive deeper into how communication works within a VPC, security best practices using NACLs and Security Groups, and how to effectively apply them.

1. How Does Communication Work in a VPC?

Communication in a VPC involves routing traffic between your AWS resources, the internet, and on-premises networks. Let’s explore the flow of traffic from the internet to various VPC components.

1.1 Traffic Flow via Internet Gateway (IGW)

An Internet Gateway (IGW) allows communication between instances within your VPC and the internet. For instance in public subnets to send and receive traffic from the internet, they need to have a public IP or an Elastic IP.

Example:

Consider a web server in a public subnet:

  1. The web server (EC2 instance) initiates a request to the internet (e.g., fetching data from a public API).
  2. The route table for the public subnet sends outbound traffic (0.0.0.0/0) to the Internet Gateway.
  3. The Internet Gateway forwards the traffic to the Internet.

For incoming traffic:

  1. An external user accesses your web server via its public IP.
  2. The request is routed through the IGW and delivered to the web server in the public subnet.

Mermaid Diagram (Traffic via IGW):

Image description

1.2 Traffic Flow via NAT Gateway

For private subnets that do not have direct access to the internet, NAT Gateway comes into play. It allows instances in private subnets to initiate outbound traffic to the internet while preventing inbound traffic from reaching those instances.

Example:

Consider an EC2 instance in a private subnet that needs to download software updates:

  1. The EC2 instance sends an outbound request (e.g., to download a patch) to the NAT Gateway.
  2. The NAT Gateway forwards the traffic to the Internet Gateway, which connects to the Internet.
  3. The response from the internet is routed back through the NAT Gateway and delivered to the EC2 instance in the private subnet.

Mermaid Diagram (Traffic via NAT Gateway):

Image description

1.3 Traffic Flow via Route Tables

Route tables control how traffic flows within the VPC and between subnets. By default, a VPC comes with a local route that enables instances in the same VPC to communicate with each other.

Example:

Consider two instances in different subnets:

  1. Instance A in a public subnet tries to communicate with Instance B in a private subnet.
  2. The route table for the public subnet directs the traffic to the private subnet’s CIDR block (10.0.1.0/24).
  3. The private subnet route table directs the traffic back to the local network.
  4. The traffic reaches Instance B.

2. Security in VPC

Security is a critical aspect of a VPC setup. AWS provides two primary mechanisms for controlling network traffic: Network Access Control Lists (NACLs) and Security Groups. Understanding when and how to use these components is vital for securing your VPC.

2.1 NACLs: Stateless Network Filtering

Network Access Control Lists (NACLs) act as stateless firewalls for your subnets. They filter traffic at the subnet level based on the rules you define, and they require explicit inbound and outbound rules.

Default Rules:

  • Inbound: By default, NACLs allow all inbound traffic.
  • Outbound: By default, NACLs allow all outbound traffic.

You can modify NACL rules to deny traffic from specific IP ranges or ports.

Example:

For added security, you could configure your NACL to deny all inbound traffic on port 22 (SSH) from the public internet to your private instances.

Rule # Type Protocol Port Range Source Action
100 SSH TCP 22 0.0.0.0/0 Deny

2.2 Security Groups: Stateful Filtering

A Security Group acts as a stateful firewall for EC2 instances. Unlike NACLs, Security Groups are stateful, meaning that if you allow inbound traffic, return traffic is automatically allowed, regardless of outbound rules.

Default Rules:

  • Inbound: By default, all inbound traffic is denied.
  • Outbound: By default, all outbound traffic is allowed.

Example:

If you want to allow HTTP (port 80) traffic to your web server but block all other traffic, you would define the following security group rule:

  • Inbound: Allow TCP port 80 from 0.0.0.0/0.
  • Outbound: Allow all outbound traffic.

2.3 When to Use NACL vs. Security Groups

While both NACLs and Security Groups are used to filter traffic, they differ in their application and use cases.

  • Security Groups are typically used to control traffic to EC2 instances and are associated directly with them.
  • NACLs are better for controlling traffic at the subnet level and are often used for broad filtering across multiple instances.

When to Use Which?

  • Use NACLs for filtering traffic entering or leaving your subnets. For example, if you need to block a certain IP range from accessing all resources in a subnet, use NACL.
  • Use Security Groups to control access to specific EC2 instances, such as allowing only HTTP traffic to a web server.

Advantages and Disadvantages:

Feature NACLs Security Groups
Statefulness Stateless (requires both inbound and outbound rules) Stateful (automatic return traffic)
Granularity Operates at subnet level Operates at instance level
Ease of Use More complex, requires more rules Simpler to manage per-instance
Use Case Broad traffic control at subnet level Fine-grained control at instance level

3. Best Practices in VPC Security

To ensure the security of your VPC, it’s important to combine both NACLs and Security Groups in a layered approach.

3.1 Layered Security Approach

  1. Use NACLs for Subnet-Level Security: Apply NACLs to block unwanted traffic to your subnets. For example, block traffic on ports like 22 (SSH) to prevent brute-force attacks on your private instances.
  2. Use Security Groups for Instance-Level Control: Apply security groups to limit which services can interact with each EC2 instance. For example, ensure that only your load balancer can communicate with your web server on port 80.

3.2 Common Security Scenarios:

  • Public Web Server Security: Only allow inbound HTTP/HTTPS traffic from the internet, deny all other inbound traffic.
  • Database Server Security: Allow inbound traffic only from trusted web servers (via Security Groups), and block all other inbound traffic.
  • Bastion Host Access: Restrict SSH access to the bastion host from specific IP addresses using a combination of Security Groups and NACLs.

3.3 Common Mistakes to Avoid:

  • Relying Solely on One Layer: Using only Security Groups or NACLs can leave vulnerabilities. For example, relying solely on Security Groups could allow unnecessary traffic from other instances in the same subnet.
  • Overly Permissive Rules: Allowing 0.0.0.0/0 in both NACLs and Security Groups can expose your resources to unnecessary risks. Always restrict access to only trusted IP ranges.

4. End-to-End Communication Flow: A Comprehensive Diagram

Here’s a mermaid diagram illustrating how the communication flow works, from the internet to the VPC, across public and private subnets, and back-end resources like web servers and databases.

Image description

What's Next?

In the next article, we’ll dive into the FAQs and Common Challenges in VPC.

We’ll also include detailed mermaid diagrams and examples. Stay tuned for "FAQs and Common Challenges in VPC"!

Top comments (0)