Sub topic 7
Choosing a source: CIDR blocks, prefix lists and groups
This chapter assumes you know CIDR notation, can read a security group rule (direction, protocol, port range, source or destination), and understand that security groups allow traffic and deny everything else by default. It covers the three kinds of source or destination you can put in a rule, what each one means, and how to pick between them.
The three kinds of source
Every rule needs something that says which traffic it matches. For an inbound rule this is the source. For an outbound rule it is the destination. A security group rule accepts one of these:
- A CIDR block: an IPv4 range (
--cidr) or an IPv6 range (::/0style, set withIpv6Ranges). - A prefix list: a named, reusable set of CIDR blocks, referenced by its ID (
pl-...). - A security group: a reference to another group (
sg-...), which matches traffic from or to the network interfaces that group is attached to.
Each rule uses exactly one of them. To allow two kinds of source on the same port, create two rules.
| Source type | Matches | Best for | Changes when |
|---|---|---|---|
| CIDR block | Addresses in a range | Internet clients, on-premises networks, fixed partners | You edit the rule |
| Prefix list | Every CIDR in the list | The same set of ranges used in many groups | You edit the list once |
| Security group | Interfaces that have the group attached | Traffic between your own AWS resources | Instances join or leave the group |
CIDR blocks
A CIDR block rule matches by IP address. The prefix length sets how many addresses match.
203.0.113.25/32matches exactly one IPv4 address.10.0.0.0/16matches 65,536 addresses.0.0.0.0/0matches every IPv4 address.::/0matches every IPv6 address.
IPv4 and IPv6 are separate. A rule for 0.0.0.0/0 does not allow IPv6 clients. If your instance has an IPv6 address and should be reachable over it, add a second rule with ::/0.
The AWS CLI example from the plan opens HTTPS to the whole IPv4 internet:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0That is reasonable for a public web server on port 443. It is a poor choice for SSH, databases, or any admin port. For those, use the narrowest range that works. A single-address rule with a description keeps its purpose clear:
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"}]'Gotchas with CIDR rules
- Host bits are normalized. If you write
10.0.0.5/24, the rule is stored as10.0.0.0/24. The rule matches the whole /24, not one host. Check that you wrote/32when you meant one address. - Public addresses change. An office or home connection often gets a new public IP from its provider. A
/32rule then silently stops matching, and the symptom is a timeout on SSH. - Private ranges match private addresses. Inside a VPC, traffic between instances in the same VPC arrives with private source addresses. A rule for the VPC CIDR therefore allows every interface in the VPC, including ones you did not intend.
- Duplicates are rejected. Adding a rule that already exists returns an
InvalidPermission.Duplicateerror.
Prefix lists
A prefix list is a named set of CIDR blocks. You create it once, then reference its ID in rules. When you change the list, every rule that references it follows the change.
There are two kinds:
- Customer-managed prefix lists hold CIDRs you choose. Each list has an address family (IPv4 or IPv6) and a maximum number of entries that you set when you create it.
- AWS-managed prefix lists are maintained by AWS. Examples are the lists for Amazon S3 and DynamoDB gateway endpoints, and the CloudFront origin-facing list. You can reference them but not edit them.
Creating a customer-managed list and using it in a rule:
aws ec2 create-managed-prefix-list \
--prefix-list-name office-ranges \
--address-family IPv4 \
--max-entries 10 \
--entries Cidr=203.0.113.0/24,Description="HQ" Cidr=198.51.100.0/24,Description="Branch"
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,PrefixListIds=[{PrefixListId=pl-0123456789abcdef0,Description="Office SSH"}]'Replace pl-0123456789abcdef0 with the ID that create-managed-prefix-list prints.
Why use a prefix list
Suppose forty security groups each allow the same five office ranges. When a range changes, you would edit forty groups. With a prefix list, you edit one list. Updates are versioned, so you can see what changed and restore an older version.
Prefix lists and quotas
The part that surprises people is how a prefix list counts against the rule quota of a security group. A reference to a prefix list counts as the maximum number of entries declared on the list, not the number of entries currently in it. A list created with --max-entries 10 uses 10 rule slots in every group that references it, even if it has two entries today. Set the maximum close to what you need. The quota itself is covered in a later chapter.
Two further limits matter:
- The address family must match the rule. An IPv4 list belongs in an IPv4 rule, and an IPv6 list in an IPv6 rule.
- You can share a customer-managed list with other accounts through AWS Resource Access Manager. Security groups in another account can reference it, though that account cannot modify it.
Security groups as sources
A rule can name another security group instead of an address range. The rule then matches traffic whose source is a network interface that has the referenced group attached. The plan's second example allows the application tier in only from the web tier:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0This rule says: allow TCP 8080 into the group sg-0aaaaaaaaaaaaaaaa from any interface that carries sg-0123456789abcdef0.
Two points about what this does and does not do:
- It matches the private IP addresses of the attached interfaces. It does not pull in the referenced group's own rules. The web group's inbound rules play no part.
- Membership is dynamic. Launch a new web instance with the web group and it is allowed immediately. Terminate one and its address stops matching. No rule edits are needed, which is the main advantage over listing instance IPs.
The next chapter covers references in more detail, including cross-VPC cases. Here the point is the choice: use a group reference whenever both ends are your own resources in AWS.
The traffic path decides which source matches
A CIDR rule sees the source address of the packet as it arrives at the interface. That address depends on the path:
- A client on the internet reaching an instance through an internet gateway appears with its public address.
- A client behind a NAT gateway appears with the NAT gateway's address, so a rule can only restrict to the NAT gateway's range, not to individual clients behind it.
- Traffic that goes through a load balancer arrives from the load balancer's private addresses. The instance's group should allow the load balancer's security group, not the end clients' CIDR.
- A security group reference works with private addresses. If traffic between two instances goes through their public or Elastic IP addresses, a group reference will not match. Use private addressing between your own resources.
Choosing between them
Work through these questions in order:
- Are both ends resources inside AWS that you control? Use a security group reference.
- Is the source outside AWS (internet, office, partner)? Use a CIDR block.
- Does the same set of CIDR blocks appear in more than one group, or change often? Use a prefix list.
- Does AWS publish a list for the service you are allowing (CloudFront, S3 endpoints)? Use the AWS-managed prefix list rather than copying ranges that may go stale.
Declaring sources in Terraform
In Terraform, each rule is its own resource, and each resource takes exactly one source argument. The attribute names show the three kinds:
resource "aws_vpc_security_group_ingress_rule" "from_office_cidr" {
security_group_id = aws_security_group.web.id
cidr_ipv4 = "203.0.113.25/32"
ip_protocol = "tcp"
from_port = 22
to_port = 22
description = "Admin workstation"
}
resource "aws_vpc_security_group_ingress_rule" "from_office_list" {
security_group_id = aws_security_group.web.id
prefix_list_id = "pl-0123456789abcdef0"
ip_protocol = "tcp"
from_port = 443
to_port = 443
description = "Office ranges"
}
resource "aws_vpc_security_group_ingress_rule" "from_web_group" {
security_group_id = aws_security_group.app.id
referenced_security_group_id = aws_security_group.web.id
ip_protocol = "tcp"
from_port = 8080
to_port = 8080
description = "App port from web tier"
}This assumes aws_security_group.web and aws_security_group.app are defined elsewhere in the configuration. The attributes cidr_ipv4, prefix_list_id and referenced_security_group_id are mutually exclusive, so setting two in one resource is an error.
Common mistakes
- Using
0.0.0.0/0on admin ports. Port 22 or 3389 open to the world draws constant scanning. Use a narrow CIDR, a prefix list, or a managed access method. - Forgetting IPv6. A dual-stack instance can be reachable over IPv6 through a rule you did not intend, or unreachable because you only wrote the IPv4 rule.
- Hard-coding instance IPs. The rule breaks when instances are replaced. Use a group reference.
- Oversized prefix list maximums. Each reference costs the full maximum in rule quota.
- Referencing a group across a path that changes the source. NAT, load balancers and public addresses all alter what the target sees.
Exercise
A database group must accept TCP 5432 from the application servers, which are launched and terminated by an Auto Scaling group and have the group sg-app attached. Which kind of source should the rule use, and why would a CIDR rule be a worse choice?
Show answer
Use a security group reference to sg-app. Auto Scaling changes the set of instances and their private IPs over time. A group reference follows membership automatically, so new instances are allowed and terminated ones stop matching. A CIDR rule would need either a wide subnet range that also admits unrelated hosts, or constant edits as addresses change.
Exercise
You create a customer-managed prefix list with --max-entries 50 and only 3 entries, then reference it from 20 security groups. How many rule slots does the list use in each group, and what should you do if that is a problem?
Show answer
It uses 50 slots in each group, because a reference counts as the list's maximum size, not its current entry count. If that pushes groups toward their rule quota, recreate the list with a maximum closer to what you need, leaving only modest room for growth, and point the rules at the new list.