Architecture fundamentals · 13 min
Security groups and network ACLs in Amazon VPC
In this lesson you will learn how a security group filters traffic and how a network ACL filters it, why one lets the response through on its own and the other doesn't, and how to chain security groups between the tiers of an application.
The traffic of a network is the messages that go back and forth between machines, such as the request your phone makes when you open a page and the response the server sends it. A VPC has two traffic filters, which decide which messages get through and which are blocked: the security group and the network ACL. The security group works on each resource, such as an instance, a load balancer or a database, while the network ACL works on the whole subnet. For example, in an online store, the database's security group protects only the database, and the public subnet's network ACL protects everything in that subnet.
According to AWS, security groups are the primary mechanism for controlling access, and network ACLs are a secondary control. The SAA-C03 guide assesses these two filters by name in task 1.2, along with route tables and NAT gateways. The two filters decide differently which traffic gets through, so the same rule can work in one and fail in the other.
To understand their rules you need one more idea: the port. A port is a number that says which program on the machine a message goes to, like the apartment number inside a building that already has an address. For example, secure web pages arrive at port 443 on the server, and a PostgreSQL database receives on port 5432, so a rule can let 443 through and block everything else.
How does a security group work?
A security group decides which traffic reaches a resource with these operating rules:
- The rules, which can only allow traffic, because a security group has no deny rule. Whatever no rule allows stays out.
- The connection state, which the group remembers because it is a stateful filter, so if it let a request in, its response goes out even if there is no outbound rule, and if the instance starts a connection, the response comes in even if there is no inbound rule.
- The initial configuration, where a new group has no inbound rules and has one outbound rule that allows everything.
- The evaluation, where the group checks all its rules before deciding.
- The source of a rule, which is where the traffic comes from and which can be another security group instead of an IP range, and in that case the rule applies to all the instances that have that group.
For example, if the group of the store's web server has a single rule, "allow 443 from anywhere", a customer who requests the page comes in through 443 and gets their response without there being an outbound rule for it, while someone who tries to get in through another port stays out.
Chained security groups
Using another group as the source lets you chain security groups between the tiers of an application, where each tier accepts traffic only from the group of the previous tier.

In a three-tier chain, the load balancer accepts HTTPS from anywhere, the application accepts its port only from the load balancer's group, and the database accepts the PostgreSQL port only from the application's group. That way, when Auto Scaling launches a new instance with sg-app, the database already accepts it, without you changing any rule and without knowing its IP. For example, on a sale night the store goes from two to six application servers, and the four new ones reach the database from the first minute because they carry sg-app.
A rule based on an IP range doesn't have that advantage. For example, if the database accepts "TCP 5432 from 10.0.16.0/20", which is the application subnet in zone A, the instances that Auto Scaling launches in zone B have a different range and don't reach the database.
Which rule do you put on the database?
The application instances are in an Auto Scaling group across three zones, with the security group sg-app. The database must only accept connections from those instances. Which inbound rule do you add to its security group?
How does a network ACL work?
A network ACL decides which traffic enters or leaves a subnet with different operating rules:
- The assignment, where each subnet has exactly one network ACL, which is the VPC's default one if you don't assign another, and where the same ACL can serve several subnets.
- The default ACL, which allows all traffic.
- The rules, which can allow or deny and which use IP ranges (CIDR), with no option to point to a security group.
- The order, where each rule has a number from 1 to 32766 and the rules are checked from lowest to highest, until the first one that matches decides and the rest are no longer checked.
- The final asterisk rule, which denies everything that didn't match any other rule and which can't be deleted.
- The connection state, which the ACL doesn't remember because it is a stateless filter, so the response needs its own rule.
- The moment of evaluation, which is when traffic enters or leaves the subnet, so traffic between instances in the same subnet doesn't go through it.
For example, if the store wants nobody from a certain address range to reach any machine in its public subnet, it adds a single deny rule to that subnet's network ACL, instead of touching the security group of each resource, which couldn't deny anyway.
The number of each rule
Since the first rule that matches decides, a deny rule has to have a lower number than the allow rule that covers the same traffic. For example, if rule 100 allows TCP 443 from anywhere and rule 110 denies an attacking range, that range gets in through 443, because rule 100 decided before reaching rule 110.
cloud path tip: number the rules in increments of 10 starting at 10, or of 100 starting at 100, as AWS suggests, so that later you can insert a rule between two others.
What happens to the response to a request?
The difference in state between the two filters shows up in the response. Each connection uses two ports: the server's, such as 443, and one on the client, which is where the client waits for the response. If you open port 443 inbound and outbound in the security group and in the network ACL, the site may not load, because the server's response goes to a high port on the client.

For example, if the client opens the connection from its port 50123 to the server's port 443, on the way in the request passes both filters. On the way back, the security group remembers the connection and lets the response out. The network ACL, on the other hand, remembers nothing and sees new traffic going to the client's port 50123, so if its outbound rule doesn't allow it, the response stays inside.
Ephemeral ports
That high port is called an ephemeral port, because it only lasts as long as the connection, and the client's operating system chooses it. According to the AWS documentation, each system uses a different range:
- Many Linux kernels, which use 32768 to 61000.
- Windows Server 2008 and later versions, which use 49152 to 65535.
- Elastic Load Balancing, NAT gateways and AWS Lambda, which use 1024 to 65535.
That is why an outbound network ACL rule that opens 1024 to 65535 covers all three cases. For example, if one of the store's customers opens the page from a Linux computer and another from a Windows one, that single rule lets the response return to both.
Security group or network ACL?
0 of 6Tap the correct column for each characteristic.
It applies to a whole subnet
The response to an allowed request gets through on its own
It can deny a specific IP range
A rule can point to another group as the source
It checks its rules in order and stops at the first one that matches
A new resource with no inbound rules receives nothing
Traffic that neither one filters
The two filters share one exception: neither security groups nor network ACLs filter traffic to the Amazon DNS server, Amazon DHCP or EC2 instance metadata. For example, even if the store blocks all outbound traffic from a server in its network ACL, that server can still ask the Amazon DNS server for the address of a name.
Words in the question that point to each filter
On the exam, each filter is recognized by the words in the question:
- "Block" or "deny" an IP or a range, which points to a network ACL, just like "stateless".
- "Allow only from the load balancer" or "only from the application instances", which points to a security group that uses another group as the source.
- A site that doesn't load even though the security group and the ACL allow 443 inbound and outbound, where the ephemeral ports are missing from the ACL's outbound rules.
For example, in "the store is receiving attacks from an address range and wants to block it", the word "block" points to a network ACL, because a security group can't deny.
Security groups and network ACLs are part of Amazon VPC, which is in the new experience free plan, so they work the same way in both AWS accounts and you can practice with them in either one.
Review question
A website receives malicious traffic from the range 198.51.100.0/24. The team wants to block that range for all the instances in the public subnet without affecting the rest of the users. What does it do?
Next
- How Amazon VPC works, the AWS guide, from which you can continue to the pages on security groups and network ACLs.
- Take a practice exam at /examenes and review your results in the secure architectures domain.