All posts
No. 001 / VPC

Security groups have no deny rule

Coming from firewalls, I went looking for the deny rule. Security groups don't have one.

Orange patch cables plugged into a grey switch

On every firewall I've run, a rule ends in permit or deny. So when I built my first security group in the lab, I looked for the action field so I could block one address. There isn't one.

What a security group can do

A security group is a list of things you allow. Anything you haven't allowed is dropped. You can't write a rule that says deny, so you can't carve an exception out of a wider allow.

It is also stateful. If you allow a request in, the reply goes back out without a rule of its own.

The rule, in Terraform

Here is the ingress rule from the lab. There is no argument for an action, because allow is the only option. The closest thing to a deny is leaving the rule out.

security-group.tfTerraform
resource "aws_security_group" "web" {
  name   = "web"
  vpc_id = aws_vpc.lab.id
}

resource "aws_vpc_security_group_ingress_rule" "https" {
  security_group_id = aws_security_group.web.id
  cidr_ipv4         = "0.0.0.0/0"
  from_port         = 443
  to_port           = 443
  ip_protocol       = "tcp"
}

Where the deny lives

To block a specific address you go up a level, to the network ACL on the subnet. A NACL has both allow and deny, and it reads the way an access list does: numbered rules, lowest first, first match wins. In Terraform that is aws_network_acl_rule, and it does have a rule_action.

Security group Network ACL
Applies to A network interface A subnet
Rules Allow only Allow and deny
State Stateful Stateless
Evaluation All rules together Number order, first match

What I do now

In the lab, security groups say who may reach each resource, and each subnet has one NACL for the rare address I need to block. Before I call a security group done, I check three things:

  • Each rule names a source, a port and a protocol.
  • No rule is wider than the thing it protects needs.
  • The description says why the rule exists.
Changes to this post
[DATE]First published
[DATE]Corrected the part about rule order

Spot amistake?

If something here is wrong, tell me. I'll fix the post and say what changed.

Phillip EvansNetwork engineerSend a correction