How to create an EKS cluster using Terraform?

Photo by Susan Q Yin on Unsplash

How to create an EKS cluster using Terraform?

In this blog, I explain how to use a terraform library which automates an EKS cluster creation on AWS.

This is the first part of 3 part series on how to Deploy ArgoCD on AWS EKS.

Prerequisites:

  1. Terraform
  2. eksctl library installed
  3. AWS account
  4. AWS configured credentials on your machine

GitHub Repo-https://github.com/Abhishek-569/k8-con..

1. Setting up Terraform

image.png

Advantages

As you are familiar with EKS you know how much code you have to write to create a cluster repeatedly. It is a time-consuming and repetitive task. So I decided to use an IaC tool to automate cluster creation on EKS. Terraform is the best IaC tool for this purpose of the advantages:

  1. Decreased risk
  2. Stable & consistent environments for faster iterations
  3. Cost optimization
  4. Self-documenting

Terraform provides all the above-mentioned benefits of IaC. Several other benefits of Terraform are:

  • Terraform can manage infrastructure on multiple cloud platforms.
  • The human-readable configuration language helps you write infrastructure code quickly.
  • Terraforms state allows you to track resource changes throughout your deployments.
  • You can commit your configurations to version control to safely collaborate on infrastructure.

eksctl

I have used eksctl to manage my EKS because eksctl is a simple CLI tool for creating and managing clusters on EKS - Amazon's managed Kubernetes service for EC2. It is written in Go, uses CloudFormation, and was created by Weaveworks.

Now to use eksctl in terraform I have used this provider on terraform. I have used this provider to set up my EKS cluster on AWS on terraforming because it is easy to use and understand and you only need to add a couple of lines in your code to use it.

Now to use this provider simply add below code to your terraform file.

terraform {
  required_providers {
    eksctl = {
      source = "mumoshu/eksctl"
      version = "0.16.2"
    }
  }
}

provider "eksctl" {
  # Configuration options
}

2. Setting up Configuration of the cluster.

Now we have completed the setup stage, now we will add the details in our terraform file to configure what kind of cluster we want to set up. now add the below code in terraform file:

provider "eksctl" {}

resource "eksctl_cluster" "primary" {
  eksctl_bin = "C:/ProgramData/chocolatey/bin/eksctl.exe"
  name = "test-cluster"
  region = "ap-south-1"
  version= "1.22"
  spec = <<-EOS
  nodeGroups:
  - name: linux-nodes
    instanceType: t2.large
    desiredCapacity: 2
  EOS
}

Above you have defined the name of the cluster, region, version and instance type. ⚠️⚠️ Please DO NOT select t2.micro as instance type as It might cause a problem further down the road.

✅ ✅ Use t2.large or instance type with more computing capacity.

Now our final EKS_on_AWS.tf file will look like:

terraform {
  required_providers {
    eksctl = {
      source = "mumoshu/eksctl"
      version = "0.16.2"
    }
  }
}
provider "eksctl" {}

resource "eksctl_cluster" "primary" {
  eksctl_bin = "C:/ProgramData/chocolatey/bin/eksctl.exe"
  name = "test-cluster"
  region = "ap-south-1"
  version= "1.22"
  spec = <<-EOS
  nodeGroups:
  - name: linux-nodes
    instanceType: t2.large
    desiredCapacity: 2
  EOS
}
  1. Use

    terraform init
    

    command to initialize the terraform file and download the provider mentioned above.

  2. Now use

    terraform apply
    

To apply the configuration. Type "yes" when asked to proceed. or use

terraform apply --auto-approve

To auto approve the whole set up.

It might take 15-20 min for AWS to spin up the cluster meanwhile keep an eye on AWS Cloudformation if your setup is completely created or not.

3. Destroy the set up

image.png

Bring the whole cluster down using a single command:

terraform destroy --auto-approve