Traffic Control in AWS VPC with Security Groups and NACLs

In AWS, Security Groups and Network ACLs (NACLs) are the core tools for controlling inbound and outbound traffic within Virtual Private Clouds (VPCs). Think of them as layers of security that, together, help keep your resources safe by blocking unwanted traffic. While they serve a similar purpose, each works at a different level and has distinct features that make them effective when combined.

1. Security Groups as room-level locks

Imagine each instance or resource within your VPC is like a room in a house. A Security Group acts as the lock on each of those doors. It controls who can get in and who can leave and remembers who it lets through so it doesn’t need to keep asking. Security Groups are stateful, meaning they keep track of allowed traffic, both inbound and outbound.

Key Features

  • Stateful behavior: If traffic is allowed in one direction (e.g., HTTP on port 80), it automatically allows the response in the other direction, without extra rules.
  • Instance-Level application: Security Groups apply directly to individual instances, load balancers, or specific AWS services (like RDS).
  • Allow-Only rules: Security Groups only have “allow” rules. If a rule doesn’t permit traffic, it’s blocked by default.

Example

For a database instance on RDS, you might configure a Security Group that allows incoming traffic only on port 3306 (the default port for MySQL) and only from instances within your backend Security Group. This setup keeps the database shielded from any other traffic.

2. Network ACLs as property-level gates

If Security Groups are like room locks, NACLs are more like the gates around a property. They filter traffic at the subnet level, screening everything that tries to get in or out of that part of the network. NACLs are stateless, so they don’t keep track of traffic. If you allow inbound traffic, you’ll need a separate rule to permit outbound responses.

Key Features

  • Stateless behavior: Traffic allowed in one direction doesn’t mean it’s automatically allowed in the other. Each direction needs explicit permission.
  • Subnet-Level application: NACLs apply to entire subnets, meaning they cover all resources within that network layer.
  • Allow and Deny rules: Unlike Security Groups, NACLs allow both “allow” and “deny” rules, giving you more granular control over what traffic is permitted or blocked.

Example

For a public-facing web application, you might configure a NACL to block any IPs outside a specific range or region, adding a layer of protection before traffic even reaches individual instances.

Best practices for using security groups and NACLs together

Combining Security Groups and NACLs creates a multi-layered security setup known as defense in depth. This way, if one layer misconfigures, the other provides a safety net.

Use security groups as your first line of defense

Since Security Groups are stateful and work at the instance level, they should define specific rules tailored to each resource. For example, allow only HTTP/HTTPS traffic for frontend instances, while backend instances only accept requests from the frontend Security Group.

Reinforce with NACLs for subnet-level control

NACLs are stateless and ideal for high-level filtering, such as blocking unwanted IP ranges. For example, you might use a NACL to block all traffic from certain geographic locations, enhancing protection before traffic even reaches your Security Groups.

Apply NACLs for public traffic control

If your application receives public traffic, use NACLs at the subnet level to segment untrusted traffic, keeping unwanted visitors at bay. For example, you could configure NACLs to block all ports except those explicitly needed for public access.

Manage NACL rule order carefully

Remember that NACLs evaluate traffic based on rule order. Rules with lower numbers are prioritized, so keep your most restrictive rules first to ensure they’re applied before others.

Applying layered security in a Three-Tier architecture

Imagine a three-tier application with frontend, backend, and database layers, each in its subnet within a VPC. Here’s how you could use Security Groups and NACLs:

Security Groups

  • Frontend: Security Group allows inbound traffic on ports 80 and 443 from any IP.
  • Backend: Security Group allows traffic only from the frontend Security Group, for example, on port 8080.
  • Database: Security Group allows traffic only from the backend Security Group, on port 3306 (for MySQL).

NACLs

  • Frontend Subnet: NACL allows inbound traffic only on ports 80 and 443, blocking everything else.
  • Backend Subnet: NACL allows inbound traffic only from the frontend subnet and blocks all other traffic.
  • Database Subnet: NACL allows inbound traffic only from the backend subnet and blocks all other traffic.

In a few words

  • Security Groups: Act at the instance level, are stateful, and only permit “allow” rules.
  • NACLs: Act at the subnet level, are stateless, and allow both “allow” and “deny” rules.
  • Combining Security Groups and NACLs: This approach gives you a layered “defense in depth” strategy, securing traffic control across every layer of your VPC.

Share