Sub topic 4
Allow-only rules and implicit deny
This chapter assumes you know CIDR notation, ports and protocols. It also assumes you have created an EC2 instance and know how to run AWS CLI commands. You should know what a security group is and that it attaches to network interfaces. This chapter covers how a security group decides whether to permit traffic when it has only allow rules.
The core idea: every rule is an allow
A security group rule can only say "permit this traffic". There is no deny action, no rule priority, and no way to write "block 198.51.100.7". The rule has no field for an action. Direction, protocol, port range and a source or destination are the only choices.
The firewall's behavior follows from two facts:
- A packet is permitted if at least one rule in the security group allows it.
- A packet that no rule allows is dropped. This is the implicit deny.
You do not configure the implicit deny. It cannot be removed or edited, and it applies in both directions.
The following diagram shows the decision for a single new inbound connection to an instance.
The last step, automatic return traffic, comes from stateful filtering. The next chapter covers it. Here we only need the allow or drop decision.
What a new security group contains
The starting rules differ by direction. This difference causes many surprises.
| Direction | Rules in a new group | Effect |
|---|---|---|
| Inbound | None | All inbound traffic is denied. |
| Outbound | One rule: all protocols, all ports, destination 0.0.0.0/0 | All outbound IPv4 traffic is allowed. |
The outbound default does not cover IPv6. Traffic to ::/0 needs its own rule. A security group created in the console or with the AWS CLI gets the default outbound rule. The default inbound state is empty, so a new instance accepts no connections until you add a rule.
You can see both directions with one command:
aws ec2 create-security-group \
--group-name demo-sg \
--description "Allow-only demo" \
--vpc-id vpc-0123456789abcdef0
aws ec2 describe-security-groups \
--group-ids sg-0bbbbbbbbbbbbbbbb \
--query 'SecurityGroups[0].{In:IpPermissions,Out:IpPermissionsEgress}'The In list comes back empty. The Out list contains one entry with IpProtocol set to -1, which means all protocols, and a range of 0.0.0.0/0. Use the group ID that create-security-group returned in place of the placeholder shown here.
Consequences of having no deny rule
Removing a rule is how you block
Because access exists only through allow rules, you block traffic by not allowing it or by revoking the rule that allows it. If you revoke the HTTPS rule from a group, new HTTPS connections are dropped. No other change is needed.
You cannot carve an exception out of an allow
Suppose you want to allow 10.0.0.0/24 but exclude the hosts in 10.0.0.64/26. In a firewall with deny rules, you would add a deny with higher priority. Security groups cannot do this. Instead, rewrite the allowed space as ranges that skip the excluded block:
10.0.0.0/26(addresses .0 to .63)10.0.0.128/25(addresses .128 to .255)
aws ec2 authorize-security-group-ingress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--ip-permissions 'IpProtocol=tcp,FromPort=5432,ToPort=5432,IpRanges=[{CidrIp=10.0.0.0/26},{CidrIp=10.0.0.128/25}]'The excluded block is never mentioned. It is denied because nothing allows it. For large or irregular exclusions, this gets tedious. A better approach is to avoid broad allows in the first place and reference smaller sources, such as another security group.
A broad rule cannot be overridden by a narrow one
If a group contains an allow for 0.0.0.0/0 on port 22 and another allow for 203.0.113.25/32 on port 22, the narrow rule does nothing useful. The broad rule already covers every address. Rules are only ever additive. Adding a rule can only widen access, so review the whole rule list before concluding that a source is restricted.
Blocking a specific address needs a different tool
If a requirement says "block this abusive IP while the rest of the internet can reach the site", a security group cannot express it. A network ACL has explicit deny rules, and AWS WAF or AWS Network Firewall can filter at other layers. A later chapter compares security groups with network ACLs.
The outbound side is also allow-only
Many teams leave the default allow-all outbound rule in place. It is convenient, but it lets a compromised instance connect to any destination. To restrict outbound traffic, you delete the default rule and then allow only what the instance needs. After the default rule is gone, the implicit deny applies outbound as well.
aws ec2 revoke-security-group-egress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--ip-permissions 'IpProtocol=-1,IpRanges=[{CidrIp=0.0.0.0/0}]'
aws ec2 authorize-security-group-egress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--ip-permissions 'IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges=[{CidrIp=10.0.0.0/16,Description="HTTPS to VPC endpoints"}]'After these two commands, the instance can start connections only to port 443 inside 10.0.0.0/16. Things that used to work silently, such as DNS to the VPC resolver, package downloads and calls to AWS APIs, may now fail. Check the destinations your instance uses before you remove the default rule. The Amazon-provided DNS server answers queries without needing a security group rule, so name resolution through it continues to work. Dependencies that go to the public internet will not.
How denied traffic looks to a client
A security group drops packets without replying. The sender gets no refusal message. This gives you a useful diagnostic:
| Client symptom | Likely cause |
|---|---|
| Connection hangs, then times out | A security group (or network ACL, or route) dropped the packets. |
| Connection refused immediately | The packet reached the instance, but no process listens on that port. |
A "connection refused" error means the security group allowed the packet. Do not edit the group in that case. Start the service or check which port and interface it listens on.
Terraform and the default outbound rule
Terraform handles the default outbound rule differently from the console and the CLI. When Terraform creates an aws_security_group, it removes the default allow-all outbound rule. A group with no declared egress rules therefore has an implicit deny in both directions. Instances in it can accept nothing and reach nothing.
If you want outbound traffic, declare it:
resource "aws_security_group" "app" {
name = "app-sg"
description = "Application tier"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_egress_rule" "https_out" {
security_group_id = aws_security_group.app.id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "tcp"
from_port = 443
to_port = 443
description = "HTTPS to anywhere"
}This is a common cause of "works when I click it in the console, fails from Terraform". Compare IpPermissionsEgress in the two environments.
Common errors
- Looking for a deny option.
aws ec2 authorize-security-group-ingresshas no deny parameter. If you want to deny, rework the allow list or use a network ACL. - Assuming 0.0.0.0/0 covers IPv6. It covers IPv4 only. IPv6 clients need a rule with
::/0or a specific IPv6 range. - Specifying a port with protocol
-1. All-protocol rules (-1) allow every port. Port values are ignored, so the rule is wider than it appears. - Adding a duplicate rule. Authorizing a rule that already exists fails with
InvalidPermission.Duplicate. This is harmless, but it shows that the rule already exists. - Revoking by the wrong parameters. A revoke must match an existing rule's protocol, ports and source. A revoke that names a different source does not change the original rule.
- Revoking the default outbound rule without a replacement. The instance loses outbound access. Add the needed outbound rules in the same change.
Putting it together
For a security group, ask two questions about any packet:
- Is there at least one rule, in any attached group, that matches its direction, protocol, port and peer?
- If not, the packet is dropped, and nobody is told.
Everything else in security group design builds on this. Narrow rules reduce what is allowed, and the lack of a matching rule denies the rest.
Exercise
A security group allows TCP 443 from 0.0.0.0/0. You want to block the single address 198.51.100.7 from reaching port 443, while keeping the rule for everyone else. Can you do this with a security group rule? What are two ways to achieve the goal?
Show answer
No. Security groups have only allow rules, so you cannot add a deny for one address, and a narrower rule cannot override the broad allow. One option is to replace the 0.0.0.0/0 rule with a set of CIDR blocks that cover the whole address space except 198.51.100.7. That is impractical for the whole internet. The usual option is a network ACL on the subnet with an explicit deny rule for 198.51.100.7/32 numbered lower than the allow rule. AWS WAF (when behind an ALB or CloudFront) or AWS Network Firewall can also block the address.
Exercise
You create a security group with Terraform and attach it to an instance. Inbound SSH is allowed, but the instance cannot download packages from the internet. The same setup created in the console works. What is the most likely cause, and how do you fix it?
Show answer
Terraform removes the default allow-all outbound rule when it creates an awssecuritygroup, so outbound traffic hits the implicit deny. Fix it by declaring an egress rule, for example an awsvpcsecuritygroupegress_rule allowing the needed ports (such as TCP 443 and 80) to the required destinations. Also confirm routing to a NAT gateway or internet gateway.
Exercise
A client connecting to your instance on port 8080 gets "connection timed out" in one test and "connection refused" in another test after you change something. What does each result tell you about the security group?
Show answer
A timeout means packets were dropped before reaching a listening process, which points to a security group (or network ACL or routing) with no matching allow rule. "Connection refused" means the packets reached the instance and the operating system rejected them because nothing listens on the port, so the security group allowed the traffic.