Security Groups vs. NACLs Cheat Sheet

Comparison

ComparisonSecurity Groups (SGs)Network Access Control Lists (NACLs)
Scope

Operate at the instance level

Operate at the subnet level (sort of like network-based firewalls)

Maintains state?

Yes - Stateful

Automatically allows outbound traffic if it was allowed inbound and vice versa

No - Stateless

Does not automatically allow outbound traffic if it was allowed inbound or vice versa

Rule evaluation

All security group rules are evaluated and access is permitted if any security group rule permits access

Evaluates rules in order from lowest number to highest until there’s a match (ie: Rule #100 evaluates before #110, and if it matches the request, rule #110 never gets evaluated)

How are they applied?

Multiple can be applied to a single host

Only one NACL can be applied per subnet, but the same NACL can be applied to multiple subnets

Permissions

Can only specify allow rules, not deny rules. Deny is the default.

Can have both inbound and outbound rules, but neither is technically required

Can specify both allow and deny, and there’s a catch-all deny if nothing matches. Has inbound and outbound rules and both are required

Anatomy of rules
  • Name
  • Rule ID
  • IP version (IPv4, IPv6, or blank)
  • Protocol Type (ie: HTTPS, All Traffic, etc)
  • Protocol (ie: TCP)
  • Port range
  • Source or destination (can even reference security groups)
  • Description (optional)
  • Rule number
  • Type (ie: SSH)
  • Protocol (ie: ICMP, TCP, etc)
  • Port range (ie: 22, 1024 - 65536, etc)
  • Source (inbound rules only) - Destination (outbound rules only)
  • Allow/Deny
Examples

Allow requests with specific Protocols/Ports/IPs to reach or leave hosts. Should be very specific and as restrictive as possible.

Allow or Block requests Protocols/Ports/IPs to enter or leave specific subnets. Tends to be broader than SGs.

* It’s important to note that NACLs only control traffic entering or leaving subnets, not intra-subnet traffic

Examples

Security Group Rules - Example access rules for web server instances

Inbound

Protocol typeProtocol numberPortSource IPNotes
TCP680 (HTTP)0.0.0.0/0Allows inbound HTTP access from any IPv4 address
TCP6443 (HTTPS)0.0.0.0/0Allows inbound HTTPS access from any IPv4 address
TCP680 (HTTP)::/0Allows inbound HTTP access from any IPv6 address
TCP6443 (HTTPS)::/0Allows inbound HTTPS access from any IPv6 address

Source: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html#sg-rules-web-server

NACL Rules - Example access rules for subnets hosting web servers

Inbound

Rule #TypeProtocolPort rangeSourceAllow/DenyComments
100HTTPTCP800.0.0.0/0ALLOWAllows inbound HTTP traffic from any IPv4 address.
110HTTPSTCP4430.0.0.0/0ALLOWAllows inbound HTTPS traffic from any IPv4 address.
120SSHTCP22192.0.2.0/24ALLOWAllows inbound SSH traffic from a specific public IPv4 address range
140Custom TCPTCP32768-655350.0.0.0/0ALLOWAllows inbound return IPv4 traffic from the internet (that is, for requests that originate in the subnet).
*All trafficAllAll0.0.0.0/0DENYCatch-all deny rule (can’t be modified or removed)

Outbound

Rule #TypeProtocolPort rangeDestinationAllow/DenyComments
100HTTPTCP800.0.0.0/0ALLOWAllows outbound IPv4 HTTP traffic from the subnet to the internet.
110HTTPSTCP4430.0.0.0/0ALLOWAllows outbound IPv4 HTTPS traffic from the subnet to the internet.
120SSHTCP1024-65535192.0.2.0/24ALLOWAllows outbound SSH traffic from a specific public IPv4 address range
140Custom TCPTCP32768-655350.0.0.0/0ALLOWAllows outbound IPv4 responses to clients on the internet
*All trafficAllAll0.0.0.0/0DENYCatch-all deny rule (can’t be modified or removed)

Source: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html

Security Groups vs. NACLs
Security Groups vs. NACLsInternet → Internet Gateway → Load Balancer in the AWS VPC. Public Subnets: Subnet A and Subnet B each contain a Web Server and its Security Group. Both subnets use NACL A. Private Subnets: Subnet C and Subnet D each contain a Database and its Security Group. Both subnets use NACL B. The Load Balancer connects to both public subnets. Both web-server subnets connect to both database subnets.AWSVPCPublic SubnetsPrivate SubnetsSubnet ASecurity GroupSubnet BSecurity GroupSubnet CSecurity GroupSubnet DSecurity GroupInternetInternet GatewayLoad BalancerNACL AWeb ServerNACL AWeb ServerNACL BDatabaseNACL BDatabase
Security Groups vs. NACLs

Drag the background to pan. Use the controls to zoom or fit the full diagram. Press Escape to close.

Use this cheat sheet with AI

Copy a ready-to-use prompt into your preferred AI assistant.

Preview prompt