Deploy Kubernetes on AWS with Terraform

Kubernetes β€’ Amazon Web Services β€’ Terraform

Configuration Files

# main.tf - AWS EKS Cluster with Terraform

terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.23"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

# VPC for EKS
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"

  name = "${var.cluster_name}-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["${var.aws_region}a", "${var.aws_region}b", "${var.aws_region}c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway   = true
  single_nat_gateway   = true
  enable_dns_hostnames = true

  public_subnet_tags = {
    "kubernetes.io/role/elb" = 1
  }

  private_subnet_tags = {
    "kubernetes.io/role/internal-elb" = 1
  }
}

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

  cluster_name    = var.cluster_name
  cluster_version = "1.28"

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

  cluster_endpoint_public_access = true

  eks_managed_node_groups = {
    general = {
      desired_size = 2
      min_size     = 1
      max_size     = 4

      instance_types = ["t3.medium"]
      capacity_type  = "ON_DEMAND"
    }
  }

  tags = {
    Environment = "production"
    Terraform   = "true"
  }
}

# variables.tf
variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

variable "cluster_name" {
  description = "EKS cluster name"
  type        = string
  default     = "my-eks-cluster"
}

# outputs.tf
output "cluster_endpoint" {
  description = "Endpoint for EKS control plane"
  value       = module.eks.cluster_endpoint
}

output "cluster_name" {
  description = "Kubernetes Cluster Name"
  value       = module.eks.cluster_name
}

output "configure_kubectl" {
  description = "Configure kubectl"
  value       = "aws eks update-kubeconfig --region ${var.aws_region} --name ${module.eks.cluster_name}"
}

Prerequisites

  • AWS account with admin access
  • Terraform 1.0+ installed
  • AWS CLI installed and configured
  • kubectl installed
  • Basic Kubernetes knowledge
  • Understanding of AWS networking (VPC, subnets)
  • Credit card for AWS billing

Deployment Steps

  • Install Terraform: https://www.terraform.io/downloads
  • Install AWS CLI: https://aws.amazon.com/cli/
  • Configure AWS credentials: aws configure
  • Create project directory and add main.tf, variables.tf, outputs.tf
  • Initialize Terraform: terraform init
  • Review plan: terraform plan
  • Apply configuration: terraform apply
  • Wait 10-15 minutes for cluster creation
  • Configure kubectl: aws eks update-kubeconfig --region us-east-1 --name my-eks-cluster
  • Verify cluster: kubectl get nodes
  • Deploy applications with kubectl or Helm
  • To destroy: terraform destroy (WARNING: deletes everything)

πŸ“ Additional Notes

  • ☸️ Production-grade Kubernetes cluster
  • 🌍 Multi-AZ deployment for high availability
  • πŸ’° Cost: ~$75-150/month (EKS control plane $0.10/hour + EC2 instances)
  • πŸš€ Auto-scaling node groups
  • πŸ“¦ Managed node groups (AWS handles updates)
  • πŸ”’ IAM integration for pod security
  • πŸ“Š CloudWatch integration for monitoring
  • ⚑ EBS CSI driver for persistent volumes
  • πŸ”„ Rolling updates supported
  • ⚠️ Cluster creation takes 10-15 minutes
  • ⚠️ Remember to destroy resources when done testing
  • πŸ’‘ Use Spot instances for cost savings (add capacity_type = "SPOT")
  • πŸ’‘ Enable cluster autoscaler for dynamic scaling
  • 🎯 Perfect for microservices and production workloads