Skip to main content

Command Palette

Search for a command to run...

EKS Cluster Using Module

Published
β€’2 min read
P

πŸ‘‹ Hello, and welcome to my DevOps journey! πŸš€ I am Priyanka Varshney,πŸ› οΈ As an aspiring DevOps engineer, I'm all about bridging the gap between development and operations, making software delivery seamless and efficient. πŸ’»πŸ”§ On this Hashnode blog, I'll be sharing my learnings, experiences and adventures as I dive deep into the world of continuous integration, automation, and cloud technologies. β˜οΈβš™οΈ Let's connect, learn, and grow as a vibrant DevOps community. Follow my Hashnode blog, and let's embrace the DevOps adventure together! πŸ€πŸ”—

Terraform Implementation: Create EKS Cluster using Terraform Modules | by  DJ. KONE | Medium

  • Modules are reusable, self-contained Terraform configurations.

  • Help organize, simplify, and standardize infrastructure code.

  • Used like functions: define once, use many times.

  • Can be local, from Terraform Registry, or remote sources (e.g., GitHub).

  • Improve code reusability, maintainability, and collaboration in teams.

    Step 2: Write Terraform Code for VPC and EKS

You can split your code by resource type or keep it in a single main.tf for simplicity.

VPC Setup (Basic) using Module

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = "pv-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1a", "us-east-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  tags = {
    Terraform = "true"
  }
}

locals.tf (Optional: can be inside main.tf if preferred)

locals {
    name = "pv-eks-cluster"
    vpc_cidr = "10.0.0.0/16"
    region = "us-east-2"
    azs   = ["us-east-2a", "us-east-2b"]
    private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
    public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]
    intra_subnets = ["10.0.5.0/24", "10.0.6.0/24"]
    env = "dev"
}

Using a Module (EKS)

Instead of writing all the EKS cluster logic manually, you can use a community-maintained module like this:

module "eks" {
    #import the module template
  source  = "terraform-aws-modules/eks/aws"

  #cluster info control-plane
  cluster_name    = local.name
  cluster_endpoint_public_access = true

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

  #control plane network
  control_plane_subnet_ids = module.vpc.intra_subnets

  cluster_addons = {
        vpc-cni = {
            most_recent = true
        }
        kube-proxy = {
            most_recent = true
        }
        coredns = {
            most_recent = true
        }

      }

   # EKS Managed Node Group(s)
  eks_managed_node_group_defaults = {
        instance_types = ["t2.medium"]
        attach_cluster_primary_security_group = true
  }


  eks_managed_node_groups = {
      eks-node-group = {
      instance_types = ["t2.medium"]

      min_size     = 1
      max_size     = 2
      desired_size = 1

      capacity_type = "SPOT"
    }
  }

  tags = {
    Environment = local.env
    Terraform   = "true"
  }
}

Here:

  • source points to the module location (Terraform Registry)

  • The rest are input variables required by the module

terraform.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.0.0"  # or latest stable version
    }
  }
}

providers.tf

provider "aws" {
    region = local.region
}

Step 3: Initialize and Apply Terraform

Open your terminal inside VS Code and run:

terraform init
terraform plan
terraform apply

Type yes to confirm the apply.

Finally, you can see the instance in the EKS on AWS

Happy learning :)