☁️ Cloud

NAT

Network Address Translation

Translates private IPs to public IPs. Required for communication outside a VPC.

📖 Detailed Explanation

NAT (Network Address Translation) is a networking technology that translates private IP addresses into public IP addresses. It solves the IPv4 address exhaustion problem and protects internal networks from the outside. It allows multiple internal devices to access the internet through a single public IP.

NAT was first defined in RFC 1631 in the mid-1990s, and it is now used in almost all home routers, enterprise networks, and cloud environments. In particular, a NAT Gateway is essential when internet access is needed from a Private Subnet in an AWS VPC.

NAT comes in two types: SNAT (Source NAT) and DNAT (Destination NAT). SNAT translates the source IP of traffic going from the inside to the outside, and DNAT translates the destination IP of traffic coming in from the outside. PAT (Port Address Translation) also translates port numbers, handling thousands of concurrent connections with a single public IP.

In cloud environments, a NAT Gateway provides high availability (HA) and automatic scaling, and hourly charges and data processing charges apply. AWS NAT Gateway supports up to 45Gbps of bandwidth, and it is recommended to deploy one in each AZ for redundancy.

💻 Code Example

# AWS NAT Gateway Terraform 구성
resource "aws_eip" "nat" {
  domain = "vpc"
  tags = {
    Name = "nat-gateway-eip"
  }
}

resource "aws_nat_gateway" "main" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.public.id

  tags = {
    Name = "main-nat-gateway"
  }

  depends_on = [aws_internet_gateway.main]
}

# Private 서브넷 라우팅 테이블
resource "aws_route_table" "private" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block     = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.main.id
  }

  tags = {
    Name = "private-route-table"
  }
}

# Private 서브넷 연결
resource "aws_route_table_association" "private" {
  subnet_id      = aws_subnet.private.id
  route_table_id = aws_route_table.private.id
}

🗣️ Say It Like This in Practice

💬 In an architecture design meeting
"EC2 instances in the Private Subnet need to call an external API, so we need a NAT Gateway. Let's deploy one in each AZ to eliminate the single point of failure, and also consider a VPC Endpoint to reduce data transfer costs."
💬 In an Interview
"To explain the difference between a NAT Gateway and a NAT Instance: the Gateway is AWS-managed and provides high availability and automatic scaling, while the Instance is EC2-based, so it can be customized but must be managed directly. If cost optimization is important, choose a NAT Instance; if stability is important, choose a Gateway."
💬 In a cost-reduction discussion
"NAT Gateway costs are running over $300 a month, but if we switch S3 and DynamoDB access to a VPC Endpoint, we can reduce NAT traffic. Gateway Endpoints are free, and Interface Endpoints have hourly charges, but they're cheaper than large-volume data transfer."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Deploying a NAT Gateway in only a single AZ

If you place a NAT Gateway in only one AZ, Private Subnets in other AZs also lose internet access when that AZ fails. Cross-AZ traffic costs are also incurred, so deploy a NAT Gateway in each AZ.

❌
Ignoring the port exhaustion problem

Each NAT Gateway supports up to 55,000 concurrent connections. If you need a large number of outbound connections, distribute them across multiple NAT Gateways or use connection pooling.

✅
Correct cost optimization

Use Gateway Endpoints (free) for S3 and DynamoDB, and Interface Endpoints for ECR, CloudWatch, and similar services. A NAT Gateway is charged at $0.045 per hour + $0.045 per GB, so consider other approaches for large volumes of traffic.

🔗 Related Terms

📚 Learn More