NAT Gateways, Security Groups and NACLs

Give private subnets outbound internet safely and control traffic with stateful security groups and stateless network ACLs.

Intermediate · about 5 hours · difficulty 3/5

Learn first: VPC, Subnets, Route Tables and Internet Gateways

Private instances still need to download updates and call external APIs. And every resource needs a firewall. This topic covers both.

NAT for outbound traffic

  • A NAT Gateway sits in a public subnet. Private subnets route 0.0.0.0/0 to it, so instances can reach out but nothing can connect in.
  • Deploy one NAT Gateway per AZ for high availability, or one shared gateway for cheap dev environments.
  • NAT Gateways cost money per hour and per GB processed. For labs, consider VPC endpoints (next topics) or a small NAT instance instead.

Security groups

  • Virtual firewalls attached to network interfaces (instances, load balancers, RDS, Lambda in a VPC).
  • Stateful: if inbound traffic is allowed, the reply is automatically allowed.
  • Allow rules only; anything not allowed is denied.
  • Rules can reference other security groups, for example "the database SG allows port 5432 only from the app SG". This is the pattern to use instead of IP ranges.

Network ACLs

  • Attached to subnets, evaluated in rule-number order.
  • Stateless: you must allow return traffic on ephemeral ports (1024-65535) explicitly.
  • Support deny rules, useful to block a known bad IP range.
  • Most teams leave NACLs permissive and do fine-grained control with security groups.

Pitfalls

  • Opening SSH (22) or RDP (3389) to 0.0.0.0/0. Use Systems Manager Session Manager instead.
  • Adding a deny NACL and forgetting the ephemeral port range, then wondering why responses never arrive.
  • Leaving a NAT Gateway running after a tutorial.

You are ready to move on when you can explain stateful vs stateless filtering and design a three-tier security group chain (ALB, app, database).

Key concepts

  • NAT Gateway
  • NAT per AZ vs shared
  • Security groups (stateful)
  • Security group referencing
  • Network ACLs (stateless)
  • Ephemeral ports
  • VPC Flow Logs: Debug which traffic was accepted or rejected

Mini project

In your lab VPC, create three security groups (alb-sg, app-sg, db-sg) where each only allows traffic from the previous tier. Add a NAT Gateway, confirm a private instance can run dnf update, then delete the NAT Gateway and release its Elastic IP within the hour to keep cost to a few rupees. Stay within free tier for the instance and terminate it afterwards.

Check yourself

Why must a NACL allow outbound ephemeral ports for a web server, while a security group does not need that rule?

Free resources