Sub topic 6
How rules combine: no ordering, multiple groups add up
This chapter assumes you know CIDR notation, ports and protocols, and that a security group is attached to an EC2 instance's network interface. It covers how AWS decides whether traffic is allowed when a network interface has many rules, or many security groups.
No rule order, no priorities
Security group rules have no numbers and no order. AWS does not read them top to bottom, and no rule can override another. Security groups only contain allow rules, so no rule can deny traffic either.
For a packet, AWS asks one question: does at least one rule in the applicable groups allow it? If yes, the traffic is permitted. If no rule matches, the implicit deny applies and the packet is dropped.
A group's rules therefore form a union. If one rule allows TCP 443 from 0.0.0.0/0 and another allows TCP 443 from 10.0.0.0/16, the narrower rule adds nothing. The broader rule already covers it.
Multiple groups add up
You can attach more than one security group to a network interface. AWS combines all the rules from all the attached groups into one effective set. This applies to inbound and outbound rules separately.
Suppose an instance has two groups:
| Group | Inbound rule |
|---|---|
| `web-sg` | TCP 443 from `0.0.0.0/0` |
| `admin-sg` | TCP 22 from `203.0.113.25/32` |
The instance accepts HTTPS from anywhere and SSH from the administrator address. Removing admin-sg from the instance removes only the SSH access.
Why this matters
Adding a group can only widen access. It never narrows it. Common consequences:
- You cannot make an instance stricter by attaching a more restrictive group. You must remove or edit the permissive rule.
- A broad rule in one group, such as a shared "allow all from the office" group, applies to every instance that has that group.
- To find out why traffic is allowed, check every group on the interface, not just the one you are editing.
Checking the effective rules
First list the groups attached to an instance, then read each group's rules.
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].SecurityGroups[].GroupId"
aws ec2 describe-security-groups \
--group-ids sg-0123456789abcdef0 sg-0aaaaaaaaaaaaaaaa \
--query "SecurityGroups[].{Id:GroupId,Inbound:IpPermissions}"The first command returns the group IDs. The second shows the inbound rules of the groups you name. Read all of them together to get the effective inbound rules.
Common mistakes
- Expecting a narrow rule to win. A rule for one address does not restrict a broader rule elsewhere that already allows the traffic.
- Looking at one group only. An unexpected open port often comes from a second group attached to the same interface.
- Trying to block one address. You cannot add a deny rule. To exclude an address, replace the broad rule with rules that leave it out, or use a network ACL, which supports deny rules.
- Forgetting that inbound and outbound are separate. An inbound allow in one group does not depend on an outbound rule, and the reverse is also true.
Exercise
An instance has two security groups. Group A allows TCP 3306 from 10.0.1.0/24. Group B allows TCP 3306 from 0.0.0.0/0. Which source addresses can reach port 3306, and how would you restrict access to 10.0.1.0/24 only?
Show answer
Any address can reach port 3306, because the rules are combined and Group B's rule already allows all IPv4 sources. Group A's narrower rule does not restrict it. To restrict access, remove the 0.0.0.0/0 rule from Group B (or detach Group B from the instance) so only Group A's rule remains.