Vpc Deny Ports
Deny-Listing Ports at the VPC Boundary with NACLs
Security groups can’t deny — they’re allow-only. If you need an explicit “never this port” rule, the tool is the Network ACL, and doing it via module helpers beats hand-numbering ACL entries.
The pattern
This example builds a VPC and then applies deny rules for specific ports at the NACL layer — the stateless, subnet-level firewall that evaluates before security groups.
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
tags = { Name = var.vpc_name }
}
The module’s NACL helpers (network-acl-inbound, network-acl-outbound, port-range-calculator) translate “deny ports 3389, 1433” into correctly-numbered aws_network_acl_rule entries — including the tricky part everyone forgets: NACL rules need an explicit DENY evaluated in rule-number order, so a deny must land below the allow rules but above the final * deny.
Why deny at the NACL instead of the SG
- Stateful vs stateless: SG return traffic is auto-allowed; a NACL deny kills both directions — useful for hard blocks on RDP/SMB/database ports regardless of any SG mistake
- Defense in depth: SG errors are the most common exposure path; a subnet-level deny is the safety net that doesn’t depend on every resource’s SG being right
- Compliance language: “port 3389 is network-denied at the subnet boundary” is a much stronger audit statement than “we didn’t open it in the SG”
Takeaway
Allow in the security group, deny in the NACL. The two layers answer different questions — and only one of them can say “no, ever.”
Verified: reconstructed example passes terraform validate.