Sub topic 15
Quotas and keeping rule sets manageable
This chapter assumes you can read a security group rule, know that rules are allow-only and stateful, and have used CIDR blocks, prefix lists and security group references. It covers the limits AWS places on security groups, how those limits interact, and habits that keep rule sets small enough to review.
Why quotas matter for rule design
Security groups look unlimited when you have three of them. Teams usually discover the limits when a deployment fails with an error such as RulesPerSecurityGroupLimitExceeded or SecurityGroupLimitExceeded. Knowing the limits before you design lets you choose sources and group layouts that never reach them.
Every quota below is a default. Most can be raised through Service Quotas, but a raise has a cost. Large rule sets are harder to audit, and the number of rules that apply to one network interface also affects how the platform processes traffic.
The quotas to know
These are the limits that shape designs. Check the Service Quotas console or CLI for the current values in your account and Region, because AWS can change defaults and your account may already have raised values.
| Quota | Typical default | Scope |
|---|---|---|
| Security groups per VPC | 2,500 | Per Region, per VPC |
| Inbound or outbound rules per security group | 60 each | Per security group |
| Security groups per network interface | 5 | Per network interface |
| Entries in a prefix list (maximum entries) | Set when you create the list | Per prefix list |
Two details are easy to miss:
- The rules-per-group limit counts IPv4 and IPv6 rules separately. A rule with an IPv4 CIDR and a rule with an IPv6 CIDR each use one of the 60 slots. Inbound and outbound are counted separately.
- Security groups per network interface multiplied by rules per group gives the number of rules that can apply to one interface. AWS requires that this product stay at or under 1,000. If you raise one of the two quotas, the other may have to come down.
You can read the real values from the CLI:
aws service-quotas list-service-quotas \
--service-code vpc \
--query "Quotas[?contains(QuotaName, 'security group')].[QuotaName,Value,QuotaCode]" \
--output tableUse the QuotaCode from that output to request an increase:
aws service-quotas request-service-quota-increase \
--service-code vpc \
--quota-code L-0EA8095F \
--desired-value 100The code L-0EA8095F is the code AWS has used for inbound or outbound rules per security group. Confirm it in your own output before you submit a request.
How the limits interact
The quotas are linked, so changing one has side effects. The diagram shows the relationship that matters most: an interface has several groups, and each group has its own rules, all of which count toward the interface limit.
Because rules from all attached groups are merged, a quota error on an instance launch can come from the sum of groups, not from any one group.
Referenced groups do not multiply rules
A security group reference is one rule, no matter how many instances carry the referenced group. If the application tier allows port 8080 from the web tier's group, that is one rule whether the web tier has two instances or two hundred.
Compare that with allowing the web tier by IP address. You would need one /32 rule per instance, and you would have to edit the rules every time Auto Scaling adds or removes an instance. References scale with no rule growth, so they are the first tool to reach for when you want to stay under the limit.
Prefix lists collapse many CIDRs into one rule
When the source really is a set of IP ranges, such as office networks or a partner's addresses, a customer-managed prefix list lets you refer to all of them with a single rule. A prefix list counts toward a security group's rule quota according to its maximum size, not the number of entries it currently holds. That behavior surprises people.
Create the list with a maximum entry count, then add entries:
aws ec2 create-managed-prefix-list \
--prefix-list-name office-networks \
--address-family IPv4 \
--max-entries 20 \
--entries Cidr=203.0.113.0/24,Description="HQ" Cidr=198.51.100.0/24,Description="Branch office"Reference it from a rule:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=443,ToPort=443,PrefixListIds=[{PrefixListId=pl-0123456789abcdef0,Description="Office networks"}]'Set --max-entries to a realistic ceiling. If you set it to 1,000 "just in case", the rule can consume 1,000 slots of the group's capacity and the rule may fail to create. You can raise the maximum later with modify-managed-prefix-list, and security groups that reference the list are updated to account for the new size.
The same prefix list can be shared across many security groups. Updating the list once changes every rule that points to it, which removes the need to edit rules in dozens of groups.
Keeping rule sets manageable
Staying under quota is a minimum bar. A rule set can be within limits and still be impossible to review. These practices keep groups readable.
- Group by role, not by instance. Create one group per function, such as
web-sg,app-sganddb-sg, and attach it to every instance with that function. Avoid one group per server. - Use several small groups per interface. Attach a shared group for common access, such as monitoring, next to a role group. This keeps the common rule in one place. Remember the limit of five groups per interface.
- Prefer references and prefix lists over raw CIDRs. Each raw CIDR is a rule you must maintain by hand.
- Describe every rule. A description is searchable and tells a reviewer why the rule exists. A rule with no description is a rule nobody dares to delete.
- Delete what is unused. Groups that no interface uses and rules that reference removed resources add noise.
- Do not rely on broad ranges to save rules. Replacing ten precise rules with
0.0.0.0/0stays within quota but gives up least privilege. Use a prefix list or a group reference instead.
Finding unused and duplicate groups
Security groups that are not attached to anything still count toward the per-VPC quota. This command lists the groups in a VPC next to the network interfaces that use them, so you can spot candidates for removal:
aws ec2 describe-network-interfaces \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 \
--query "NetworkInterfaces[].Groups[].GroupId" \
--output text | tr '\t' '\n' | sort -u > used-groups.txt
aws ec2 describe-security-groups \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 \
--query "SecurityGroups[].GroupId" \
--output text | tr '\t' '\n' | sort -u > all-groups.txt
comm -13 used-groups.txt all-groups.txtThe final command prints the IDs that exist but are not attached to any interface in that VPC. Before you delete one, check three things:
- Another security group may reference it in a rule. A delete fails with a dependency error if so, which is a useful safety net.
- The group may be reserved for a resource you create on demand, such as a launch template that is not currently running.
- The default group cannot be deleted. Ignore it in your cleanup.
Delete a confirmed unused group with:
aws ec2 delete-security-group --group-id sg-0aaaaaaaaaaaaaaaaUsing infrastructure as code to prevent drift
Manual console edits are the main reason rule sets grow without anyone deciding they should. If Terraform owns the groups, a one-off rule added in the console shows up as drift in the next plan. The separate rule resources shown earlier help here: each rule is one block, a reviewer sees a one-line diff, and removing a rule means removing a block.
Avoid mixing inline ingress blocks inside aws_security_group with separate rule resources for the same group. The two approaches fight over ownership and can remove rules that the other created. Pick one style per group, and prefer the separate aws_vpc_security_group_ingress_rule resources.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `RulesPerSecurityGroupLimitExceeded` | The group has reached its inbound or outbound rule cap | Merge CIDRs into a prefix list, use group references, or request an increase |
| `SecurityGroupLimitExceeded` | The VPC has reached its group cap | Delete unused groups or share groups between roles |
| Creating a rule with a prefix list fails | The list's maximum size exceeds the group's remaining capacity | Lower the list maximum or free rule slots |
| Cannot attach another group to an interface | The interface already has the maximum number of groups | Merge groups or consolidate common rules |
| Quota increase request is rejected or only partly granted | The product of the per-interface and per-group quotas would pass 1,000 | Lower one quota while raising the other |
Exercise
A security group allows HTTPS from 15 office CIDR blocks and has 60 inbound rules in total. You need to add 10 more office CIDRs, and the group is already at its rule cap. Describe a change that fixes this without requesting a quota increase, and say one thing to check when you create it.
Show answer
Create a customer-managed prefix list that holds the office CIDRs, with a maximum entry count that leaves room for growth, such as 30. Replace the 15 individual HTTPS rules with one rule that references the prefix list, then add the 10 new CIDRs to the list. This frees 14 rule slots and future changes happen in the list. When you create it, check that the list's maximum size, not its current entry count, is what counts against the group's rule quota, so choose a realistic maximum.
Exercise
An instance launch fails with a rule quota error, yet each of the three security groups attached to its network interface is well under 60 rules. What explains this, and where do you look?
Show answer
The rules from all attached groups apply to the interface together, and the product of the groups-per-interface quota and the rules-per-group quota is capped. The combined rule count across the three groups, including prefix lists counted at their maximum size, has passed what the interface allows. Check the Service Quotas values for both quotas, add up the effective rule counts of the groups, and consolidate rules with prefix lists or group references.