Lesson 9 of 11 · 12 min · AWS networking
Security groups, NACLs, load balancers and Route 53
What you will learn
- Security groups: rules, statefulness, referencing other groups
- Network ACLs: rule numbers, allow and deny, statelessness
- When to use which, and how they combine
- ALB, NLB and the other load balancers, with listener rules and health checks
- Route 53: hosted zones, record types, alias records and routing policies
In simple words
Once the network exists, you decide who may come in and how visitors find you. Security groups and network ACLs are two layers of guards. A load balancer spreads visitors across your servers. Route 53 is AWS's phone book, pointing your domain name at the right place.
Words to know
- Security group: a firewall around a single machine. It remembers connections, so replies are allowed back.
- Network ACL: a firewall around a whole subnet. It checks every packet in each direction.
- Load balancer (ALB / NLB): spreads requests over several servers. ALB understands web requests; NLB works on raw connections.
- Route 53: AWS's DNS service.
Two layers of firewall
The Networking course introduced firewalls generally. AWS has two VPC-level layers, and a packet must pass both:
| Security group (SG) | Network ACL (NACL) | |
|---|---|---|
| Attached to | instance / network interface (ENI) | subnet |
| State | stateful: replies to allowed traffic are allowed automatically | stateless: return traffic needs its own rule |
| Rules | allow only; everything not allowed is denied | allow and deny, evaluated in order by rule number, first match wins |
| Sources | CIDRs, prefix lists, other security groups | CIDRs only |
| Default | new SG: no inbound, all outbound | default NACL: allow all; custom NACL: deny all |
| Typical use | the main, fine-grained control | coarse subnet guard rails, explicit deny of bad ranges |
Security groups
Rules specify protocol, port range and source/destination. Create a web SG allowing HTTPS from anywhere, and a database SG allowing PostgreSQL only from the web SG:
SG=$(aws ec2 create-security-group --group-name web --description web --vpc-id $VPC --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id $SG --protocol tcp --port 443 --cidr 0.0.0.0/0
SGDB=$(aws ec2 create-security-group --group-name db --description db --vpc-id $VPC --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id $SGDB --protocol tcp --port 5432 --source-group $SG
We read back the database rule: 5432 sg-1baf8085328781755 (the web group's id as source). And a new group's default egress was -1 0.0.0.0/0 (all protocols to anywhere).
Why the source-group reference matters: "allow 5432 from members of group web" stays correct as instances come and go, scale out, change IPs and move subnets, with no CIDR bookkeeping. Layered groups express your architecture:
internet ──443──▶ [alb-sg] ──8080──▶ [app-sg] ──5432──▶ [db-sg]
alb-sgallows 80/443 from0.0.0.0/0.app-sgallows 8080 from alb-sg.db-sgallows 5432 from app-sg.
An instance can have several groups (rules are unioned). Changes apply immediately to running instances. Because SGs are stateful, you do not open ephemeral ports for replies. Egress rules default to open: restrict them in sensitive tiers (database tier: no egress except what is needed). Never open SSH/RDP to 0.0.0.0/0; prefer Session Manager (no inbound ports at all) or restrict to a known range. Reference security group IDs from Terraform (source_security_group_id / referenced_security_group_id).
Limits worth knowing: quotas on rules per group and groups per interface; an SG belongs to one VPC; deleting a group still referenced by another fails (dependency).
Network ACLs
NACLs filter at the subnet boundary, with numbered rules for inbound and outbound separately. Rule 100 is checked before 110; the first match decides; a final * rule denies everything unmatched. We created one:
aws ec2 create-network-acl-entry --network-acl-id $NACL --rule-number 100 --protocol tcp --port-range From=443,To=443 --cidr-block 0.0.0.0/0 --rule-action allow --ingress
aws ec2 create-network-acl-entry --network-acl-id $NACL --rule-number 110 --protocol tcp --port-range From=22,To=22 --cidr-block 203.0.113.0/24 --rule-action deny --ingress
100 allow 0.0.0.0/0
110 deny 203.0.113.0/24
Because NACLs are stateless, if you allow inbound 443 you must also allow the outbound ephemeral ports (1024-65535) for the responses, or connections hang. This is the classic NACL mistake. Because of that complexity, most teams keep the default "allow all" NACLs and do their filtering with security groups, and use NACLs for specific needs: blocking a hostile IP range for a whole subnet, a compliance requirement for subnet-level controls, or an extra layer for the data tier. Number rules with gaps (100, 200, 300) so you can insert rules later.
Debug order for a blocked connection: route table, security group (both ends), NACL (both directions), OS firewall, application bind address, then DNS (Networking and VPC lessons).
Elastic Load Balancing
Load balancing concepts (algorithms, health checks, layer 4 vs 7) are in the Networking course. AWS offers:
| Type | Layer | Use |
|---|---|---|
| Application Load Balancer (ALB) | 7 (HTTP/HTTPS/gRPC) | web apps and APIs: host/path/header routing, TLS termination (ACM certificates), redirects, authentication (OIDC/Cognito), WAF integration, targets: instances, IPs, Lambda, containers |
| Network Load Balancer (NLB) | 4 (TCP/UDP/TLS) | extreme performance, static IPs per AZ, non-HTTP protocols, PrivateLink services; preserves client IP |
| Gateway Load Balancer | 3 | inline security appliances (firewalls, inspection) |
| Classic LB | legacy | avoid for new work |
An ALB is built from:
- Listeners (port + protocol: 80, 443) with rules: conditions (host
api.shop.example, path/static/*, header) → actions (forward to target group, redirect HTTP→HTTPS, fixed response). - Target groups: the pools (instances, IPs, Lambda) with a health check (path, interval, healthy/unhealthy thresholds), deregistration delay (connection draining), stickiness and algorithm (round robin or least outstanding requests).
- Deployed in at least two public subnets in different AZs; its security group allows 80/443 from the internet; targets' security groups allow traffic only from the ALB's group.
- Access logs to S3, metrics to CloudWatch (
HTTPCode_Target_5XX_Count,TargetResponseTime,UnHealthyHostCount).
Typical settings: redirect 80→443, an ACM certificate on the 443 listener (free, auto-renewing), the targets' keep-alive timeout set longer than the ALB's idle timeout (60 seconds by default); if the target closes idle connections first, the ALB may send a request on a connection that is already closed and return sporadic 502 errors, WAF on the ALB for common attacks, deletion protection on.
Troubleshooting ELB: 502 the target closed the connection or sent an invalid response; 503 no healthy targets; 504 target timeout. Look at target health reasons in the console/CLI (aws elbv2 describe-target-health), security group rules between ALB and targets, health check path/port, and application logs.
Route 53
Amazon Route 53 is AWS's DNS service (highly available, global). The DNS course concepts apply directly.
- A hosted zone holds the records of a domain (public zone for the internet, private zone for names visible only inside associated VPCs). Delegate by setting the zone's four NS records at your registrar.
- Record types: A, AAAA, CNAME, MX, TXT, NS... plus alias records, a Route 53 extension: an A/AAAA record that points to an AWS resource (ALB, CloudFront distribution, S3 website, another record) with no extra charge for queries, works at the zone apex (where CNAME is not allowed), and follows the resource's changing IPs automatically.
- Routing policies decide which record answers:
| Policy | Behaviour | Use |
|---|---|---|
| Simple | one record | basic |
| Weighted | split by weights | canary / blue-green (CI/CD course), A/B tests |
| Latency | lowest latency region | multi-region apps |
| Failover | primary, secondary on failed health check | active-passive DR |
| Geolocation / geoproximity | by user location | compliance, localisation |
| Multivalue answer | several healthy IPs | simple client-side balancing |
We created two weighted records for app.example.test:
app.example.test. blue 90 60
app.example.test. green 10 60
(name, set identifier, weight, TTL). Ninety percent of lookups answer with the blue address and ten percent with green. The 60-second TTL limits how long caches keep an answer, which controls how quickly traffic shifts (DNS course: lower TTL before changes). Health checks (HTTP/TCP/CloudWatch alarm based) remove unhealthy records from answers automatically.
Also: Route 53 Resolver for hybrid DNS (forwarding to on-premises resolvers), DNSSEC support, domain registration. DNS is global and cached, so it is the slowest tool for traffic shifting; load balancers shift faster.
Putting it together: the request path and the rules
user ─▶ Route 53 (alias) ─▶ ALB (public subnets, alb-sg: 443 from internet)
└─▶ targets in private subnets (app-sg: 8080 from alb-sg)
└─▶ RDS (db-sg: 5432 from app-sg)
Everything between tiers is expressed with security-group references, not IP addresses.
Common mistakes
- Opening SSH/RDP/database ports to
0.0.0.0/0. - Using CIDR ranges between tiers instead of security group references.
- Stateless NACL rules that forget the return ports.
- Contradicting rules ordering in NACLs (a broad allow before a specific deny).
- Targets that allow traffic from anywhere instead of only from the load balancer's group.
- ALB in a single AZ, or health check on a path requiring login (always unhealthy).
- CNAME at the zone apex (use alias).
- High TTL before a cut-over.
Try it yourself
- Create ALB, app and DB security groups in a VPC where each allows traffic only from the previous layer's group. Draw the rules.
- Add a NACL that blocks a test range and explain what happens to return traffic if you forget the ephemeral ports.
- Create an ALB with two target groups and path-based rules (
/api/*to one, default to the other), plus an HTTP→HTTPS redirect. - In Route 53, create two weighted records, query them repeatedly with
dig +short, and confirm the distribution; add a health check.
Check yourself
What does "stateful" mean for security groups?
If a connection is allowed in, the reply traffic is automatically allowed out (and vice versa), so you don't need separate rules for return traffic.
Why reference a security group as a source instead of a CIDR?
Membership follows instances as they scale and change IPs; the rule stays correct and expresses intent ("the app tier may talk to the database").
Why do NACLs need ephemeral port rules?
NACLs are stateless; responses go to high-numbered client ports and must be explicitly allowed in the opposite direction.
ALB versus NLB?
ALB is layer 7 (HTTP routing by host/path, TLS, WAF); NLB is layer 4 (TCP/UDP) with very high throughput, static IPs and client IP preservation.
Interview questions
How do you restrict database access to the application tier only?
Put the database in private subnets, give it a security group allowing the database port only from the application's security group, no public IP, and optionally NACLs and endpoint policies as extra layers.
Users get 503 from the ALB. What do you check?
Target health (all unhealthy / none registered), health check path and port, security groups between ALB and targets, whether the application is running and listening, recent deployments, and capacity limits.
How would you do a canary release using Route 53?
Weighted records for the old and new endpoints (for example 95/5), a low TTL, health checks, and gradual weight changes while monitoring; though load balancer weighted target groups shift faster than DNS.
Summary
- Security groups are stateful allow-lists on instances, best used with group-to-group references; NACLs are stateless, ordered allow/deny lists on subnets.
- ALB for HTTP routing, NLB for TCP/UDP performance; target groups with health checks, in at least two AZs, with targets accepting traffic only from the LB's group.
- Route 53 provides public/private zones, alias records (also at the apex) and routing policies (weighted, latency, failover, geo).
- Troubleshoot in order: routes → security groups → NACLs → OS → app → DNS.
Going deeper
- Security groups: rules, referencing and quotas.
- Network ACLs: rule evaluation and ephemeral ports.
- Application Load Balancers: listeners, rules and target groups.
- Network Load Balancers: layer 4 load balancing.
- Amazon Route 53 developer guide: zones, records and routing policies.
- Choosing a routing policy: all Route 53 routing policies.
See how much of this you can explain
A free mock interview asks you new questions on Cloud platforms and AWS fundamentals for DevOps Engineer and scores every answer, with what to fix.