Lesson 1 of 4 VPC Network Security

Filtering VPC Traffic with Security Groups and Network ACLs

This lesson explains how security groups and network ACLs, the two traffic filters built into every VPC, differ, which is essential for design and troubleshooting questions.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish stateful security groups from stateless network ACLs.
  • Explain how security groups and network ACLs work together.
  • Explain how VPC Flow Logs and Reachability Analyzer reveal blocked paths.
  • Write tier-to-tier rules that reference security groups instead of IP addresses.

Side by side

Security groups and network ACLs both filter traffic in a VPC, but work differently. A security group is attached to network interfaces, so it protects individual resources such as instances, load balancers, RDS databases, Lambda functions in a VPC and endpoints. It is stateful: when a request is allowed, the response is allowed automatically. It has only allow rules, so anything not allowed is denied, and its rules can reference other security groups, for example allowing the database group to accept traffic only from the application group.

A network ACL is attached to a subnet and filters all traffic entering or leaving it. It is stateless, so return traffic must be allowed explicitly, usually on the ephemeral ports 1024–65535 that clients use for responses. It has both allow and deny rules, evaluated in number order with the first match winning, which makes it the tool for blocking a specific IP range. Its rules use CIDR ranges only. The default network ACL allows all traffic, while a new custom one denies all until rules are added. The following table summarises the differences.

Security group
Attached toNetwork interfaces (instances, LBs, RDS, Lambda in VPC, endpoints)
StateStateful: responses allowed automatically
RulesAllow only
ReferencesOther security groups and prefix lists
Default—
Network ACL
Attached toSubnets
StateStateless: return traffic needs explicit rules, usually ephemeral ports 1024–65535
RulesAllow and deny, numbered, lowest first, first match wins
ReferencesCIDRs only
DefaultDefault NACL allows all; a new custom NACL denies all
Sort into groups

Security group or network ACL?

Exam skills 3.3.1

Decide which filter each statement describes.

Log in to try this activity Interactive activities are free with an account.

Using them together

Security groups and network ACLs complement each other. The following list describes how they are used together.

  • Security groups give fine-grained workload rules, e.g. the database allows 5432 only from the app tier's group.
  • NACLs give coarse subnet guardrails and explicit denies, e.g. blocking a malicious IP range.
  • During containment, security group changes may not break existing tracked connections; NACL changes apply to all traffic immediately.

The following diagram shows a chain of security groups in a three-tier application that reference each other instead of CIDR ranges, and the second layer of defence.

Security groups that reference each other instead of CIDR ranges. The web tier accepts 8080 only from the load balancer's group, the application tier 9000 only from the web group, and the database 5432 only from the application group, so a compromised web server cannot reach the database directly.
The scenario is illustrative.
Scenario questions

Choosing the right filter

Exam skills 3.3.1 · 2.2.4

Choose security groups or network ACLs for each requirement.

Log in to try this activity Interactive activities are free with an account.

Troubleshooting

VPC Flow Logs show REJECT records; Reachability Analyzer names the security group, NACL or route blocking a path.

True or false

Troubleshooting blocked traffic

Exam skills 3.3.1 · 1.3.2

Check the tools for diagnosing blocked connections.

Log in to try this activity Interactive activities are free with an account.
Worked example

Tier-to-tier rules without IP addresses

A three-tier application must allow only the load balancer to reach the web tier, only the web tier to reach the application tier, and only the application tier to reach the database. Referencing security groups instead of CIDR ranges keeps the rules correct as instances scale and change IP:

aws ec2 authorize-security-group-ingress --group-id $SG_WEB \
  --ip-permissions "IpProtocol=tcp,FromPort=8080,ToPort=8080,UserIdGroupPairs=[{GroupId=$SG_ALB}]"
aws ec2 authorize-security-group-ingress --group-id $SG_APP \
  --ip-permissions "IpProtocol=tcp,FromPort=9000,ToPort=9000,UserIdGroupPairs=[{GroupId=$SG_WEB}]"
aws ec2 authorize-security-group-ingress --group-id $SG_DB \
  --ip-permissions "IpProtocol=tcp,FromPort=5432,ToPort=5432,UserIdGroupPairs=[{GroupId=$SG_APP}]"

A compromised web server still cannot reach the database directly, because it is not in SG_APP. The subnets' network ACLs add a coarse second layer, for example denying all traffic from the internet to the database subnets, and are where an emergency block on a malicious address range goes, because security groups cannot deny.

Check yourself

Inbound traffic is allowed, but the responses are dropped. What is the likely cause?

Show answer

The network ACL has no outbound rule for the ephemeral ports used by the responses, because network ACLs are stateless.

Exam Tips

Remember for the exam

  • To block one IP address or range, use a network ACL deny rule, because security groups cannot deny.
  • To allow only the web tier to reach the application tier, use a security group rule that references the web tier's group.
  • If inbound traffic is allowed but responses are dropped, the NACL is missing an outbound rule for ephemeral ports.
  • Security groups are stateful; network ACLs are stateless.
  • Network ACL rules are evaluated from the lowest number first.