> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Security Groups and NACLs

> Explains AWS Security Groups versus Network ACLs, covering stateful versus stateless firewall behavior, rule processing, scope, configuration differences, and practical VPC usage.

This article explains how AWS Security Groups and Network Access Control Lists (NACLs) act as firewalls inside a VPC. First we cover general firewall concepts — inbound vs outbound rules, and the difference between stateless and stateful behavior — then map those concepts to NACLs and security groups in AWS.

## Firewall fundamentals

A firewall inspects network traffic and permits only traffic that matches a set of rules. Rules are categorized as:

* Inbound (ingress): controls traffic entering a resource.
* Outbound (egress): controls traffic leaving a resource.

Two common firewall behaviors:

* Stateless: evaluates each packet independently; the firewall does not track connection state.
* Stateful: tracks connection state (for example, TCP sessions) and implicitly allows return traffic for an established connection.

Example scenario — HTTPS web server:

* Client sends a request to the server with destination port 443.
* Server replies from source port 443 to the client's ephemeral source port (typically in the range 1024–65535).

Stateless firewall behavior:

* You must explicitly allow the inbound request (destination port 443) and also allow the outbound response (destination ephemeral port range).
* If the server initiates an outbound connection (for example, to an update server on port 80), you need rules allowing outbound port 80 and inbound ephemeral ports for the response.
* Key point: the firewall does not infer that a response should be allowed just because the request was allowed.

Stateful firewall behavior:

* Allowing inbound HTTPS (port 443) causes the firewall to track the TCP session and automatically permit the outbound response to the client’s ephemeral port.
* If the server initiates an outbound connection on port 80, only an outbound rule for port 80 is required; the corresponding inbound response is implicitly allowed.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/stateful-firewall-https-443-ephemeral-ports.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=9c873f11294932b60cb56a3796366bb2" alt="A colorful infographic explaining stateful firewalls, showing inbound port 443 allowed and outbound ephemeral ports 1024–65535 allowed, with arrows through a firewall icon to a server. Speech bubbles note the firewall recognizes responses and the server is listening on port 443." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/stateful-firewall-https-443-ephemeral-ports.jpg" />
</Frame>

## How this maps to AWS

AWS exposes two complementary firewall mechanisms within a VPC:

* Network Access Control Lists (NACLs): operate at the subnet level and are stateless.
* Security Groups: act at the resource level (ENIs, EC2, RDS, LBs) and are stateful.

Below we describe each service and practical configuration notes.

### Network Access Control Lists (NACLs)

Key characteristics:

* NACLs filter traffic entering and leaving a subnet in a VPC.
* Each subnet is associated with exactly one network ACL at a time, but a NACL can be associated with multiple subnets.
* NACLs are stateless — you must explicitly configure both inbound and outbound rules for each flow.
* NACLs do not evaluate traffic between instances within the same subnet.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/vpc-nacls-stateless-public-private-subnets.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=c9901167acd5a491aeada5739d7f8f27" alt="An infographic showing Network Access Control Lists (NACLs) in a Virtual Private Cloud with public and private subnets. It illustrates inbound/outbound arrows and notes that NACLs are stateless and filter traffic entering and leaving a subnet but not traffic within a subnet." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/vpc-nacls-stateless-public-private-subnets.jpg" />
</Frame>

NACL rules are processed in ascending numeric order; the first matching rule is applied. Unlike security groups, NACL rules can explicitly `Allow` or `Deny` traffic.

### Security Groups

Key characteristics:

* Security groups act as virtual firewalls for individual resources (EC2 instances, ENIs, load balancers, RDS instances, etc.).
* Security groups are stateful: return traffic for an allowed request is automatically permitted.
* Security group rules are allow-only — there is no explicit deny rule. All traffic not explicitly allowed is implicitly denied.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-groups-vpc-stateful-firewall-resources.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=aa22e987cf06a7c995b5baecd0fcc97e" alt="A slide titled &#x22;Security Groups&#x22; showing a VPC diagram with public and private subnets where security groups (firewall icons) control access to resources (chip icons) in the 172.16.0.0/24 network. The text explains security groups act as firewalls for individual resources (EC2, LB, RDS) and are stateful so only the request needs to be allowed." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-groups-vpc-stateful-firewall-resources.jpg" />
</Frame>

## Configuring Security Group rules (AWS Console)

Security groups show separate Inbound and Outbound sections; the rule fields look identical for both directions. Common rule fields:

* Name / description (optional) — human-friendly label.
* Rule ID (internal) — unique identifier.
* IP version — IPv4 or IPv6.
* Type / Protocol / Port range — e.g., HTTP → TCP port 80, HTTPS → TCP port 443, or a custom rule.
* Source (inbound) or Destination (outbound) — a CIDR range like `0.0.0.0/0` or a single IP like `1.1.1.1/32`.

Example: inbound rules that allow HTTP (TCP port 80) from everywhere and a custom TCP port 200 from a specific IP.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-group-inbound-ports-80-200.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=c6a173abb1acbd8fb346e913c3c7caca" alt="Screenshot of an &#x22;Inbound Rules&#x22; panel (likely an AWS security group) showing two entries. One rule allows HTTP (TCP port 80) from 0.0.0.0/0 and a highlighted custom TCP rule allows port 200 from 1.1.1.1/32." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-group-inbound-ports-80-200.jpg" />
</Frame>

By default, a new security group includes an outbound rule that allows all outbound traffic (`0.0.0.0/0`). You can remove or tighten that rule depending on your security posture.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/outbound-rule-allow-all-ipv4.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=feb4a81a61155172a9ee1166f863d259" alt="A screenshot of an &#x22;Outbound Rules&#x22; page in a cloud console showing a single security group outbound rule. The rule allows all IPv4 traffic (all protocols/ports) to destination 0.0.0.0/0, with a blue arrow pointing at the destination." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/outbound-rule-allow-all-ipv4.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Security groups use allow-only rules; unspecified traffic is implicitly denied. Newly created security groups include a default outbound rule that allows all outbound traffic — remove or restrict it if you need tighter egress controls.
</Callout>

## NACL rule differences

* Rules are numbered and processed in ascending order; the first matching rule applies.
* NACLs can explicitly `Allow` or `Deny` traffic.
* Being stateless, NACLs require explicit, matching entries for both directions of a connection.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/nacl-inbound-rules-ports-80-22.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=4ea8df829ac91842df01ac15b0eb7351" alt="A screenshot of a &#x22;NACL Rules&#x22; inbound rules table showing rule numbers, types (HTTP, SSH, All traffic), protocols, port ranges, source CIDR blocks and Allow/Deny status. The list includes rules for ports 80 and 22 with various sources and allow/deny icons." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/nacl-inbound-rules-ports-80-22.jpg" />
</Frame>

## Multiple security groups and effective ruleset

* You can attach multiple security groups to a resource (for example, an EC2 instance).
* The effective ruleset is the union of all rules from attached security groups. If any attached group permits a flow, it is allowed (subject to NACLs and routing).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/multiple-security-groups-instance-web-mgmt.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=6c8476574c5e3b43c6fa9063183f3d5e" alt="A presentation slide titled &#x22;Multiple Security Groups&#x22; explaining that you can assign multiple security groups to a single instance. It shows an instance with &#x22;web&#x22; and &#x22;mgmt&#x22; groups and a table of merged port/IP rules (e.g., ports 80, 443, 3389, 22 with their IP ranges)." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/multiple-security-groups-instance-web-mgmt.jpg" />
</Frame>

## Default behaviors, associations, and exceptions

* By default, security groups allow all outbound traffic until you change the outbound rules.
* Every subnet in a VPC must be associated with a network ACL.
* A network ACL can be associated with multiple subnets, but each subnet can only be associated with one NACL at a time.

NACLs do not filter certain AWS-managed traffic that is required for core VPC functionality. Examples include:

* Amazon DNS
* DHCP traffic
* EC2 instance metadata and ECS task metadata endpoints
* Windows license activation
* Amazon Time Sync Service
* Reserved VPC router IP addresses

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/network-acls-exempt-services.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=3ebd8c272117d505df6284bb0950f4ba" alt="A presentation slide titled &#x22;Network ACLs do not filter traffic destined to and from the following:&#x22; showing seven colorful rounded boxes listing services: Amazon DNS, DHCP, EC2 instance metadata, ECS task metadata endpoints, Windows license activation, Amazon Time Sync Service, and reserved VPC router IP addresses." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/network-acls-exempt-services.jpg" />
</Frame>

<Callout icon="warning" color="#FF6B6B">
  Because NACLs are stateless and can explicitly deny traffic, be careful when adding deny rules: rule order matters. If you add a deny that blocks legitimate return traffic, you must also add matching allow rules for the reverse flow.
</Callout>

## Quick comparison tables

| Feature | Network ACL (NACL) | Security Group |
| - | - | - |
| Scope | Subnet-level | Resource-level (ENI, EC2, LB, RDS) |
| Stateful? | No (stateless) | Yes (stateful) |
| Rule action | Allow or Deny | Allow only (implicit deny) |
| Rule processing | Numbered, earliest match applies | All rules evaluated; effective is union of allows |
| Default behavior | Custom NACLs may be restrictive | Default outbound allow for new SGs |
| Use case | Network-level filtering, cross-subnet policies | Host/resource-level security controls |

| Concept | Practical implication |
| - | - |
| Stateless | Must configure matching ingress + egress rules for return traffic |
| Stateful | Allowing one side of the flow permits return traffic automatically |
| Multiple SGs | The union of attached SG rules becomes the effective allowlist |

## Summary

* Stateless firewalls (like NACLs) evaluate each packet independently and require explicit ingress and egress rules for a complete flow.
* Stateful firewalls (like Security Groups) track connections and automatically allow return traffic for established sessions.
* NACLs filter traffic at the subnet boundary, are stateless, and can explicitly allow or deny traffic (rule-number order matters).
* Security Groups are stateful, attached to resources, and only contain allow rules; multiple security groups combine their rules into an effective allowlist.
* Every subnet must be associated with a network ACL; a NACL can cover multiple subnets, but a subnet can only have one NACL association.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/firewall-summary-stateless-stateful-acls.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=11323a22bff346e74ebeb1a2ff3c1391" alt="A presentation slide titled &#x22;Summary&#x22; with a turquoise left panel and four colored numbered bullets on the right. The bullets summarize firewall concepts: stateless firewalls need explicit inbound/outbound rules, stateful firewalls track requests to allow responses, and network ACLs filter subnet traffic (they are stateless)." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/firewall-summary-stateless-stateful-acls.jpg" />
</Frame>

* Security groups act as stateful firewalls for resources, only allow traffic (no deny), and multiple security groups' rules are merged to form the effective allowlist.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-groups-stateful-summary.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=e073b2d0520801fdecfb67e2fe77c713" alt="A presentation slide titled &#x22;Summary&#x22; with a teal gradient panel on the left and numbered colorful icons down the right. The right side lists points about security groups — they act as stateful firewalls for resources, only &#x22;allow&#x22; traffic (NACLs can &#x22;allow&#x22; or &#x22;deny&#x22;), and multiple security groups' rules are merged." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/security-groups-stateful-summary.jpg" />
</Frame>

* Remember: every subnet in a VPC must be associated with a network ACL; an ACL can be used by multiple subnets, but a subnet may be associated with only one ACL at a time.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4aIgI-Z1-7mA6vcc/images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/vpc-network-acl-subnet-association-summary.jpg?fit=max&auto=format&n=4aIgI-Z1-7mA6vcc&q=85&s=d02d2b259dcf33119c5c8ab4f8cead4c" alt="A presentation slide with a turquoise left panel labeled &#x22;Summary&#x22; and two numbered points on the right stating VPC network ACL rules: every subnet must be associated with a network ACL, and while an ACL can cover multiple subnets, a subnet can be associated with only one ACL at a time." width="1920" height="1080" data-path="images/AWS-Solutions-Architect-Associate-Certification/Services-Networking/Security-Groups-and-NACLs/vpc-network-acl-subnet-association-summary.jpg" />
</Frame>

## Links and references

* AWS Security Groups — [https://docs.aws.amazon.com/vpc/latest/userguide/VPC\_SecurityGroups.html](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html)
* AWS Network ACLs — [https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html)
* TCP/IP ephemeral port ranges — (varies by OS; commonly 1024–65535)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/aws-solutions-architect-associate-certification/module/e03ffb87-3345-4fbb-9576-cb53d21d7a6a/lesson/edde951d-3e42-43fd-b080-204b2dfda4a6" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.