Amazon EKS — Terraform VPC Module
A production VPC for EKS needs public subnets (for load balancers), private subnets (for nodes), proper tagging for auto-discovery, and NAT Gateways for outbound traffic.
VPC Module — variables.tf
# modules/vpc/variables.tf
variable "cluster_name" {
description = "EKS cluster name (used for subnet tagging)"
type = string
}
variable "environment" {
description = "Environment name"
type = string
}
variable "vpc_cidr" {
description = "VPC CIDR block"
type = string
default = "10.0.0.0/16"
}
variable "azs" {
description = "Availability zones"
type = list(string)
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
variable "private_subnets" {
description = "Private subnet CIDR blocks"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
}
variable "public_subnets" {
description = "Public subnet CIDR blocks"
type = list(string)
default = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
}
VPC Module — main.tf
# modules/vpc/main.tf
data "aws_availability_zones" "available" {
state = "available"
}
locals {
azs = length(var.azs) > 0 ? var.azs : slice(data.aws_availability_zones.available.names, 0, 3)
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.environment}-eks-vpc"
}
}
# ── Internet Gateway ──
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.environment}-igw"
}
}
# ── Public Subnets ──
resource "aws_subnet" "public" {
count = length(var.public_subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnets[count.index]
availability_zone = local.azs[count.index]
map_public_ip_on_launch = true
tags = {
Name = "${var.environment}-public-${local.azs[count.index]}"
"kubernetes.io/role/elb" = "1"
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
}
}
# ── Private Subnets ──
resource "aws_subnet" "private" {
count = length(var.private_subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnets[count.index]
availability_zone = local.azs[count.index]
tags = {
Name = "${var.environment}-private-${local.azs[count.index]}"
"kubernetes.io/role/internal-elb" = "1"
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
}
}
# ── NAT Gateway (one per AZ for HA) ──
resource "aws_eip" "nat" {
count = length(var.public_subnets)
domain = "vpc"
tags = {
Name = "${var.environment}-nat-eip-${count.index}"
}
}
resource "aws_nat_gateway" "main" {
count = length(var.public_subnets)
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = {
Name = "${var.environment}-nat-${local.azs[count.index]}"
}
depends_on = [aws_internet_gateway.main]
}
# ── Public Route Table ──
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "${var.environment}-public-rt"
}
}
resource "aws_route_table_association" "public" {
count = length(var.public_subnets)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# ── Private Route Tables (one per AZ) ──
resource "aws_route_table" "private" {
count = length(var.private_subnets)
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main[count.index].id
}
tags = {
Name = "${var.environment}-private-rt-${local.azs[count.index]}"
}
}
resource "aws_route_table_association" "private" {
count = length(var.private_subnets)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
VPC Module — outputs.tf
# modules/vpc/outputs.tf
output "vpc_id" {
description = "VPC ID"
value = aws_vpc.main.id
}
output "private_subnet_ids" {
description = "List of private subnet IDs"
value = aws_subnet.private[*].id
}
output "public_subnet_ids" {
description = "List of public subnet IDs"
value = aws_subnet.public[*].id
}
output "vpc_cidr" {
description = "VPC CIDR block"
value = aws_vpc.main.cidr_block
}
output "nat_gateway_ips" {
description = "NAT Gateway public IPs"
value = aws_eip.nat[*].public_ip
}
Key Subnet Tags for EKS
| Tag | Value | Purpose |
|---|---|---|
kubernetes.io/cluster/<name> |
shared |
EKS identifies subnets belonging to the cluster |
kubernetes.io/role/elb |
1 |
Public subnets for internet-facing ALB/NLB |
kubernetes.io/role/internal-elb |
1 |
Private subnets for internal ALB/NLB |
Without these tags, the AWS Load Balancer Controller cannot auto-discover subnets for provisioning load balancers.
Cost-Saving Tip: Single NAT Gateway
For non-production environments, use a single NAT Gateway:
# Single NAT Gateway (dev/staging only)
resource "aws_nat_gateway" "single" {
count = 1 # Instead of length(var.public_subnets)
allocation_id = aws_eip.nat[0].id
subnet_id = aws_subnet.public[0].id
tags = {
Name = "${var.environment}-nat"
}
}