Firewalls and Security Groups Explained Simply
You start a new cloud server, run curl against it from your laptop, and the command hangs before giving up, although the app is running. Most often, a firewall is quietly throwing your packets away. This lesson explains firewalls and security groups: what a rule says, why replies get back in, how cloud security groups differ from network ACLs, and what an error tells you.
What a firewall rule says
In lesson 2, a firewall was a filter that blocks unwanted traffic. More exactly, it is software that checks each packet against a list of rules and lets it through or discards it. It runs on laptops, home routers, company networks and in the cloud.
Each rule names:
- Action: allow or deny.
- Direction: inbound (arriving) or outbound (leaving).
- Protocol: TCP or UDP (lesson 5), or ICMP (lesson 12), which has no ports.
- Port: one, such as 443, or a range.
- Address range in CIDR notation (lesson 11): the source for inbound rules, the destination for outbound ones.
Traffic that no rule matches gets the firewall's default. A safe firewall denies inbound traffic by default, as the guard turns away anyone unlisted, and often allows all outbound. These are an illustrative web server's inbound rules; 0.0.0.0/0, the default route's range in lesson 12, means any IPv4 address.
| Action | Protocol | Port | Source | Why |
|---|---|---|---|---|
| Allow | TCP | 443 | 0.0.0.0/0 | HTTPS from anyone |
| Allow | TCP | 22 | 203.0.113.0/24 | SSH from the office |
| Deny | Any | Any | Anything else | The default |
When rules overlap, many firewalls check them in order and stop at the first match, so order matters. Cloud security groups hold only allow rules and let a packet in if any rule allows it.
Stateful firewalls let replies back in
When your laptop fetches a page, the request leaves from a temporary port, such as 50000, and the reply returns from port 443 to port 50000. If the laptop's firewall denies all inbound traffic, how does the reply get in?
A stateful firewall remembers connections. When an allowed packet starts one, the firewall records the protocol, both addresses and both ports in a connection table, much like lesson 12's NAT table. Later packets matching an entry, in either direction, pass without a rule check until the connection closes or goes idle: the guard waving a resident back in.
A stateless firewall remembers nothing and checks every packet against its rules, so letting replies in means allowing every temporary port they might return to, which opens far more than the replies need.
In the flowchart, notice that the rule list is consulted only for packets that start something new.
Security groups and network ACLs
In the cloud, your servers live in a VPC (virtual private cloud), a private network divided into subnets, as What Is a VPC? in Cloud Fundamentals explains. Each server is an instance, a virtual machine. AWS gives you two firewalls; the names and details here are AWS's.
A security group is a stateful firewall attached to an instance, strictly to its network interface, its virtual network card. It has allow rules only, and an instance's groups add up. A new group allows no inbound and all outbound traffic. A rule's source can be an address range or another security group, meaning any instance in that group.
A network ACL (access control list) is a stateless firewall on a subnet, checking every packet that enters or leaves. Its numbered rules can allow or deny and are checked from the lowest number up; the first match decides. The default ACL allows everything.
Being stateless, an ACL needs rules for replies: a web server's subnet must allow outbound traffic to visitors' temporary ports, a wide range such as 1024–65535, because operating systems pick them from different ranges.
In the analogy, a security group is a guard at each flat's door who remembers who went out; a network ACL is a checkpoint at the block gate with a printed list and no memory.
Other clouds differ: Azure's network security groups and Google Cloud's VPC firewall rules are stateful but also accept deny rules, ordered by priority.
Least privilege: open only what you need
Lesson 9's least privilege, giving each user only the access their job needs, applies to ports too: open only the ports a service uses, to only the sources that need them.
So narrow the source: allow SSH on port 22 from your office range, such as 203.0.113.0/24, or one address as a /32, never from 0.0.0.0/0. And keep internal ports internal: a database port such as 5432 never faces the internet. On AWS, 0.0.0.0/0 covers IPv4 only; IPv6's "anywhere", ::/0, needs its own rule.
The diagram chains three tiers, with illustrative group names. Only the load balancer, a server that spreads requests across the app servers (lesson 14), faces the internet; each later tier accepts traffic only from the group in front of it.
The database rule names app-sg, not the app subnet's range: a range would also admit anything placed in that subnet later, while the group admits only the app servers, however many there are.
Timeout or refused: what the error tells you
Lesson 2 showed that "connection refused" means the machine answered but nothing listens on that port, while a timeout means nothing answered. Firewalls cause most of those silences.
A firewall can drop a packet, discarding it silently, or reject it, sending back a refusal. Security groups and network ACLs drop, so a blocked connection looks like a timeout: your side resends its opening SYN packet from lesson 5 a few times, then gives up.
So a timeout points at the path: the security group's rule for that port and your address, the ACL in both directions, the route. A refusal proves your packet reached the machine: check that the program runs on that port and listens on an address other than 127.0.0.1.
It is a rule of thumb, because some firewalls reject: then a blocked port may look refused or report "No route to host". If a program clearly listens and you are still refused, check the machine's own firewall.
Check yourself
A security group allows SSH only from the office range 203.0.113.0/24. From home, SSH hangs, then times out. Is the server down?
Probably not. Your home address is outside that range, so the security group drops your packets silently, which shows up as a timeout. Connect from the office or add a /32 rule for your home address.
A web server's security group allows inbound TCP 443 from anywhere, and someone deletes all its outbound rules. Can visitors still load pages?
Yes. Security groups are stateful, so replies to an allowed inbound connection go out automatically. Only connections the server starts itself, such as downloading updates, break.
A subnet's network ACL allows only TCP 443, inbound and outbound. The security group is correct, yet pages never load. Why?
The ACL is stateless, so it checks the server's replies too. They go to each visitor's temporary port, not 443, and no outbound rule allows that; allowing outbound 1024–65535 fixes it.