Sub topic 6
How Rules Combine: No Ordering, Multiple Groups Add Up
This chapter explains how AWS decides whether a packet is allowed when a security group holds many rules, and when an interface has several security groups attached. It assumes you know CIDR notation, ports and protocols, what an EC2 instance and a VPC are, and that a security group holds allow-only rules attached to a network interface.
The core idea: a union of allow rules
A security group is a set of allow rules. When traffic reaches a network interface, AWS asks one question: does any rule, in any attached security group, permit this traffic? If at least one does, the traffic is allowed. If none does, the traffic is dropped by the implicit deny.
This produces three properties that surprise people who come from traditional firewalls:
- Rules have no order. There is no "first match wins" and no rule number.
- Rules cannot conflict. There is no deny rule, so one rule can never cancel another.
- Adding a rule or a group can only widen access. Removing one can only narrow it.
A traditional firewall evaluates rules from top to bottom and stops at the first match. A security group evaluates the whole set at once, and the result is the logical OR of every rule.
No ordering within a group
Consider a group with these inbound rules.
| Rule | Protocol | Port | Source |
|---|---|---|---|
| 1 | TCP | 443 | 0.0.0.0/0 |
| 2 | TCP | 443 | 10.0.0.0/16 |
| 3 | TCP | 22 | 203.0.113.25/32 |
Rule 2 is redundant, because rule 1 already allows port 443 from every IPv4 address, including 10.0.0.0/16. The redundancy does no harm and does not change the outcome. The order in which the console, the CLI or the API lists the rules carries no meaning, and you cannot reorder them.
Two consequences follow.
- You cannot make a rule more specific to override a broader one. In a firewall with ordered rules, you might allow
10.0.0.0/16and then deny10.0.5.0/24above it. A security group cannot express this. To exclude a subrange, you must replace the broad rule with several narrower rules that leave the excluded range out. - Overlapping rules are legal but wasteful. They count against your rule quota (see the chapter on quotas) and make audits harder. AWS rejects an exact duplicate rule with an
InvalidPermission.Duplicateerror, but it does not detect overlaps such as the one above.
Rules are matched on all fields together
A rule matches only when every field matches: direction, protocol, port range and source (or destination). Two rules never combine into a larger one. Port 80 from 10.0.0.0/16 plus port 443 from 192.168.0.0/16 does not permit port 80 from 192.168.0.0/16.
Likewise, inbound and outbound rules are evaluated separately. An inbound rule has no effect on new connections that the instance starts, and an outbound rule has no effect on connections arriving at the instance. (Return traffic for an allowed connection is handled by connection tracking, which is covered in the chapter on stateful filtering.)
Multiple security groups on one interface
A network interface can have more than one security group attached. The default limit is five groups per interface, and it can be raised with a quota request. When several groups are attached, AWS merges their rules into one effective set and applies the same union logic.
Suppose an instance has two groups.
| Group | Direction | Protocol | Port | Source |
|---|---|---|---|---|
| `web-sg` | Inbound | TCP | 443 | 0.0.0.0/0 |
| `admin-sg` | Inbound | TCP | 22 | 203.0.113.25/32 |
The instance accepts HTTPS from anywhere and SSH from the single administrator address. Neither group needs to know about the other. Detaching admin-sg removes SSH access and leaves HTTPS untouched.
This lets you build groups as reusable building blocks:
- A group for a role, such as
web-sgorworker-sg. - A group for a capability, such as
admin-ssh-sgormonitoring-sg. - A group for a shared dependency, such as
db-client-sg, which other groups can reference as a source.
Why one group cannot restrict another
Because every group only adds permissions, attaching a "strict" group does not tighten a "loose" one. If loose-sg allows all TCP ports from anywhere, and you attach strict-sg that allows only port 443, the instance still accepts every TCP port. The strict group contributes nothing to narrowing the result.
To restrict an instance, remove permissions. You cannot add a group to subtract them.
Seeing the effective rules
Because the effective policy is spread across groups, you need to look at all of them. First find the groups attached to an instance, then list their rules.
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[].Instances[].NetworkInterfaces[].[NetworkInterfaceId,Groups[].GroupId]'
aws ec2 describe-security-groups \
--group-ids sg-0123456789abcdef0 sg-0aaaaaaaaaaaaaaaa \
--query 'SecurityGroups[].[GroupId,IpPermissions]'The first command prints each interface and its attached group IDs. The second prints the inbound rules (IpPermissions) of the groups you name. Use IpPermissionsEgress to see outbound rules. Remember that groups are attached per interface, so an instance with two network interfaces can have different effective rule sets on each.
Evaluation across the two directions
The union rule applies once for each direction. A connection that a client opens to your instance needs an inbound allow on the instance's interface. If the client is also an instance in your VPC, its own interface needs an outbound allow toward your instance. Both checks must pass, and each is a union over that interface's groups.
By default, a new security group has one outbound rule that allows all traffic to all destinations. If you keep that rule, outbound filtering is effectively off for that group. If you delete it, the outbound union is empty and the instance can start no connections, until you add allow rules. Another attached group that still has the default outbound rule would restore full outbound access, because of the union.
Common mistakes
- Expecting a later rule to override an earlier one. There is no override. If a port is open somewhere in the union, it is open.
- Attaching a restrictive group to "lock down" an instance. It has no effect while a permissive group stays attached. Find and remove the permissive rule.
- Forgetting the default group. An instance launched without an explicit group gets the VPC's default security group. If you add a custom group but leave the default one attached, its rules still contribute to the union.
- Reading only one group during an audit. Check every group on every interface. The surprise rule is often in a shared group that another team owns.
- Assuming a rule you cannot see is absent. Rules in groups attached through an auto scaling launch template or a load balancer are only visible by inspecting those groups.
Designing with the union model
Knowing that rules only add, you can use these habits:
- Keep each group small and named for one purpose, so that the effect of attaching or detaching it is easy to predict.
- Put shared permissions in a shared group and attach it where needed, rather than copying the same rule into many groups.
- Prefer one narrow rule over a broad rule plus exceptions. Exceptions do not exist.
- Review the union, not individual groups, when you ask "who can reach this instance?"
Exercise
An instance has two security groups. Group A allows inbound TCP 443 from 0.0.0.0/0. Group B allows inbound TCP 443 only from 10.0.0.0/16. A client at 198.51.100.7 connects to port 443. Is it allowed, and what should you change if you wanted only the 10.0.0.0/16 range to connect?
Show answer
The connection is allowed. The effective rules are the union, and Group A's rule matches any IPv4 source. Group B's narrower rule does not restrict anything. To limit access to 10.0.0.0/16, remove the 0.0.0.0/0 rule from Group A (or detach Group A if it exists only for that rule). Group B's rule then becomes the only match.
Exercise
A security group lists rule 1 allowing TCP 80 from 10.0.0.0/16, and rule 2 allowing TCP 443 from 192.168.0.0/16. Can a host at 192.168.4.9 connect to port 80? Explain why.
Show answer
No. A rule matches only when protocol, port and source all match, and rules do not merge. Rule 1 covers port 80 but not that source. Rule 2 covers that source but not port 80. No rule matches, so the implicit deny applies.