Sub topic 4
Allow-only rules and implicit deny
This chapter assumes you know TCP/IP basics (CIDR notation, ports, protocols), can launch EC2 instances, understand VPCs and subnets, and can run AWS CLI commands. It covers how a security group decides what to block, and the mistakes that follow from that model.
The allow-only model
A security group contains only allow rules. You can't write a rule that says "deny port 23" or "block this address." Every rule you add opens something. Anything that no rule opens is dropped.
This is called implicit deny. The deny isn't a rule you can see or edit. It is what happens when traffic matches nothing.
A new security group shows the model clearly:
- It has no inbound rules, so it accepts no inbound traffic.
- It has one outbound rule that allows all traffic to any destination, so instances can reach out freely.
Adding a rule only ever widens access. Removing a rule only ever narrows it.
What a rule matches
Each rule matches on direction, protocol, port range, and a source (inbound) or destination (outbound). Traffic is allowed when all of those fit at least one rule. For example, this rule opens TCP port 443 to every IPv4 address:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0Nothing else is open after this command. A request on port 80, or a UDP packet to port 443, matches no rule and is dropped.
Consequences of having no deny rule
Three practical results follow.
- You can't carve out an exception. Suppose you allow
10.0.0.0/16and want to exclude10.0.5.0/24. A security group can't express that. Instead, allow the smaller ranges that make up the part you want, or use a different control. Network ACLs support explicit deny rules, and a later chapter compares them with security groups. - Rule order doesn't matter. Because no rule can override another, there is nothing to sort. If any rule allows the traffic, it passes.
- Dropped traffic is silent. Denied packets are discarded without an error reply. A client sees a timeout, not a "connection refused" message. A timeout is the typical sign of a security group block.
Outbound rules follow the same model
Outbound rules are also allow-only. The default "allow all" outbound rule is a convenience, not a requirement. If you delete it and add nothing, the instance can't start any outbound connection, including calls to package repositories or AWS APIs. Teams with strict requirements replace it with narrow rules, such as HTTPS to a specific prefix list or a database port to one group.
Checking what is open
Because the only way to understand a group is to read its allows, list them:
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0Read the IpPermissions (inbound) and IpPermissionsEgress (outbound) sections. Anything not listed is blocked.
Common errors
| Symptom | Likely cause |
|---|---|
| Connection times out | No rule allows that port, protocol or source |
| Connection refused | Traffic reached the instance, so the security group isn't the cause. Check whether anything is listening on the port. |
| Removed a rule but access remains | Another rule or another attached group still allows it |
| Can't block one address inside an allowed range | Security groups have no deny rule. Split the range or use a network ACL. |
Exercise
A security group allows inbound TCP 22 from 10.0.0.0/16. You need to stop one host, 10.0.9.9, from connecting, while everyone else in the range keeps access. Why can't you do this with a deny rule in the security group, and what are two ways to get the result?
Show answer
Security groups support only allow rules, so there is no way to write a deny for 10.0.9.9. One option is to replace the /16 rule with several narrower CIDR rules that cover the range except 10.0.9.9. Another is to add a network ACL on the subnet with an explicit deny for 10.0.9.9/32, since network ACLs support deny rules.