Sub topic 7
Choosing a source: CIDR blocks, prefix lists and groups
This chapter assumes you know CIDR notation, how a security group rule is built (direction, protocol, port range, source or destination), and that security group rules only allow traffic. Each inbound rule needs a source. Each outbound rule needs a destination. This chapter covers the three kinds of source you can pick and when each one fits.
The three kinds of source
| Source type | Example | Matches | Best for |
|---|---|---|---|
| CIDR block | `203.0.113.25/32` | Traffic from an IP range | Internet clients, office networks, on-premises ranges |
| Prefix list | `pl-0123456789abcdef0` | Traffic from a managed set of CIDR blocks | The same set of ranges used in many rules |
| Security group | `sg-0123456789abcdef0` | Traffic from interfaces that have that group attached | Traffic between your own AWS resources |
CIDR blocks
A CIDR block matches by IP address. Use /32 for exactly one IPv4 address. Use 0.0.0.0/0 for every IPv4 address. IPv6 uses its own field, so ::/0 is a separate rule from 0.0.0.0/0.
This command allows HTTPS from any IPv4 address:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0Open ranges suit a public web server on port 443. They are a poor choice for administrative ports such as SSH. For those, use the narrowest range you can, and add a description so the purpose is clear later:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=203.0.113.25/32,Description="Admin workstation"}]'A common error is a CIDR that is not in canonical form, such as 10.0.0.5/24. The CLI may accept it and store the normalized range 10.0.0.0/24, which is wider than you may have intended. Check the rule with describe-security-groups after you create it.
Prefix lists
A prefix list is a named set of CIDR blocks. You create it once and reference its ID in a rule, in place of a CIDR. When you add or remove an entry, every rule that uses the list sees the change. Two kinds exist:
- Customer-managed prefix lists hold ranges you choose, such as all your office networks.
- AWS-managed prefix lists hold ranges AWS maintains, such as the ranges for some AWS services.
A prefix list has a maximum number of entries, which you set when you create it. That maximum counts against the quota of every security group rule that references the list. A list with 50 entries therefore uses up 50 rules' worth of quota in each group that references it. Set the maximum no higher than you need.
Security groups as a source
Instead of an IP range, a rule can name another security group. The rule then matches traffic from any interface with that group attached. It does not match the group's members by IP address, so the rule stays correct when instances are replaced or their private addresses change.
This command lets the application tier accept port 8080 only from the web tier:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0Two points cause confusion:
- The rule matches the private IP addresses of the source interfaces. Traffic that reaches your instance through a public IP or an Elastic IP does not match it.
- The reference gives access in one direction only. The web group can reach the application group on port 8080. The application group gains no access to the web group.
Choosing between them
- Is the traffic from your own resources in the same Region? Reference a security group.
- Is the same list of ranges repeated across several rules or groups? Use a prefix list.
- Is it a single, stable range, such as one administrator's address? Use a CIDR block.
Exercise
An application group must accept port 8080 from web servers that an Auto Scaling group replaces often, so their private IP addresses keep changing. Which source type should the rule use, and why?
Show answer
Use the web tier's security group as the source. The rule matches any interface that has that group attached, so new instances are allowed automatically and no rule edits are needed when addresses change.