VPC
Progress checklist
Overview
Section titled “Overview”The VPC
Virtual Private Cloud — an isolated, private network in AWS where your cluster resources run. is the network boundary for the entire cluster.
EKS worker nodes run in private subnets and reach the internet through a NAT
NAT Gateway — allows instances in private subnets to reach the internet without being directly reachable from it. .
Public subnets host the NAT Gateway and any public-facing load balancers.
See Networking concepts for why each piece exists.
Repository layout
Section titled “Repository layout”Directorymodules/ - vpc/ - main.tf VPC, subnets, IGW, NAT, and route tables - variables.tf - outputs.tf - walkthrough/ - main.tf Entry point — calls the vpc module - variables.tf - terraform.tfvars.example Copy to terraform.tfvars and fill in values
- …
-
Initialise Terraform. Required
walkthrough/ terraform init -
Create the VPC. Required
modules/vpc/main.tf resource "aws_vpc" "main" {cidr_block = var.cidrenable_dns_support = trueenable_dns_hostnames = truetags = merge(var.tags, { Name = var.name })} -
Create public and private subnets. Required
Spread subnets across three Availability Zones (as in the concepts diagram) for high availability. EKS requires subnet tags so it can discover them — see EKS subnet tags for why each tag is needed.
This walkthrough uses
/24for private subnets (matching the diagram); see VPC CNI and IP planning for larger clusters. See VPC CNI and IP address planning.modules/vpc/main.tf resource "aws_subnet" "public" {for_each = var.public_subnetsvpc_id = aws_vpc.main.idcidr_block = each.value.cidravailability_zone = each.value.aztags = merge(var.tags, {Name = each.key"kubernetes.io/cluster/${var.cluster_name}" = "shared""kubernetes.io/role/elb" = "1"})}resource "aws_subnet" "private" {for_each = var.private_subnetsvpc_id = aws_vpc.main.idcidr_block = each.value.cidravailability_zone = each.value.aztags = merge(var.tags, {Name = each.key"kubernetes.io/cluster/${var.cluster_name}" = "shared""kubernetes.io/role/internal-elb" = "1"})} -
Attach an Internet Gateway and NAT Gateway. Required
modules/vpc/main.tf resource "aws_internet_gateway" "main" {vpc_id = aws_vpc.main.idtags = merge(var.tags, { Name = var.name })}resource "aws_eip" "nat" {}resource "aws_nat_gateway" "main" {allocation_id = aws_eip.nat.idsubnet_id = values(aws_subnet.public)[0].idtags = merge(var.tags, { Name = var.name })} -
Apply and verify. Required
Terminal window terraform applyTerminal window terraform output vpc_idterraform output private_subnet_ids