"Every reply needs its own firewall rule." On many firewalls, that is how it works. An AWS security group is different: it remembers the connections it has allowed and lets the replies through by itself. Understanding that one behaviour removes a lot of unnecessary rules, and a lot of confusion when you debug.
What stateful means
A security group is the firewall around a resource, such as an EC2 instance, a load balancer or a database. More precisely, it is attached to the resource's network interface.
Stateful means it tracks each connection:
- If an inbound request is allowed, the response is allowed to leave, regardless of the outbound rules.
- If your resource sends an outbound request, the response is allowed back in, regardless of the inbound rules.
So a web server needs one inbound rule for port 443. It does not need an outbound rule for the replies. And a server that only calls other services needs no inbound rule at all.
This is the opposite of a network ACL, which is stateless and checks every packet in both directions. The comparison is covered in Security Groups vs NACLs.
Allow rules only
Security group rules can only allow. There is no deny rule. Anything that no rule allows is denied.
That gives a simple way to think: for each resource, ask who starts the conversation, and write a rule for that side only.
- Users start connections to the load balancer: inbound 443 on the load balancer's group.
- The load balancer starts connections to the app: inbound on the app's group, with the load balancer's security group as the source.
- The app starts connections to the database: inbound on the database's group from the app's group.
Referencing a security group instead of an IP range keeps the rule correct when servers are replaced.
Defaults to know
- A new security group has no inbound rules, so nothing can connect in, and one outbound rule that allows all traffic.
- The default security group of a VPC is different: it allows inbound traffic from other resources in the same group. AWS recommends creating your own groups instead of using it.
- You can attach several security groups to one resource. Their rules add up.
A common mistake is leaving outbound wide open everywhere. For sensitive servers, tighten the outbound rules so a compromised machine can't freely call out.
Another is opening SSH (22) or RDP (3389) to 0.0.0.0/0. AWS's own guidance is to authorize only specific address ranges for those ports.
Where AI fits
Writing a rule is a good task for an assistant. Describe it in plain English, for example "allow HTTPS to the app servers only from the load balancer's security group", and ask for the AWS CLI command or the infrastructure-as-code snippet. The AWS assistant in the AWS Management Console (formerly Amazon Q Developer in the console) can generate short scripts and CLI snippets and can look up the resources your identity is allowed to see. Coding assistants can do the same in your editor.
The assistant drafts; you approve. Before running anything, check three things: the source (a group or narrow range, not the whole internet), the port range (one port, not all of them) and the group it is applied to.
Quick recap
- Security groups are stateful: replies to allowed traffic are automatic, in both directions.
- You write allow rules only, for whoever starts the conversation. Everything else is denied.
- Let AI draft the rule if you like, but review it before it reaches production.