Copyright (c) 2025 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

2.1.2. NAT Gateways and Egress-Only Internet Gateways

NAT Gateways and Egress-Only Internet Gateways provide secure, outbound-only internet connectivity for instances in private subnets, ensuring security while enabling necessary updates and communication.

Scenario: You have application servers in a private IPv4 subnet that need to download software updates from the internet. You also have some new applications using IPv6 in private subnets that require outbound internet access. Neither should be publicly accessible.

Instances deployed in private subnets do not have direct internet access. However, they often need to initiate outbound connections for software updates, external API calls, or to download patches.

Key Concepts:
Practical Implementation: Configuring a Private Subnet Route Table for NAT Gateway
# Assuming PRIVATE_SUBNET_ID and NAT_GATEWAY_ID are already defined
# 1. Create a new route table for the private subnet (if not using default)
PRIVATE_ROUTE_TABLE_ID=$(aws ec2 create-route-table --vpc-id $VPC_ID --query RouteTable.RouteTableId --output text)
echo "Private Route Table ID: $PRIVATE_ROUTE_TABLE_ID"

# 2. Add a default route (0.0.0.0/0) to the NAT Gateway in the private route table
aws ec2 create-route --route-table-id $PRIVATE_ROUTE_TABLE_ID --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_GATEWAY_ID

# 3. Associate the private subnet with this route table
aws ec2 associate-route-table --subnet-id $PRIVATE_SUBNET_ID --route-table-id $PRIVATE_ROUTE_TABLE_ID

⚠️ Common Pitfall: Placing a NAT Gateway in a private subnet. A NAT Gateway must be placed in a public subnet to have a route to the Internet Gateway, which it needs to function.

Key Trade-Offs:
  • Security vs. Cost: NAT Gateways provide enhanced security by preventing inbound connections but incur costs based on hourly usage and data processing.

Reflection Question: How do NAT Gateways (for IPv4) and Egress-Only Internet Gateways (for IPv6) provide secure, outbound-only internet connectivity for instances in private subnets, ensuring necessary communication while maintaining network isolation and security?