Table of Contents

Introduction

Terraform and Kubernetes are a powerful combination. Terraform provisions the cluster infrastructure (EKS, AKS, GKE), while the Kubernetes provider manages the resources running inside it (deployments, services, configmaps). This guide covers both levels of the stack.

For Kubernetes-specific recipes and patterns, visit Kubernetes Recipes.

Level 1: Provisioning Kubernetes Clusters

AWS EKS

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

  cluster_name    = "production"
  cluster_version = "1.28"

  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets

  eks_managed_node_groups = {
    general = {
      instance_types = ["t3.medium"]
      min_size       = 2
      max_size       = 10
      desired_size   = 3
    }
  }
}

Azure AKS

resource "azurerm_kubernetes_cluster" "main" {
  name                = "production-aks"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
  dns_prefix          = "production"

  default_node_pool {
    name       = "default"
    node_count = 3
    vm_size    = "Standard_D2_v2"
  }

  identity {
    type = "SystemAssigned"
  }
}

GCP GKE

resource "google_container_cluster" "primary" {
  name     = "production-gke"
  location = "us-central1"

  initial_node_count = 3

  node_config {
    machine_type = "e2-medium"
    oauth_scopes = [
      "https://www.googleapis.com/auth/cloud-platform"
    ]
  }
}

Level 2: Managing Kubernetes Resources

provider "kubernetes" {
  host                   = module.eks.cluster_endpoint
  cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
  token                  = data.aws_eks_cluster_auth.cluster.token
}

resource "kubernetes_namespace" "app" {
  metadata {
    name = "production"
    labels = {
      environment = "production"
    }
  }
}

resource "kubernetes_deployment" "app" {
  metadata {
    name      = "web-app"
    namespace = kubernetes_namespace.app.metadata[0].name
  }

  spec {
    replicas = 3
    selector {
      match_labels = { app = "web-app" }
    }
    template {
      metadata {
        labels = { app = "web-app" }
      }
      spec {
        container {
          name  = "web"
          image = "nginx:latest"
          port {
            container_port = 80
          }
        }
      }
    }
  }
}

The Full Stack

Terraform manages everything:

Cloud Provider (AWS/Azure/GCP)
  └── VPC / Network
      └── Kubernetes Cluster (EKS/AKS/GKE)
          ├── Namespaces
          ├── Deployments
          ├── Services
          ├── ConfigMaps
          ├── Secrets
          └── Ingress

When to Use Terraform vs Helm vs kubectl

ToolBest For
TerraformCluster provisioning, core K8s resources, GitOps-managed infra
HelmApplication packaging, templating, release management
kubectlDebugging, ad-hoc commands, quick changes

Learning Resources

Conclusion

Terraform gives you a single language to manage both the Kubernetes cluster and the workloads running on it. Combined with Kubernetes Recipes for K8s-specific patterns and our Terraform course for IaC fundamentals, you have everything you need to build production Kubernetes infrastructure.