Security Group Rule Design Best Practices
What you'll learn: How to design security group rules that minimize attack surface while maintaining necessary access.
Core Principle: Least Privilege
Security group rules act like bouncers at your cloud resource's door—they decide who gets in and for what purpose. The principle of least privilege means granting only the minimum permissions required for legitimate traffic. Never open more ports, protocols, or sources than absolutely necessary.
Think of it like giving someone keys to specific rooms in a building, not a master key to everything.
Port Range Specificity
Avoid broad port ranges. Instead of allowing 0-65535, specify exact ports:
- ✅ Allow TCP port
443for HTTPS - ✅ Allow TCP port
5432for PostgreSQL - ❌ Allow TCP ports
1-10000"just in case"
Opening wide port ranges creates unnecessary exposure. Even unused ports become potential attack vectors.
Protocol Selection
Choose the specific protocol your application needs:
- Use
TCPfor connection-oriented services - Use
UDPfor specific services that require it (like DNS) - Use
ICMPonly when necessary for diagnostics - Avoid allowing "All Protocols" unless you have exceptional requirements
Source and Destination Specifications
Be precise about sources:
- ✅ Specify exact IP addresses or CIDR blocks (
10.0.1.5/32) - ✅ Reference other security groups for inter-service communication
- ❌ Use
0.0.0.0/0(internet-wide access) unless serving public traffic
For internal services, source should be specific security groups or private subnets—never "anywhere."
Avoiding Overly Permissive Rules
Common anti-patterns to avoid:
- Allowing
0.0.0.0/0on administrative ports (SSH, RDP) - Using description "temporary test rule" that stays forever
- Opening all ports to your office network instead of specific ranges
- Allowing unrestricted outbound traffic without justification
Review rules quarterly and remove anything not actively required.
Key Takeaway: Design security group rules with surgical precision—specific ports, protocols, and sources only. Every overly broad rule is an invitation to attackers.