Volume 2 · Chapter 3 — AWS Networking Deep Dive (VPC)

Security Groups and Network ACLs

Goutham Mayaluri 11 min read AWSCloudVPCSecurity GroupNetworkingSecurity

3.1 Introduction

Routing decides where trafficcango; security groups and network ACLs decide what traffic isallowed. They are the two firewall layers inside a VPC, and interviewers love asking about the difference because it tests whether you understand stateful versus stateless filtering and instance-level versus subnet-level control. Getting them right is also a real security responsibility - it’s your half of the shared responsibility model - and misconfigurations here are a leading cause of both outages (traffic wrongly blocked) and breaches (traffic wrongly allowed). This chapter draws the distinction precisely.

3.2 Security Groups

A security group acts as a virtual firewall at the level of an individual resource - typically an EC2 instance or a database - controlling the traffic allowed in and out of that resource. Security groups have two defining properties. First, they are stateful: if you allow an inbound request, the response is automatically allowed back out, and vice versa, without needing a matching rule for the return traffic - you think only in terms of the connections you want to permit. Second, they are allow-only: you specify what is permitted, and everything not explicitly allowed is denied; there are no deny rules. A powerful feature is that security group rules can reference other security groups rather than IP ranges - for example “allow the database security group to accept traffic from the application security group” - so the rule stays correct even as instances come and go, which is ideal for dynamic, auto-scaled environments.

3.3 Network ACLs

A network ACL (NACL) is a firewall at the subnet level, controlling traffic entering and leaving an entire subnet. NACLs differ from security groups in two mirror-image ways. First, they are stateless: they do not remember connections, so if you allow inbound traffic you must also explicitly allow the corresponding outbound return traffic, and vice versa - both directions must be permitted for a connection to work. Second, they support both allow and deny rules, evaluated in numbered order, which makes them useful for explicitly blocking specific IP ranges - something security groups cannot do. NACLs are applied to the subnet, so they affect every resource in it uniformly, acting as a coarse, broad-brush layer, whereas security groups provide fine-grained, per-resource control.

flowchart TB
  Traffic[Incoming traffic] --> NACL{Network ACL - subnet\nstateless, allow+deny, ordered}
  NACL -->|permitted| SG{Security Group - resource\nstateful, allow-only}
  SG -->|permitted| R[EC2 / Database]
  NACL -->|denied| DropN[Blocked at subnet edge]
  SG -->|not allowed| DropS[Blocked at resource]

3.4 How They Work Together (Defense in Depth)

The two layers combine as defense in depth. Traffic entering a subnet is first evaluated by the subnet’s NACL; if permitted, it then reaches the resource, where the security group evaluates it again. Both must allow the traffic for it to succeed. In most designs, security groups do the real work of precise access control - allow the load balancer to reach the app, allow the app to reach the database on the database port - because they’re stateful and can reference other groups, making them easier to manage correctly. NACLs serve as a secondary, coarse guardrail, often left permissive but available to explicitly block known bad IP ranges at the subnet boundary. The senior framing is: security groups are your primary, fine-grained, stateful control; NACLs are a supplementary, stateless, subnet-wide layer that adds the ability to deny.

3.5 Least-Privilege Rules

The guiding principle for both is least privilege: open only the specific ports and sources genuinely required, and nothing more. A database security group should accept traffic only on the database port and only from the application’s security group - not from the whole VPC, and certainly not from the internet. A load balancer’s security group accepts the public web ports from the internet; the application’s accepts traffic only from the load balancer’s group; the database’s accepts traffic only from the application’s group. This chain, expressed by referencing security groups rather than IP ranges, means each tier can talk only to its immediate neighbours, so a compromise of one tier can’t freely reach the others - the network equivalent of least privilege.


# Least-privilege chain: DB accepts PostgreSQL only from the app's security group.
aws ec2 authorize-security-group-ingress \
  --group-id sg-database \
  --protocol tcp --port 5432 \
  --source-group sg-application    # reference the group, not an IP range

3.6 Banking Production Scenario

A bank layers its firewalls in a strict chain. The load-balancer security group allows HTTPS from the internet. The application security group allows traffic only from the load-balancer group on the app port - so nothing but the load balancer can reach the app. The database security group allows the PostgreSQL port only from the application group - so only the app can reach the database, never the internet or other tiers. NACLs on the subnets are kept largely permissive but are used to explicitly deny a set of known malicious IP ranges at the subnet edge as an extra guardrail. Because the rules reference security groups rather than fixed IPs, they remain correct as Auto Scaling adds and removes instances. The result is a tiered, least-privilege network where each layer can talk only to its neighbour.

3.7 Troubleshooting: Blocked or Over-Open Traffic

When traffic is unexpectedly blocked, the usual culprits are a security group missing an allow rule, or - classically - a NACL that allows inbound but not the outbound return traffic (because NACLs are stateless). The tell for the NACL case is that a connection half-works or times out; the fix is to allow the return traffic explicitly, remembering the ephemeral port range. When traffic is unexpectedly allowed - a security review finds a resource reachable that shouldn’t be - the cause is almost always an over-broad rule, like a security group allowing a port from0.0.0.0/0(the whole internet) or from the entire VPC instead of a specific source group. The fix is to tighten to least privilege: specific ports, specific source security groups. The diagnostic order is to check the security group first (stateful, primary), then the NACL (stateless, remember both directions), then routing.

3.8 Security Group vs NACL: The Comparison Table

This distinction is one of the most heavily-tested in the exam, so it’s worth having as a crisp side-by-side. The two operate at different levels and filter differently, and they work together as defense in depth.

Aspect Security Group Network ACL
Level Resource / ENI (instance) Subnet
State Stateful (return traffic auto-allowed) Stateless (must allow return traffic explicitly)
Rules Allow only (implicit deny) Allow and Deny
Rule evaluation All rules evaluated together Numbered order, first match wins
References Can reference other security groups CIDR ranges only
Default Denies all inbound, allows all outbound Default NACL allows all; custom NACL denies all until you add rules
Typical use Primary fine-grained control (tier chains) Coarse subnet guardrail; explicitly deny bad IPs

The exam traps to remember: a half-working/timed-out connection is often a stateless NACL missing the return rule (ephemeral ports 1024–65535); security groups can’t have deny rules (use a NACL to block a specific IP); and a newly-createdcustomNACL denies everything until you add rules, whereas the default NACL allows everything.

Interview Question

What is the difference between a security group and a network ACL?

Security groups and network ACLs are the two firewall layers in a VPC, and they differ along two key axes: the level they operate at, and how they filter. A security group is a virtual firewall at the level of an individual resource - an EC2 instance or a database - controlling the inbound and outbound traffic for that specific resource. A network ACL is a firewall at the subnet level, controlling traffic entering and leaving an entire subnet, so it applies uniformly to every resource in that subnet. The more important distinction is behavioural. Security groups are stateful: if you allow an inbound connection, the return traffic is automatically allowed back out without a separate rule - you reason only about the connections you want to permit. Network ACLs are stateless: they don’t track connections, so if you allow traffic in one direction you must explicitly allow the corresponding return traffic in the other direction, or the connection fails. The second behavioural difference is that security groups are allow-only - you specify what’s permitted and everything else is implicitly denied, with no deny rules - whereas network ACLs support both allow and deny rules, evaluated in numbered order, which lets them explicitly block specific IP ranges, something a security group cannot do. There’s also a very practical feature of security groups: their rules can reference other security groups instead of IP ranges - for example, “allow the database group to accept traffic from the application group” - so the rule stays correct even as instances are added and removed by Auto Scaling, which is ideal for dynamic environments. In practice, they work together as defense in depth: traffic hits the subnet’s NACL first, then the resource’s security group, and both must allow it. Most access control is done with security groups because they’re stateful and reference-based and therefore easier to manage precisely, while NACLs act as a coarser subnet-wide guardrail, often kept permissive but used to deny known-bad IP ranges at the subnet edge. So the summary is: security group = resource-level, stateful, allow-only, can reference other groups; network ACL = subnet-level, stateless, allow and deny, ordered rules - and you use security groups for fine-grained primary control and NACLs as a supplementary broad layer.

Interview Question

How would you design network security rules for a three-tier application following least privilege?

I’d build a strict chain of security groups so each tier can talk only to its immediate neighbour, using group references rather than IP ranges, and I’d keep NACLs as a supplementary subnet-level guardrail. The three tiers are the load balancer (internet-facing), the application servers, and the database. For the load balancer’s security group, I allow inbound only the public web ports - typically HTTPS - from the internet, because that’s the sole legitimate public entry point. For the application security group, I allow inbound only from the load balancer’s security group, on the application’s port - so nothing except the load balancer can reach the application, not the internet and not other resources. For the database security group, I allow inbound only the database port - say PostgreSQL’s 5432 - and only from the application’s security group, so only the application tier can reach the database, never the internet and never any other tier. Critically, I express these as references to the other security groups rather than fixed IP addresses, which means the rules remain correct as Auto Scaling adds and removes instances - the “application group” always denotes whatever app instances currently exist. This creates least privilege at the network layer: each tier’s exposure is limited to exactly the neighbour that must talk to it, so a compromise of the web tier can’t freely reach the database, and the database has no path to or from the internet at all. I reinforce this with subnet placement - load balancer in public subnets, application and database in private subnets - so the sensitive tiers have no internet route regardless of security group rules. Then I use NACLs on the subnets as a coarse secondary layer, generally permissive but available to explicitly deny known malicious IP ranges at the subnet boundary, remembering that because NACLs are stateless I must allow return traffic in both directions if I tighten them. The principle throughout is to open only the specific ports from the specific sources genuinely required and nothing more - no0.0.0.0/0except where the internet legitimately must reach the load balancer, and no whole-VPC allow rules. So the design is a per-tier security group chain (internet → LB → app → database), group-referenced so it survives scaling, backed by private subnet placement and NACL guardrails - which together give a tiered, least-privilege network where each layer reaches only its neighbour.

Chapter Revision Notes

  • Security group = resource-level firewall, stateful (return traffic auto-allowed), allow-only, can reference other groups.
  • Network ACL = subnet-level firewall, stateless (must allow both directions), supports allow and deny rules in order.
  • They combine as defense in depth: NACL at the subnet, then security group at the resource - both must permit traffic.
  • Use security groups for primary fine-grained control; NACLs as a coarse guardrail that can deny known-bad IP ranges.
  • Apply least privilege with a tier chain (internet → LB → app → DB) using group references so rules survive Auto Scaling.