Network Security: Security Groups and NACLs

Two firewall layers in a VPC: stateful security groups on resources and stateless network ACLs on subnets.

What is it?

AWS gives you two built-in packet filters inside a VPC:

  • Security group (SG): attached to an instance's network interface. Stateful: if a request is allowed in, its reply is allowed out automatically, and vice versa. Only allow rules exist; anything not allowed is denied.
  • Network ACL (NACL): attached to a subnet. Stateless: inbound and outbound are judged separately, so you must also allow return traffic (usually ephemeral ports). Has allow and deny rules, checked in number order.

Defaults: a new security group denies all inbound and allows all outbound. The default NACL of a default VPC allows all traffic both ways; a NACL you create starts by denying everything.

Security groups can use other security groups as sources, so you can say 'the app tier may talk to the database tier' without hard-coding IPs.

Exam favourite: to block one specific IP address, use a NACL. Security groups can't deny, so they can't block a single address. NACL rules target CIDR ranges: /32 is exactly one IPv4 address, /0 is all addresses.

More details:

  • NACL rules are checked from the lowest number up; the first match wins. Leave gaps (100, 150, 200) so you can slot rules in later.
  • A NACL applies to everything in its subnet, never one instance. Each subnet uses the VPC's default NACL unless you associate another.
  • Security group rules name a protocol and port (SSH 22, HTTP 80, HTTPS 443, PostgreSQL 5432) and a source (a CIDR range, 'My IP', or another security group).
  • Every VPC has a default security group that can't be deleted. A group referenced by another group can't be deleted until the reference is removed.

Explain like I'm 10

A subnet's NACL is the guard at a building's front desk who checks everyone in and everyone out against a list, treating each direction separately. A security group is the receptionist at each company's door who remembers 'I invited this visitor', so no re-check is needed when they leave.

Examples

A web tier security group

Security group: web-sg   (stateful)
Direction  Protocol  Port   Source / Destination     Purpose
---------  --------  -----  -----------------------  -------------------
Inbound    TCP       443    0.0.0.0/0                public HTTPS
Inbound    TCP       22     203.0.113.10/32          admin SSH from office
Outbound   All       All    0.0.0.0/0                default egress

Security group: db-sg
Inbound    TCP       5432   source = web-sg          only app tier may connect
(No other inbound rules -> everything else denied)

A NACL pair (stateless: both directions needed)

Rule  Dir      Protocol  Ports        Source/Dest      Action
----  -------  --------  -----------  ---------------  ------
100   Inbound  TCP       443          0.0.0.0/0        ALLOW
110   Inbound  TCP       1024-65535   0.0.0.0/0        ALLOW   <- replies to our outbound calls
120   Inbound  All       All          198.51.100.0/24  DENY    (blocked bad range; lower number wins)
100   Outbound TCP       1024-65535   0.0.0.0/0        ALLOW   <- replies to clients
110   Outbound TCP       443          0.0.0.0/0        ALLOW
*     Both     All       All          0.0.0.0/0        DENY    (implicit final rule)

Rule 120 never matches traffic already allowed by rule 100 or 110, because NACL rules run in number order. Give specific denies lower numbers than broad allows.

Create and open a security group

SG=$(aws ec2 create-security-group --group-name web-sg \
  --description "web tier" --vpc-id vpc-0abc1234 --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id "$SG" \
  --protocol tcp --port 443 --cidr 0.0.0.0/0

How it works

Follow one request: a client sends HTTPS to a web server in a subnet. It first meets the subnet's NACL inbound rules. If allowed, it reaches the instance's security group inbound rules.

The reply skips the security group's outbound check (it is tracked as part of the connection). It must still pass the NACL outbound rules, because NACLs don't track connections.

NACLs are blunt (per subnet, IP/port based), so most daily control uses security groups. NACLs are an extra layer, handy for blocking an IP range at the subnet edge.

  client --> [ NACL in ] --> [ SG in ] --> instance
  client <-- [ NACL out ] <-- (reply auto-allowed by SG)
              ^ stateless: needs its own rule

Why does it exist?

Defense in depth: one mistake shouldn't expose everything. Instance-level and subnet-level filters give layered protection: SGs for precise allow-lists, NACLs for coarse allow/deny at the boundary.

When to use it

Use security groups on every resource, with tight sources. Use NACLs when you need explicit denies or a coarse subnet-level guard.

When not to use it

Don't rely on a NACL alone to protect instances. Don't open 0.0.0.0/0 on admin ports like SSH or RDP; use Systems Manager Session Manager or a bastion with restricted sources.

Common mistakes

  • Forgetting ephemeral-port return rules in a NACL and wondering why connections hang.

  • Expecting a security group to support deny rules.

  • Opening port 22 or 3389 to the world.

  • Misordering NACL rule numbers so a broad allow shadows a specific deny.

  • Hard-coding IPs where a security group reference would follow the instances automatically.

  • Trying to block an attacker's IP with a security group; you need a NACL deny rule (or AWS WAF for web traffic).

  • Writing /0 when you mean a single address; one IPv4 address is /32.

Practice exercises

  1. Easy:

    State two differences between security groups and NACLs.

  2. Medium:

    Write SG rules for a three-tier app (ALB, app, database) where each tier only accepts traffic from the tier before it.

  3. Medium:

    Use the CIDR calculator to find the range for a /27 and write a NACL rule that blocks it.

  4. Hard:

    A client times out on HTTPS although the SG allows 443 and the NACL has an inbound 443 allow. Find the most likely missing rule and explain why.

Interview questions

What does stateful mean for a security group?

Return traffic for an allowed connection is automatically permitted without a separate rule.

Can a security group deny traffic explicitly?

No, it only has allow rules; unmatched traffic is denied by default. Use a NACL for explicit denies.

What are the default rules of a new security group?

All inbound denied, all outbound allowed.

Exam-style: Which acts at the subnet level and is stateless?

Network ACL.

Exam-style: Which acts at the instance level and is stateful?

Security group.

Exam-style: A company must block traffic from one malicious IP address to all instances in a subnet. What should it use?

A network ACL deny rule for that address as a /32.

In what order are NACL rules evaluated?

By rule number, lowest first; the first matching rule decides, and a final implicit rule denies everything else.