1.Security groups: stateful, instance-level firewalls
A security group is a stateful firewall attached to a resource (an instance, a load balancer, an RDS database) — it can only allow traffic, never explicitly deny, and once inbound traffic is allowed, the matching return traffic is automatically allowed back out.
Key features
- Attached to the resource itself (ENI-level) — not to a subnet
- Allow-only — there is no explicit "deny" rule in a security group
- Stateful — allowed inbound traffic's response is automatically allowed outbound, and vice versa
- A resource can have multiple security groups; the union of all rules applies
- A security group can reference another security group as its source, not just an IP range
- Inbound rule added
- Allow TCP 443 from sg-alb (the load balancer's security group)
- A request from that source on port 443 is let through
- No outbound rule needed for the reply
- The response to that same connection is automatically allowed out
- No matching outbound rule had to be added by hand
Command
aws ec2 authorize-security-group-ingress --group-id sg-0abc123 \
--protocol tcp --port 443 --source-group sg-0alb456Security groups are stateful and allow-only, attached to the resource itself — return traffic for an allowed connection is automatic, and there is no explicit deny.