Opscraft
Terraform · Lesson 10 of 12 · 3 min read

Worked example: an EKS platform with Terraform

This is a classic whiteboard question and a good portfolio project. It uses the community VPC module v6 and EKS module v21 (v21 dropped the cluster_ prefix from many inputs: cluster_name → name, cluster_version → kubernetes_version, cluster_addons → addons, cluster_endpoint_public_access → endpoint_public_access).

Enlarge diagramVPC 10.20.0.0/16 · AP-SOUTH-1public 1aALB, NAT gatewayprivate 1a (/19)nodes and podspublic 1bALB, NAT gatewayprivate 1b (/19)nodes and podspublic 1cALB, NAT gatewayprivate 1c (/19)nodes and podsEKS cluster (module "eks")managed node group · VPC CNI, CoreDNS, kube-proxy, EBS CSI · access entries · Pod Identity
Figure 1. What the worked example builds: a three-AZ VPC, an EKS cluster in the private subnets, managed nodes and core add-ons.
Swipe sideways to see all of it, or tap to open it full screen.
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6.0"

  name = "acme-prod"
  cidr = "10.20.0.0/16"
  azs  = ["ap-south-1a", "ap-south-1b", "ap-south-1c"]

  private_subnets = ["10.20.0.0/19", "10.20.32.0/19", "10.20.64.0/19"]   # sized for pod IPs
  public_subnets  = ["10.20.96.0/24", "10.20.97.0/24", "10.20.98.0/24"]

  enable_nat_gateway     = true
  one_nat_gateway_per_az = true
  enable_dns_hostnames   = true

  private_subnet_tags = { "kubernetes.io/role/internal-elb" = 1, "karpenter.sh/discovery" = "acme-prod" }
  public_subnet_tags  = { "kubernetes.io/role/elb" = 1 }
}

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "~> 21.0"

  name               = "acme-prod"
  kubernetes_version = "1.34"
  vpc_id             = module.vpc.vpc_id
  subnet_ids         = module.vpc.private_subnets

  endpoint_public_access                   = false                 # private API endpoint
  enable_cluster_creator_admin_permissions = true
  authentication_mode                      = "API"                 # EKS access entries

  addons = {
    coredns                = {}
    kube-proxy             = {}
    vpc-cni                = { configuration_values = jsonencode({ env = { ENABLE_PREFIX_DELEGATION = "true" } }) }
    eks-pod-identity-agent = {}
    aws-ebs-csi-driver     = { service_account_role_arn = module.ebs_csi_irsa.iam_role_arn }
  }

  eks_managed_node_groups = {
    system = {
      instance_types = ["m6g.large"]          # Graviton
      ami_type       = "AL2023_ARM_64_STANDARD"
      min_size = 2
      max_size = 4
      desired_size = 2
      labels = { role = "system" }
      taints = { critical = { key = "CriticalAddonsOnly", value = "true", effect = "NO_SCHEDULE" } }
    }
  }

  access_entries = {
    platform_admins = {
      principal_arn = "arn:aws:iam::123456789012:role/PlatformAdmins"
      policy_associations = {
        admin = {
          policy_arn   = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy"
          access_scope = { type = "cluster" }
        }
      }
    }
  }
}

Then, in a separate stack or via Helm/Flux:

  • Karpenter (module terraform-aws-modules/eks/aws//modules/karpenter for its IAM, SQS interruption queue, then the Helm chart and NodePools),
  • AWS Load Balancer Controller, External DNS, cert-manager, External Secrets — each with an IAM role via Pod Identity or IRSA,
  • Flux bootstrap (flux bootstrap github or the Flux Terraform provider) so everything in-cluster after that is GitOps-managed.

Talking points: state per layer (network / cluster / addons), the VPC sized for VPC CNI pod IPs, private endpoint, access entries instead of aws-auth, Graviton for cost, Terraform for AWS resources and Flux for in-cluster workloads — "Terraform builds the platform, GitOps runs what's on it".