Catalog My learning Blog
Computer Networking for Developers: TCP, DNS, HTTP, TLS · Phase 3 · Lesson 21 · 3 min read

Network Security Best Practices for Production

On this page
  1. Defense in depth
  2. Audit: a security group with three classic mistakes
  3. Network security practices in priority order
  4. Checklist for a new service
  5. Check yourself

Defense in depth

Every real breach involves something going wrong: a leaked credential, an unpatched service, a rule left open "temporarily". Defense in depth means stacking independent controls so that any single failure is caught by the next layer. The earlier lessons gave you the pieces; this one puts them in order.

Defense in depth as rings of controls (perimeter, firewalls and TLS, segmentation, application checks) around the data.
Defense in depth as rings of controls (perimeter, firewalls and TLS, segmentation, application checks) around the data.

Audit: a security group with three classic mistakes

Here's an inbound rule set of the kind found in real audits, before and after:

Code
# BEFORE
SSH (22)           from 0.0.0.0/0
PostgreSQL (5432)  from 0.0.0.0/0
HTTPS (443)        from 0.0.0.0/0

# AFTER
SSH (22)           removed: use a session manager, or allow only the VPN range 10.0.99.0/24
PostgreSQL (5432)  from sg-app-servers (the app tier's security group)
HTTPS (443)        from the load balancer's security group only

What changed and why:

  • SSH open to the internet gets scanned within minutes of an instance launching. Better than restricting it: remove it, and use a cloud session manager or identity-aware proxy that logs every session.
  • A database open to the world is reachable by anyone who finds a weak or leaked password. It should accept only the application tier.
  • HTTPS directly on instances bypasses the load balancer's TLS settings, web application firewall and DDoS protection. Only the load balancer needs to reach them.

Network security practices in priority order

1. Private by default. Databases, caches, queues and internal services go in private subnets with no public IPs. Only load balancers and CDNs face the internet. Use private endpoints for cloud services so traffic to storage or queues doesn't cross the internet.

2. Least-privilege rules. Default deny (Day 18). Reference security groups, not wide IP ranges. Restrict outbound traffic too where you can: a compromised server that can't reach arbitrary internet hosts can't easily download tools or send data out.

3. Encrypt everything in transit. TLS for public traffic, and for internal traffic as well: databases, caches and service-to-service calls. A service mesh can provide mutual TLS, where both sides present certificates, so services authenticate each other.

4. Protect the edge. CDN and DDoS protection in front, a web application firewall for common attacks, rate limits on login and expensive endpoints (Day 20), and HSTS.

5. Log and watch. Enable flow logs (VPC Flow Logs or equivalent), load balancer access logs and DNS query logs. Alert on unusual patterns: a database talking to the internet, traffic on unexpected ports, a spike in denied connections.

6. Manage rules as code. Define security groups and firewall rules in Terraform or similar, review changes like code, and scan for risky patterns (0.0.0.0/0 on admin ports) in CI. Manual console changes drift and are forgotten.

Checklist for a new service

QuestionGood answer
Who can reach it?Only named security groups or services
Is it on the internet?Only through the load balancer or CDN
Is traffic encrypted?TLS on every hop, including internal
How do admins get in?Session manager or VPN, with logging
What can it reach outbound?Only what it needs
Would you notice an attack?Flow logs and alerts are on

Check yourself

It removes the open port entirely, ties access to identity, and records every session, while IP allowlists drift and can be spoofed or shared.

An attacker who gets a foothold inside, through one compromised server or container, can otherwise read credentials and data and alter internal traffic. Encryption with mutual authentication contains that and limits what one breach exposes.

Manage rules as code, with pull request review, and scan in CI with policy tools for patterns like admin or database ports open to 0.0.0.0/0. Back that up with a cloud configuration scanner for changes made outside the pipeline.

Several independent layers, such as edge filtering, segmentation, host firewalls, encryption and authentication, so one failed control doesn't expose everything.

Flow logs show who talked to whom, on which ports and when, which helps investigate incidents, spot unexpected connections like a database talking to the internet, and verify that segmentation works as designed.