Create NAT Gateway

Create a NAT Gateway

ℹ️ NAT Gateway Overview

A NAT Gateway (Network Address Translation Gateway) is a managed AWS service that lets instances in a private subnet:

  • Connect out to the internet to download updates and packages
  • Access AWS services
  • Block inbound connections initiated from the internet

🏗️ NAT Gateway Architecture:

EC2 Private → Private Subnet → NAT Gateway → Internet Gateway → Internet
                                    ↓
                              (Elastic IP)

💰 NAT Gateway Cost:

  • Hourly charge: $0.045/hour ($32/month)
  • Data processing charge: ~$0.045/GB
  • Elastic IP: free while in use, $0.005/hour when unused

💡 Note for This Workshop:

  • We’ll create a single NAT Gateway (simple and cost-effective)
  • In production, you should have a NAT Gateway in every AZ for high availability

Create an Elastic IP Address

  1. Access the EC2 Dashboard

    • Select Elastic IPs from the left menu (under Network & Security)
    • Click Allocate Elastic IP address

    💡 What is an Elastic IP?: A static IPv4 address designed for cloud computing. A NAT Gateway needs an Elastic IP to have a fixed public address.

    Create VPC

  2. Configure the Elastic IP

    • Network Border Group: Keep the default (us-west-1)
    • Public IPv4 address pool: Select Amazon’s pool of IPv4 addresses
    • Tags (optional): You can add a Name tag = “NAT-Gateway-EIP”
    • Click Allocate

    Create VPC

  3. Confirm the Elastic IP was created

    • Note the Allocated IPv4 address
    • Current status: Not associated (not yet attached to a resource)

    ⚠️ Note: An unused Elastic IP is charged $0.005/hour. We’ll attach it to the NAT Gateway right away.

    Create VPC

Deploy the NAT Gateway

  1. Access the VPC Dashboard

    • Select NAT Gateways from the left menu
    • Click Create NAT gateway

    Create VPC

  2. Configure the NAT Gateway

    • Name: Enter NAT gateway
    • Subnet: Select Public subnet 2 (or any public subnet)
    • Connectivity type: Select Public
    • Elastic IP allocation ID: Select the Elastic IP you just created

    💡 Why place the NAT Gateway in a public subnet?

    • The NAT Gateway needs an Internet Gateway to reach the internet
    • Only a public subnet has a route to the Internet Gateway
    • The NAT Gateway will use the Elastic IP as its public address

    Create VPC

  3. Review the configuration and click Create NAT gateway

    Create VPC

  4. Confirm the NAT Gateway was created

    • Initial status: Pending
    • After 1-2 minutes, the status changes to Available

    ⏱️ Wait time: The NAT Gateway takes about 1-2 minutes to initialize. You can continue with the next steps while you wait.

    Create VPC

Configure a Route Table for the Private Subnet

💡 Why do we need a separate route table?

  • A private subnet needs different routing than a public subnet
  • Traffic from the private subnet must go through the NAT Gateway (not the Internet Gateway)
  • Separating the routing improves security and makes management easier
  1. In the VPC Dashboard

    • Select Route Tables from the left menu
    • Click Create route table

    Create VPC

  2. Set up the route table

    • Name: Enter Route table - Private
    • VPC: Select the ASG VPC
    • Click Create route table

    Create VPC

  3. Confirm the route table was created

    • Note the Route table ID
    • Status: Active

Associate Subnets with the Route Table

  1. In the route table you just created

    • Select the Subnet associations tab
    • Click Edit subnet associations

    💡 Subnet Associations: Determine which subnets use this route table to route their traffic.

    Create VPC

  2. Associate the private subnets

    • Select both private subnets:
      • Private subnet 1
      • Private subnet 2
    • Click Save associations

    ℹ️ Note: Select only the private subnets. Public subnets already have their own route table with a route to the Internet Gateway.

    Create VPC

Add a Route to the NAT Gateway

  1. Configure routes

    • Select the Routes tab
    • Click Edit routes

    Create VPC

  2. Add a route for internet access

    • Click Add route
    • Destination: Enter 0.0.0.0/0 (all traffic to the internet)
    • Target: Select NAT Gateway, then choose the NAT Gateway you just created
    • Click Save changes

    💡 Route explanation:

    • 0.0.0.0/0: Represents all IP addresses (the internet)
    • Target = NAT Gateway: All outbound internet traffic goes through the NAT Gateway
    • The NAT Gateway translates the private IP to the Elastic IP before the traffic reaches the internet

    Create VPC

  3. Confirm the route table configuration

    • Check that the route table has 2 routes:
      • Local route: Allows communication within the VPC
      • 0.0.0.0/0 → NAT Gateway: Allows internet access

    ✅ Complete route table:

    Destination       Target
    10.10.0.0/16     local          (VPC internal traffic)
    0.0.0.0/0        nat-xxxxx      (Internet traffic)
    

    Create VPC

Check the Internet Connection

  1. Return to your SSH session on EC2 Private

    • If the session was closed, SSH back in from EC2 Public:
    ssh -i aws-keypair.pem ec2-user@<EC2-Private-IP>
    
  2. Run a ping test from the EC2 Private instance

    • Test the connection to Amazon:
    ping amazon.com -c5
    

    ✅ Expected result: You should receive 5 successful ping responses, confirming EC2 Private can now reach the internet through the NAT Gateway.

    Create VPC

  3. Additional tests (optional):

    # Test DNS resolution
    nslookup google.com
    
    # Test HTTP connection
    curl -I https://aws.amazon.com
    
    # Update system packages (requires internet)
    sudo yum update -y
    

🏗️ Complete Architecture:

Internet
   ↕
Internet Gateway (IGW)
   ↕
Public Subnet 2
   ↕
NAT Gateway (with Elastic IP)
   ↕
Private Route Table (0.0.0.0/0 → NAT)
   ↕
Private Subnet 1 & 2
   ↕
EC2 Private Instance

🔒 Security Achieved:

  • ✅ EC2 Private can connect out to the internet (outbound)
  • ✅ The internet CANNOT connect into EC2 Private (inbound blocked)
  • ✅ EC2 Private has no public IP (anonymous on the internet)
  • ✅ All outbound traffic goes through the NAT Gateway using the Elastic IP

💰 Cost Notes:

  • NAT Gateway: $0.045/hour ($32/month)
  • Data processing: ~$0.045/GB
  • Elastic IP: free while in use by the NAT Gateway
  • Estimated total: ~$35-50/month depending on data transfer volume

⚠️ Important: Remember to delete the NAT Gateway when you’re not using it, to avoid unnecessary charges. We’ll do the cleanup at the end of the workshop.

💡 Best Practices for Production:

  1. High Availability: Create a NAT Gateway in every AZ
  2. Monitoring: Track metrics such as BytesOutToDestination and BytesInFromSource
  3. Cost Optimization: Consider VPC Endpoints for AWS services to reduce data transfer through the NAT Gateway
  4. Backup Route: Have a contingency plan in case the NAT Gateway has an issue

✅ Done: You’ve successfully created a NAT Gateway and configured routing for the private subnets. The EC2 Private instance can now access the internet securely!