Skip to main content
In this lesson we explain how AWS implements firewall controls with Security Groups and Network ACLs (NACLs). You’ll learn firewall fundamentals, the difference between stateless and stateful firewalls, how to configure rules in the AWS Console, and the default behaviors you must consider when protecting VPC workloads.

Firewall fundamentals

A firewall inspects network traffic and permits only the connections defined by a set of rules. Rules are either inbound (ingress) or outbound (egress):
  • Inbound rules control traffic entering a resource (for example, HTTP requests to a web server).
  • Outbound rules control traffic leaving a resource (for example, responses or outgoing API calls).
Two common firewall designs are stateless and stateful. The key distinction is whether the firewall tracks connection state (sessions).

Stateless firewall (example)

  • Web server listens on destination port 443 (HTTPS).
  • A client initiates a TCP connection to destination port 443 from a random ephemeral source port (1024–65535).
  • A stateless firewall requires explicit rules for both directions: allow inbound TCP 443 and allow outbound to ephemeral port range.
  • If the server initiates an outbound request to port 80, you must also allow outbound TCP 80 and inbound to the ephemeral range.
Stateless firewalls do not track connection state, so both request and response directions must be explicitly permitted.

Stateful firewall (example)

  • Web server listens on destination port 443. A client connects.
  • A stateful firewall only needs the inbound rule for 443. It tracks TCP sessions and automatically permits outbound response packets for that connection.
  • For server-initiated outbound requests (e.g., to port 80), only an outbound 80 rule is required; the inbound response is allowed automatically.
Stateful firewalls are session-aware and require rules only for the initiating direction.
An infographic explaining stateful firewalls, showing inbound/outbound traffic rules and allowed IP/port ranges (e.g., ports 443 and 80) with a firewall icon between clients and servers. Arrows show request/response flows and speech bubbles note the firewall recognizing responses and servers listening on ports.
Stateful firewalls track connections, so once a request is permitted the corresponding response is automatically allowed.

Network ACLs (NACLs)

  • NACLs filter traffic at the subnet boundary (entering and leaving a subnet).
  • Each subnet is associated with one NACL. A NACL can be associated with multiple subnets.
  • NACLs are stateless: you must configure both inbound and outbound rules for a full conversation.
  • NACLs do not filter intra-subnet traffic; instances in the same subnet can communicate unless other controls (like security groups) block them.

Security groups

  • Security groups act as virtual firewalls for specific resources (EC2 instances, ENIs, RDS instances, load balancers).
  • Security groups are stateful: responses to allowed requests are automatically permitted.
  • Security groups are attached to resources (not subnets).
  • Security group rules are always allow rules; there are no explicit deny rules. Any traffic not matched by an allow rule is implicitly denied.

Configuring security group rules (Console)

The inbound and outbound rules share the same fields in the AWS Console. Common fields: Selecting a common Type (like HTTP) auto-fills the protocol and port. For Custom TCP, you select the protocol and enter a custom port or range. You can also scope a rule to a single IP (for example, 1.1.1.1/32) or to another security group.
A screenshot of an "Inbound Rules" panel from a cloud console showing security group entries. One rule is highlighted: Custom TCP on port 200 allowed from 1.1.1.1/32 (another rule shows HTTP on port 80 from 0.0.0.0/0).
By default, new security groups include an outbound rule that allows all traffic (All traffic, all protocols and ports) to 0.0.0.0/0. You can modify or remove that rule as needed.
Screenshot of an "Outbound Rules" page for a security group showing a single IPv4 rule that allows all traffic (all protocols and ports) to destination 0.0.0.0/0. A blue arrow highlights the 0.0.0.0/0 destination.
Security groups contain only allow rules. If no allow rules are present, the security group blocks all traffic. Be careful with overly permissive defaults like 0.0.0.0/0 for production resources.

Important security group behaviors

  • If a security group has no rules, it blocks all traffic.
  • Security groups can only allow traffic; there is no explicit deny rule.
  • Multiple security groups can be attached to a resource; their rules are combined (union) to form the effective allow list.

NACL rules and ordering

  • NACLs evaluate numbered rules from lowest to highest. The first matching rule determines Allow or Deny.
  • Unlike security groups, NACL rules can explicitly Allow or Deny traffic.
  • Because NACLs are stateless, you must create both inbound and outbound rules for bidirectional flows.
Screenshot of a "NACL Rules" (network ACL) inbound rules table showing rule numbers (70, 80, 90, *), types (HTTP, SSH, All traffic), protocols/ports, source CIDRs, and Allow/Deny statuses. Some rules are allowed (green) while others are denied (red).

Multiple security groups example

If an instance has two security groups attached—for example, a web group allowing ports 80 and 443, and a mgmt group allowing ports 22 and 3389—the effective ruleset is the union of both groups. The instance will accept traffic permitted by either security group.
A presentation slide titled "Multiple Security Groups" showing an instance with two security groups labeled "web" and "mgmt." A table to the right lists merged rules for ports 80, 443, 3389 and 22 with their corresponding IP ranges.

Default behaviors and AWS-managed exemptions

  • By default, security groups allow all outbound traffic.
  • Every subnet in a VPC must be associated with a network ACL. A single NACL can be associated with multiple subnets, but a subnet can only be associated with one NACL at a time.
Certain AWS-managed traffic bypasses NACL filtering. Examples include Amazon DNS, DHCP, EC2 instance metadata endpoints, ECS/EKS task metadata endpoints, Windows license activation, the Amazon Time Sync Service, and reserved VPC router IP addresses. This traffic is automatically allowed and is not filtered by NACLs.
A slide titled "Network ACLs do not filter traffic destined to and from the following" showing colored boxes that list services such as Amazon DNS, DHCP, EC2 instance metadata, ECS task metadata endpoints, Windows license activation, Amazon Time Sync Service, and reserved VPC router IP addresses.
Remember: Security Groups are stateful and only have allow rules. NACLs are stateless and can explicitly allow or deny traffic; they are evaluated in rule-number order.

Quick comparison

Summary

  • Stateless firewalls require explicit rules for both directions of a session.
  • Stateful firewalls track sessions and automatically allow response traffic to permitted requests.
  • Network ACLs:
    • Filter traffic entering and leaving subnets.
    • Are stateless (need rules for both directions).
    • Can explicitly allow or deny traffic.
  • Security groups:
    • Act as resource-level firewalls (applied to instances, ENIs, etc.).
    • Are stateful (responses to allowed requests are automatically permitted).
    • Only allow traffic (no explicit deny).
    • Multiple security groups attached to a resource have their rules merged to form the effective ruleset.
  • Every subnet in a VPC must be associated with a network ACL; a NACL can be associated with multiple subnets, but a subnet can only be associated with one NACL at a time.
A presentation slide titled "Summary" with a turquoise gradient panel on the left. The right side lists numbered points about security groups and NACLs — their firewall role, statefulness, allow/deny behavior, and rule merging.
A presentation slide titled "Summary" listing two points: every subnet in a VPC must be associated with a network ACL, and a network ACL can be associated with multiple subnets while a subnet can only have one ACL at a time. The slide features a teal gradient panel on the left and numbered decorative icons for points 09 and 10.
However, a subnet can only be associated with one network ACL at a time.

Watch Video