How to Provision AWS EKS Cluster Using IAC(Terraform)

How to Provision AWS EKS Cluster Using IAC(Terraform)

In this article, you will go through the simplest and fastest way to provision an EKS cluster using Terraform. At the end of this article, you will be endowed with knowledge on eks cluster provisioning.

The prerequisites for this article are as follows

  1. Terraform should be installed on your devices. Here is how to install Terraform on your device

  2. Have an AWS account. Here is how to sign up for an account on AWS

Terraform can be used on any code editor, EC2 instance, digital ocean droplet, or Linode vm.

Firstly, you will create your VPC

Create a folder in your code editor. Inside this folder, create a file and name it VPC.TF. Paste the below inside the file

provider "aws" {

region = "us-west-2"

}

data "aws_availability_zones" "available" {}

module "eksctl-eks-vpc" {

source = "terraform-aws-modules/vpc/aws" version = "2.64.0"

name = "eksctl-eks-vpc"

cidr = var.vpc_cidr_block

private_subnets = var.private_subnet_cidr_blocks

public_subnets = var.public_subnet_cidr_blocks

azs = data.aws_availability_zones.available.names

enable_nat_gateway = true single_nat_gateway = true enable_dns_hostnames = true

tags = {

"kubernetes.io/cluster/eks-cluster" = "shared"

}

public_subnet_tags = {

"kubernetes.io/cluster/eks-cluster" = "shared"

"kubernetes.io/role/elb" = 1

}

private_subnet_tags = {

"kubernetes.io/cluster/eks-cluster" = "shared"

"kubernetes.io/role/internal-elb" = 1

}

In the above snippet, ensure you have the right region that is in your aws console.

You will create a variable file, which will help you define the variables in the vpc file and other files that will be created. Create a variable.tf file and paste the below code inside the file.

variable vpc_cidr_block {}

variable private_subnet_cidr_blocks {}

variable public_subnet_cidr_blocks {}

Now we will create our EKS cluster. Create a file and name it eks_cluster.tf and paste the below code inside.

provider "kubernetes" {

host = data.aws_eks_cluster.eks-cluster.endpoint

token = data.aws_eks_cluster_auth.eks-cluster.token

cluster_ca_certificate = base64decode(data.aws_eks_cluster.eks- cluster.certificate_authority.0.data)

}

data "aws_eks_cluster" "eks-cluster" {

name = module.eks.cluster_name

}

data "aws_eks_cluster_auth" "eks-cluster" {

name = module.eks.cluster_name

}

module "eks" {

source = "terraform-aws-modules/eks/aws"

version = "18.21.0"

cluster_name = "newapp-eks-cluster" cluster_version = "1.24"

subnet_ids = module.eksctl-eks-vpc.private_subnets

vpc_id = module.newapp- vpc.vpc_id

tags = {

environment = "development"

application = "newapp"

}

eks_managed_node_groups = {

dev = {

min_size = 1

max_size = 3

desired_size = 3

instance_types = ["t2.medium"]

}

}

}

In the above snippet, the provisioner is Kubernetes. The node group block is the number of node groups that you want to provision, you can input any number of your choice. The instance type is very important, your instance should have at least 4gb ram space for your pods to function well. This is why you are using t2.medium.

Create the last file name it terraform.tfvars and paste the bellow code inside it

vpc_cidr_block = "10.0.0.0/16"

private_subnet_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] public_subnet_cidr_blocks = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]

The next thing on the agenda is to go to your terminal and type the following code.

Type terraform init, this helps to initialize terraform and all the modules in the folder

Next, type terraform plan, this will help you see the infrastructure that will be provisioned

Lastly, type terraform apply, this will provision all the resources on your AWS console.

Check your AWS console, you will see that the EKS cluster has been provisioned.