Securing a VPC with Network ACLs and Security Groups
How to secure an AWS VPC with security groups and network ACLs: stateful vs stateless rules, rule order, ephemeral ports, a three-tier design and testing.
You secure a VPC by using security groups and network ACLs together. Security groups are stateful, allow-only firewalls attached to resources such as EC2 instances. Network ACLs are stateless, numbered allow-and-deny rule lists attached to subnets. Use security groups for precise per-application access and network ACLs as a coarse subnet guardrail.
This guide explains how each control evaluates traffic, how to design rules for a typical three-tier application, and which mistakes cause most outages and exposures. It assumes you know what a VPC and a subnet are. If you need that background first, read what an AWS VPC is and how it secures resources. All behaviour described here was checked against the Amazon VPC User Guide in November 2026.
Key takeaways
- Security groups are stateful: if inbound traffic is allowed, the response leaves automatically, whatever the outbound rules say.
- Network ACLs are stateless: return traffic needs its own rule, which usually means allowing the ephemeral port range.
- Network ACL rules are numbered from 1 to 32766 and evaluated lowest first. The first match wins and later rules are ignored.
- Security groups only have allow rules. Network ACLs have both allow and deny rules, so they are the place for explicit blocks.
- Every subnet is associated with exactly one network ACL at a time. A resource can have several security groups.
- Neither control filters traffic to Amazon DNS, DHCP, instance metadata or the Amazon Time Sync Service. Use Route 53 Resolver DNS Firewall for DNS filtering.
Where each control sits in the traffic path
When a packet enters a VPC from the internet, a VPN, or a peered VPC, the VPC router sends it toward its destination subnet. Before it enters that subnet, the subnet's network ACL checks it against the inbound rules. If the ACL lets it through, the packet reaches the elastic network interface of the target resource, where the security groups attached to that interface check it again. On the way out, the order reverses: security group outbound rules first, then the subnet's outbound ACL rules.
One detail surprises many people. AWS evaluates network ACL rules when traffic enters and leaves a subnet, not when it moves between two instances inside the same subnet. Two servers in one subnet talk to each other with only their security groups between them. If you were counting on the ACL to isolate them, it will not. Put workloads that must be isolated in separate subnets, or rely on security group rules.
Both controls are free. AWS charges nothing for security groups or network ACLs, so there is no cost reason to skip either.
Security groups vs network ACLs at a glance
| Property | Security group | Network ACL |
|---|---|---|
| Attached to | Network interfaces of resources (EC2, RDS, load balancers, Lambda in a VPC) | Subnets |
| State | Stateful, return traffic allowed automatically | Stateless, return traffic needs a rule |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules considered together | Lowest number first, first match wins |
| Default for a new custom one | No inbound allowed, all outbound allowed | Denies all inbound and outbound until you add rules |
| Can reference other groups | Yes, a source can be another security group | No, CIDR blocks only |
| How many per target | Several per resource | One per subnet at a time |
How security groups work
A security group is a set of allow rules. Each inbound rule names a protocol, a port range, and a source. Each outbound rule names a protocol, a port range, and a destination. Traffic that matches no rule is dropped. You cannot write a deny rule, so the only way to block something is to not allow it.
Because security groups are stateful, they track connections. When a web server accepts an HTTPS request on port 443, the reply goes back to the client's high-numbered port without any outbound rule for it. The same applies in reverse: if the instance starts a connection to a package repository, the response is let in even if no inbound rule matches it.
The feature that makes security groups so useful for application design is that a rule's source can be another security group rather than an IP range. If your application servers carry a group called app-sg, the database group can allow port 5432 from app-sg. Any instance that later joins app-sg gets access, and nothing else does, even if IP addresses change during scaling. This is far easier to maintain than lists of private IP addresses.
A few rules from the AWS documentation are worth knowing by heart. A new VPC comes with a default security group. A security group name must be unique within the VPC, cannot start with sg-, and is not case-sensitive. You can attach more than one security group to a resource, and the effective permission is the union of all their allow rules. You can also associate a security group with other VPCs in the same Region through the Security Group VPC Association feature, and share groups across accounts with AWS Organizations.
Security group best practices from AWS
- Allow only specific IAM principals to create and modify security groups.
- Create as few groups as you need, one per role or tier, to reduce the chance of error.
- Never open SSH (22) or RDP (3389) to
0.0.0.0/0or::/0. Use specific address ranges, or better, use Systems Manager Session Manager so no inbound port is needed at all. - Avoid large port ranges. Open only the ports a service actually listens on.
How network ACLs work
A network ACL is an ordered list of numbered rules for inbound traffic and another for outbound traffic. Each rule allows or denies a protocol, port range, and CIDR block. AWS reads the rules from the lowest number upward and stops at the first match. Every ACL also ends with an asterisk rule that denies anything not matched, and you cannot delete it.
The VPC's default network ACL allows all inbound and outbound traffic, which is why most new VPCs work without anyone touching ACLs. A custom network ACL you create is the opposite: until you add rules, it denies everything in both directions. Associating a fresh custom ACL with a production subnet before writing its rules is a quick way to cause an outage.
AWS recommends numbering rules in steps of 10 or 100. If your rules are 100, 200, 300, you can later insert rule 150 to deny a specific address range ahead of a broader allow at 200. Since order decides the outcome, a deny at rule 300 will never stop traffic that an allow at rule 200 already matched.
The ephemeral port problem
Statelessness is where most ACL mistakes come from. Suppose a public subnet holds a web server and the inbound ACL allows TCP 443 from anywhere. The client connects from a random high port on its own machine. The server's reply goes out to that port. If the outbound ACL only allows 443, the reply is dropped and the website looks down even though the security group is correct.
The fix is an outbound rule that allows the ephemeral port range back to clients. The range a client uses depends on its operating system, so many teams allow TCP 1024 to 65535 outbound to 0.0.0.0/0 on public subnets. The same logic applies inbound: if instances in a private subnet call out to the internet through a NAT gateway, the inbound ACL must allow the returning responses on ephemeral ports.
What network ACLs cannot block
The VPC documentation lists traffic that neither ACLs nor security groups filter: Amazon-provided DNS (the Route 53 Resolver at the VPC base address plus two), Amazon DHCP, EC2 instance metadata, ECS task metadata endpoints, Windows license activation, the Amazon Time Sync Service, and reserved addresses used by the default VPC router. If you need to control DNS lookups, use Route 53 Resolver DNS Firewall. To control access to instance metadata, configure the instance metadata options, ideally requiring IMDSv2.
A worked design for a three-tier application
Take a common layout: an Application Load Balancer in public subnets, application servers in private subnets, and a PostgreSQL database in isolated data subnets. If you are still planning the subnet layout, our guide on designing public and private subnets covers the CIDR side.
Security groups for each tier
| Group | Inbound rule | Source |
|---|---|---|
| alb-sg | TCP 443 | 0.0.0.0/0 and ::/0 |
| app-sg | TCP 8080 | alb-sg |
| db-sg | TCP 5432 | app-sg |
Nothing on the internet can reach the application servers directly, because app-sg only trusts the load balancer group. The database trusts only the application group. Adding servers through an Auto Scaling group needs no rule changes. You can create the chain with the AWS CLI:
aws ec2 authorize-security-group-ingress \
--group-id sg-0db0000000000000 \
--protocol tcp --port 5432 \
--source-group sg-0app000000000000
Tighten outbound rules where it matters. The default outbound rule allows all traffic, which is convenient but lets a compromised server call anywhere. For the database tier, replacing it with no outbound rules at all is often fine, because replies to allowed inbound connections still leave thanks to statefulness.
Network ACLs as a subnet guardrail
For the data subnets, a custom ACL can make the intent explicit and catch mistakes in security groups. Inbound, allow TCP 5432 from the private application subnet CIDRs at rule 100, and let the default deny handle everything else. Outbound, allow TCP 1024 to 65535 to the same CIDRs at rule 100 so replies return. Even if someone later attaches an overly open security group to the database, the subnet still refuses traffic from anywhere but the application tier.
In public subnets, keep ACLs simple: allow 443 inbound, ephemeral ports outbound and inbound as needed, and add specific deny rules with low numbers when you need to block a known bad address range quickly. That fast block is the main job security groups cannot do, since they have no deny rule.
Testing and monitoring your rules
Rules that nobody checks drift. Three AWS tools help you see what is actually happening.
VPC Flow Logs record accepted and rejected traffic for a VPC, subnet, or network interface, and can be sent to CloudWatch Logs or S3. When a connection fails, a flow log entry with REJECT tells you the packet was dropped. If you see an inbound ACCEPT followed by an outbound REJECT for the same flow, the stateless ACL is blocking the reply, which almost always means a missing ephemeral port rule.
VPC Reachability Analyzer checks the configuration path between two resources without sending packets and tells you which component blocks the path, whether that is a route table, an ACL, or a security group. It is useful before a change window to confirm that a new rule set does what you expect.
AWS Config can evaluate security groups continuously against managed rules, for example flagging groups that allow unrestricted SSH. Pair it with CloudTrail so every change to a security group or ACL is recorded with who made it. Monitoring S3 and other services with CloudWatch follows the same alarm pattern; our post on CloudWatch metrics and alarms shows how to wire alerts.
Managing rules as code
Clicking rules into the console works for a lab, but production rules belong in Terraform or CloudFormation. Code review catches a 0.0.0.0/0 on port 22 before it ships, and the repository becomes the record of why each rule exists. Here is a Terraform sketch of the database group, using the standalone ingress rule resource from the AWS provider:
resource "aws_security_group" "db" {
name = "db-sg"
vpc_id = aws_vpc.main.id
}
resource "aws_vpc_security_group_ingress_rule" "db_from_app" {
security_group_id = aws_security_group.db.id
referenced_security_group_id = aws_security_group.app.id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
}
Policy as code tools such as Checkov, Trivy, or Open Policy Agent can then fail the pipeline when a plan opens a sensitive port to the world. Keeping ACL rule numbers in code also prevents two engineers from picking the same number in a hurry.
Common mistakes and how to avoid them
- Forgetting ephemeral ports on a custom ACL. Symptom: TCP handshakes time out although security groups look right. Check flow logs for outbound rejects.
- Placing a deny after a broader allow. The ACL never reaches it. Give denies lower numbers.
- Assuming the ACL isolates instances in one subnet. It does not see that traffic.
- Using IP-based rules between tiers. They break when instances are replaced. Reference security groups instead.
- Leaving SSH open "temporarily". Use Session Manager and remove port 22 entirely.
- Associating a new custom ACL before adding rules. It denies everything by default.
- Relying on ACLs to block DNS exfiltration. They cannot filter Route 53 Resolver traffic; use DNS Firewall.
Where these controls fit in a zero trust design
Network rules are one layer. A zero trust approach assumes the network can be breached and adds identity checks on every request: IAM policies on AWS APIs, TLS between services, and application-level authentication. Security groups that reference each other get you close to identity-based network rules, because membership in a group stands in for "this is an application server". For traffic between VPCs, see how VPC peering interacts with security group references, and for outbound internet access from private subnets, read why NAT gateways matter. If you want a deeper comparison of when to favour one control, our companion post explains why security groups are usually preferred for application protection.
For larger estates, AWS Network Firewall adds stateful inspection and domain filtering at the VPC edge, and AWS Firewall Manager can push security group policies across accounts. Those services complement, rather than replace, well-designed security groups and ACLs.
VPC design, security groups and network ACLs are core topics in the AWS Solutions Architect Associate exam. Our AWS Solutions Architect Associate training, run in the Kharadi, Pune classroom and live online, includes hands-on labs building exactly this three-tier layout.
Frequently asked questions
What is the main difference between a security group and a network ACL?
A security group is stateful, attaches to resources, and only allows traffic. A network ACL is stateless, attaches to subnets, supports allow and deny rules, and evaluates numbered rules in order. Return traffic is automatic with security groups but needs explicit rules with ACLs.
Do I need both security groups and network ACLs?
Security groups are required on every resource and do most of the work. Network ACLs are optional beyond the default one, but a custom ACL adds a subnet-level guardrail and lets you deny specific address ranges, which security groups cannot do.
Why does my website time out when the security group allows port 443?
The usual cause is a custom network ACL that allows 443 inbound but does not allow outbound traffic on ephemeral ports, so replies are dropped. Check VPC Flow Logs for outbound REJECT entries and add an outbound rule for TCP 1024 to 65535.
In what order are network ACL rules evaluated?
From the lowest rule number to the highest, using numbers from 1 to 32766. The first rule that matches is applied and the rest are ignored. A final asterisk rule denies anything that matched nothing.
Can a security group deny traffic?
No. Security groups contain only allow rules. Anything not allowed is dropped. To block a specific IP range explicitly, use a deny rule in the subnet's network ACL.
Can network ACLs filter traffic between instances in the same subnet?
No. Network ACLs are evaluated when traffic enters or leaves a subnet. Traffic between instances in the same subnet is controlled only by their security groups.
Can security groups or ACLs block DNS queries?
No. Neither filters traffic to the Amazon-provided DNS resolver. AWS recommends Route 53 Resolver DNS Firewall for filtering DNS queries from a VPC.
What does a new custom network ACL allow by default?
Nothing. A custom network ACL denies all inbound and outbound traffic until you add rules. The default ACL that comes with a VPC allows all traffic.
References
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0