Lesson 2 of 4 Amazon VPC

Filtering Traffic with Security Groups and Network ACLs

This lesson explains the two layers of firewall in a VPC and the differences between them that the exam regularly tests.

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.
  • Describe what VPC Flow Logs, Traffic Mirroring and Reachability Analyzer show.

Side by side

Security groups and network ACLs both filter traffic in a VPC, but they work at different levels and follow different logic. 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 interface endpoints. It is stateful: if a request is allowed in, its response is allowed out automatically. Its rules can only allow traffic, and all rules are evaluated together. A new security group allows all outbound traffic and no inbound traffic. A rule can name another security group as its source, so a database can allow the application tier's group, whatever its instances' addresses.

A network ACL is attached to a subnet and filters all traffic crossing its boundary. It is stateless, so return traffic must be allowed by its own rule, usually on the ephemeral ports 1024 to 65535. Its rules can allow or deny, are evaluated in ascending order of rule number, and the first match wins. This makes network ACLs the tool for blocking a specific address range. The default network ACL allows everything, whereas a new custom ACL denies everything until rules are added. The following table summarises the differences.

Security group
Attached toNetwork interfaces (instances, LBs, RDS, Lambda in VPC, endpoints)
StateStateful: return traffic allowed automatically
RulesAllow only, all evaluated together
DefaultAll outbound allowed, no inbound
ReferencesOther security groups, including peered VPCs in the Region
Network ACL
Attached toSubnets
StateStateless: allow return traffic explicitly, usually ports 1024–65535
RulesAllow and deny, in ascending rule number until a match
DefaultDefault ACL allows all; custom ACLs deny all until rules are added
ReferencesCIDRs

The following diagram shows where each control applies to a request and its response.

Where security groups and network ACLs apply. A network ACL filters all traffic crossing the subnet boundary; it is stateless, so the response needs its own outbound rule on the ephemeral ports, and its numbered rules allow or deny with the first match winning. A security group protects each network interface; it is stateful, so the response is allowed automatically, and its rules can only allow.
The rules are illustrative. A security group rule that names another security group as its source keeps working as instances are replaced.
Sort into groups

Security group or network ACL?

Exam skills 1.2.1 · 1.2.2

Decide which control each statement describes.

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

Using them together

Security groups and network ACLs are used together, with different roles. Security groups are the primary control: they are stateful, attached to each resource, and can reference other security groups, so each tier admits traffic only from the tier in front of it, for example the database tier only from the application tier's security group.

Network ACLs are a coarse, subnet-level backstop. Because they are stateless and support deny rules, they suit broad blocks that security groups cannot express, such as denying a malicious IP range for an entire subnet. Because they are stateless, any NACL rule set must also allow return traffic on ephemeral ports.

Visibility and troubleshooting

Three tools show what happens to network traffic, at different levels of detail. VPC Flow Logs record metadata about each connection through a VPC, subnet or network interface: the source and destination addresses and ports, the protocol, the number of bytes and whether the traffic was accepted or rejected. They are delivered to CloudWatch Logs, S3 or Firehose and do not contain packet contents, so they answer whether traffic reached a resource and which rule type rejected it.

Traffic Mirroring copies the actual packets from a network interface to a monitoring appliance, for intrusion detection or deep troubleshooting that needs payload content. Reachability Analyzer analyses configuration rather than traffic: given a source and a destination, it reports whether a path exists and, if not, which route table, security group or network ACL blocks it, without sending any packets. The following table summarises the tools.

ToolShows
VPC Flow LogsAccepted and rejected traffic per VPC, subnet or ENI (to CloudWatch Logs, S3, Firehose); no packet contents
Traffic MirroringPacket contents
Reachability AnalyzerWhich route table, security group or NACL blocks a path, without sending traffic
Match the pairs

Network visibility tools

Exam skills 1.2.1 · 4.4.5

Match each question with the tool that answers it.

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

Three tiers, least privilege

In a three-tier application, each tier should accept traffic only from the tier in front of it. The following table gives the inbound rule and permitted source for each security group.

Security groupInbound ruleSource
ALBHTTPS 4430.0.0.0/0
Web tierHTTP 8080The ALB security group
DatabasePostgreSQL 5432The web tier security group

Referencing security groups instead of IP ranges means the rules stay correct as instances scale in and out. When the company later needs to block a specific attacker's IP range, it adds a deny rule to the public subnets' network ACL, because security groups can only allow. Responses are allowed automatically by the stateful security groups, but a custom NACL must also allow outbound ephemeral ports, or replies will be dropped.

Check yourself

Which control can block traffic from one specific IP address range?

Show answer

A network ACL, because it supports deny rules. Security groups only allow traffic.

Exam Tips

Remember for the exam

  • To block a particular IP address, use a network ACL deny rule, because security groups cannot deny.
  • To allow the application tier only from the load balancer, reference the ALB's security group.
  • Network ACLs are stateless, so remember outbound rules for ephemeral ports to allow responses.
  • To investigate rejected connections, use VPC Flow Logs.
  • To find out why instance A cannot reach instance B, use VPC Reachability Analyzer.