# Security group rules: best practices

AWS configuration · Updated 17 November 2026

**Short answer:** Security groups are stateful allow-lists attached to network interfaces: new groups allow no inbound and all outbound traffic, and return traffic for allowed connections is permitted automatically. Good practice is to allow inbound traffic only from other security groups (load balancer to app, app to database) rather than CIDR ranges, never open SSH, RDP or database ports to 0.0.0.0/0, restrict outbound where the workload allows, use prefix lists for shared IP sets, and give every rule a description. Rules apply immediately to everything using the group.

## How rules behave

- **Allow only.** There are no deny rules; anything not allowed is dropped. Use network ACLs or AWS Network Firewall when you need to deny.
- **Stateful.** If inbound port 443 is allowed, responses go out regardless of outbound rules (and vice versa).
- **Aggregated.** Several groups on one interface combine into one set of allows.
- **Instant.** Adding, changing or removing a rule affects all associated resources immediately. Tracked connections may survive a rule removal until they close.
- **Limits.** 60 inbound and 60 outbound rules per group, 5 groups per interface by default; see [VPC limits](https://cloudconsole.dev/limits/vpc-limits).
- **DNS exception.** Security groups can't block queries to the Route 53 Resolver (the VPC +2 address); use DNS Firewall for that.

## Recommended rules by tier

| Tier | Inbound | Outbound |
|---|---|---|
| Public ALB | 443 (and 80 for redirect) from 0.0.0.0/0 and ::/0 | App port to the app security group |
| Application (ECS tasks, EC2) | App port from the **ALB's security group** | 443 to prefix lists or 0.0.0.0/0 via NAT; DB port to the DB group |
| Database (RDS) | 5432 or 3306 from the **app security group** only | None needed (stateful) |
| Interface VPC endpoints | 443 from the VPC CIDR or app groups | - |
| Admin access | None: use Session Manager instead of SSH or RDP | - |

Referencing a security group means "any interface that has that group", so scaling, IP changes and new subnets need no rule updates and the rule count stays small.

## Practices that prevent incidents

1. **No admin ports open to the internet.** 22, 3389, 3306, 5432, 6379 and 9200 from 0.0.0.0/0 are the classic findings. Use [Session Manager](https://cloudconsole.dev/how-to/connect-to-ec2-with-session-manager) for shell access.
2. **Describe every rule** (`Description` field): who asked for it and why. Undescribed rules never get removed.
3. **Restrict egress for sensitive tiers.** The default allow-all egress makes data exfiltration easy; databases and internal services rarely need it.
4. **Use prefix lists** for office or partner IPs, sized to what you need: a reference counts as the list's maximum entries against the rules quota.
5. **Separate groups per role**, not one shared "default-app" group used by everything.
6. **Don't use the VPC default security group** for workloads; remove its rules so nothing uses it by accident.
7. **Manage rules in IaC** and review changes like code; detect drift with AWS Config or Security Hub controls that flag unrestricted ingress.

## Change and audit

```
# Allow the app tier from the ALB's security group only
aws ec2 authorize-security-group-ingress --group-id sg-0app0000000000000 \
  --ip-permissions 'IpProtocol=tcp,FromPort=8080,ToPort=8080,UserIdGroupPairs=[{GroupId=sg-0alb0000000000000,Description="from public ALB"}]'

# Groups with an inbound rule open to the world
aws ec2 describe-security-groups --filters Name=ip-permission.cidr,Values=0.0.0.0/0 \
  --query 'SecurityGroups[].[GroupId,GroupName,VpcId]' --output table

# Who uses a group before you change it
aws ec2 describe-network-interfaces --filters Name=group-id,Values=sg-0db00000000000000 \
  --query 'NetworkInterfaces[].[NetworkInterfaceId,Description]' --output table
```

**CloudFormation:** `AWS::EC2::SecurityGroupIngress` (`GroupId`, `IpProtocol`, `FromPort`, `ToPort`, `SourceSecurityGroupId` or `CidrIp`, `Description`) or inline `SecurityGroupIngress` on `AWS::EC2::SecurityGroup`. **Terraform:** `aws_vpc_security_group_ingress_rule` and `aws_vpc_security_group_egress_rule` with `security_group_id`, `ip_protocol`, `from_port`, `to_port`, and `referenced_security_group_id`, `cidr_ipv4` or `prefix_list_id`.

## Symptoms of a wrong rule

- **Connection timeouts** (not refusals) usually mean a security group or NACL is dropping traffic; check both sides, including the target's inbound and any restricted egress on the source.
- **`RulesPerSecurityGroupLimitExceeded`** when adding rules: consolidate with group references or prefix lists.
- **Stale rules** referencing deleted groups in peered VPCs: find them with `describe-stale-security-groups`.

## Questions

### Are AWS security groups stateful?

Yes. Return traffic for an allowed connection is permitted automatically, so you only need rules for the direction the connection is opened.

### Should I remove the default outbound rule from a security group?

For sensitive tiers such as databases, yes: they rarely need outbound access. For application tiers, replace allow-all with the destinations they actually need where practical.

### Can a security group deny traffic?

No. Security groups only contain allow rules. Use network ACLs or AWS Network Firewall for explicit denies.

## When this happens away from your desk

A security group change can open a port to the world or cut off a database in seconds. CloudConsole, a phone app for AWS, is being built to show security findings and alarms on your phone so either is caught quickly.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [Security group rules](https://docs.aws.amazon.com/vpc/latest/userguide/security-group-rules.html)
- [Security best practices for your VPC](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-security-best-practices.html)
- [Amazon VPC quotas](https://docs.aws.amazon.com/vpc/latest/userguide/amazon-vpc-limits.html)

## Related

- [Amazon VPC limits and quotas](https://cloudconsole.dev/limits/vpc-limits)
- [aws ec2 authorize-security-group-ingress](https://cloudconsole.dev/cli/ec2-authorize-security-group-ingress)
- [aws ec2 revoke-security-group-ingress](https://cloudconsole.dev/cli/ec2-revoke-security-group-ingress)
- [aws ec2 describe-security-groups](https://cloudconsole.dev/cli/ec2-describe-security-groups)
- [VPC gateway endpoints for S3 and DynamoDB](https://cloudconsole.dev/config/vpc-gateway-endpoints)
- [How to connect to EC2 with Session Manager](https://cloudconsole.dev/how-to/connect-to-ec2-with-session-manager)

---

Source: https://cloudconsole.dev/config/security-group-rules

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
