Wednesday, 25 November 2026

Difference between Private NAT Gateway and Public NAT Gateway ?

A NAT Gateway (Network Address Translation Gateway) is a managed AWS service that allows resources in a private subnet to connect out to the internet (for OS updates, patches, or external APIs) while blocking anyone on the internet from initiating connections back to them.

How It Works (Step-by-Step)
Because servers in a private subnet only have private IPs (like 10.0.11.25), the public internet cannot route traffic back to them directly. The NAT Gateway acts as a middleman:
  1. Outbound Request: Your private server (10.0.11.25) sends a request (e.g., apt-get update) to 0.0.0.0/0, which its route table points to the NAT Gateway.
  2. Translation: The NAT Gateway sits inside a Public Subnet and holds an Elastic IP (e.g., 54.12.34.56). It swaps your server's private IP for its own Elastic IP.
  3. Internet Access: It sends the request through the Internet Gateway to the internet.
  4. Return Traffic: When the response comes back to 54.12.34.56, the NAT Gateway remembers who asked for it and forwards the data back to 10.0.11.25.
  5. Inbound Block: If an external hacker tries to connect to 54.12.34.56 out of the blue, the NAT Gateway drops it because no private server started that conversation.

NAT Gateway Placement

A working NAT Gateway setup requires two separate route tables:
  • NAT Gateway Placement: Must be created inside a Public Subnet and attached to an Elastic IP.
  • Private Subnet Route Table:
    • 10.0.0.0/16 -> local
    • 0.0.0.0/0 -> nat-xxxx (NAT Gateway)
  • Public Subnet Route Table (where the NAT Gateway lives):
    • 10.0.0.0/16 -> local
    • 0.0.0.0/0 -> igw-xxxx (Internet Gateway)
Key Things to Know
  • One-Way Street: Outbound connections are allowed; unsolicited inbound connections are blocked.
  • Availability Zone Specific: A NAT Gateway lives in a single Availability Zone (AZ). For high availability in production, deploy one NAT Gateway per AZ so an outage in one zone doesn't cut internet access for private servers in another.
  • Cost Consideration: AWS charges an hourly rate (around $0.045/hr depending on region) plus per-GB data processing fees—making it one of the most common surprise charges in small or test AWS accounts if left running.


How Elastic IP address (EIP) is different from a public IPv4 address ?

An Elastic IP (EIP) is a static, permanent public IPv4 address that you allocate to your AWS account and keep until you explicitly release it.

An Elastic IP (EIP) address in Amazon Web Services (AWS) is a static, public IPv4 address designed for dynamic cloud computing. Unlike a default public IP address which changes every time an instance is stopped and started, an Elastic IP remains constant and is associated with your AWS account, not a specific instance. 

When you launch a server (like an EC2 instance) in a public subnet, AWS normally gives it a temporary public IP. That temporary IP changes if you stop and start the server. An Elastic IP solves this problem:
FeatureStandard Public IPElastic IP (EIP)
LifetimeLost when the instance is stopped or terminated.Stays in your AWS account until you release it.
OwnershipBorrowed dynamically from AWS's pool.Reserved specifically for your AWS account.
ReassignmentLocked to one instance for its run.Can be moved instantly from one instance to another.
Cost (AWS)Free while attached.Small hourly fee (~$0.005/hr) per public IPv4 address.

Common Use Cases
  • Failover & High Availability: If Server A crashes, you can instantly remap your Elastic IP to healthy Server B so users keep reaching your app without waiting for DNS changes.
  • IP Whitelisting: External partners or firewalls often require a fixed IP address from your side before allowing API or database connections.
  • NAT Gateways: A NAT Gateway in a public subnet requires an Elastic IP so private servers behind it exit to the internet using one predictable public IP.