AWS Certified CloudOps Engineer – Associate
Security groups vs network ACLs: stateful vs stateless
The Amazon VPC User Guide says a security group is stateful and attached to a resource: replies to allowed traffic pass automatically, and it has allow rules only. A network ACL is stateless and attached to a subnet: it has numbered allow and deny rules, evaluated from the lowest number, and replies need their own rule.
How does a security group behave?
A security group controls traffic to and from the resources it is associated with, such as an EC2 instance. You can write allow rules but not deny rules. A new security group has no inbound rules, so nothing gets in until you add one, and it has one outbound rule that allows all outbound traffic.
Security groups are stateful. Replies to allowed inbound traffic may leave regardless of the outbound rules, and replies to a request the instance sent may come back regardless of the inbound rules.
A rule's source or destination can be a single address, a CIDR range, a prefix list or another security group. When several security groups are attached to a resource, their rules are combined into one set.
How does a network ACL behave?
A network ACL allows or denies traffic at the subnet level. Every subnet is associated with exactly one network ACL, and one ACL can serve several subnets. Rules are evaluated when traffic enters or leaves the subnet, not when it moves inside the subnet.
Each rule has a number from 1 to 32766. Evaluation starts at the lowest number and stops at the first match, whatever a higher-numbered rule says. To carve an exception out of a deny, give the allow rule a lower number than the deny. AWS suggests numbering in steps of 10 or 100 to leave room.
Network ACLs are stateless. Allowing inbound traffic does not allow the reply. For every rule you add, there must be a rule in the other direction that allows the response traffic.
What are the defaults, and which ephemeral ports matter?
The default network ACL that comes with a VPC allows all traffic in and out. A custom network ACL you create has only the rule numbered with an asterisk in each direction, which denies any packet that matched nothing else, so it blocks everything until you add rules. The asterisk rule cannot be deleted.
Replies go to the ephemeral port the client chose, and the range depends on the client. Many Linux kernels use 32768-61000. Windows Server 2008 and later use 49152-65535. Elastic Load Balancing, NAT gateways and Lambda functions use 1024-65535.
If an instance in the subnet is the client, the ACL needs an inbound rule for that instance's ephemeral range. If the instance is the server, the ACL needs an outbound rule for the clients' range. In VPC flow logs, an ACCEPT record for a request followed by a REJECT record for its reply points at a network ACL, because a security group would have allowed the reply.
Where the exam catches people
- Associating a new custom network ACL and expecting traffic to pass: it denies everything until you add allow rules in both directions.
- Adding a more specific allow with a higher rule number than a deny: the lowest-numbered matching rule wins.
- Opening only the service port on a network ACL: the reply uses an ephemeral port and needs its own rule in the other direction.
- Looking for a deny rule in a security group: security groups support allow rules only.
- Believing a security group with no outbound rules blocks replies to allowed inbound requests: it is stateful, so replies leave anyway.