Effortless EKS Cluster Deployment using Terraform: A How-To Guide

Effortless EKS Cluster Deployment using Terraform: A How-To Guide

Introduction

In the ever-evolving landscape of cloud computing and containerization, Kubernetes has emerged as the de facto standard for orchestrating containerized applications. Amazon Elastic Kubernetes Service (EKS) is a fully managed Kubernetes service, simplifying the process of running Kubernetes on AWS. However, while EKS offers unparalleled scalability and flexibility, setting up and configuring an EKS cluster can be a daunting task, often requiring significant time and expertise.

Enter Terraform, a powerful infrastructure-as-code tool that enables seamless automation of cloud resources. With Terraform's declarative syntax and extensive provider ecosystem, creating and managing complex infrastructures becomes more manageable and reproducible. In this comprehensive how-to guide, we will explore the art of effortless EKS cluster deployment using Terraform.

Let's dive into the world of Terraform and EKS, and unlock the potential for seamless Kubernetes deployment in AWS. Harness the power of infrastructure-as-code and embrace a more efficient, scalable, and consistent approach to building and managing your EKS clusters.

Prerequisites

  • An AWS account with AWS CLI configured on your machine (Windows, Mac, or Linux)

  • Terraform installed

  • kubectl installed

Creating an Amazon Elastic Kubernetes Service Cluster using Terraform

We will use Terraform to create and provision an Amazon Elastic Kubernetes Service Cluster with the following AWS resources:

We will write Terraform files to configure all the AWS resources for the Amazon EKS Cluster. Let’s start working on our Terraform files.

Creating variables.tf file

Within your coding environment, create a eks-tf folder. Open the folder and create a new file named variables.tf. In the new file, add the following Terraform code to define our Terraform variables:

variable "region" {
  description = "AWS region"
 type = string
 default = "us-east-1"
}

This file defines the region we will create the Amazon EKS cluster. Our preferred region for this project is us-east-1.

Creating a terraform.tf file

In the eks-tf folder, create a new file named terraform.tf . Open the terraform.tf file and add the following Terraform code to configure the Terraform AWS provider:

terraform {
    required_providers {
      aws = {
        source = "hashicorp/aws"
      }

      random = {
        source = "hashicorp/random"
      }

      tls = {
        source = "hashicorp/tls"
      }

      cloudinit = {
        source = "hashicorp/cloudinit"
      }

      kubernetes = {
        source = "hashicorp/kubernetes"
      }
    }

    required_version = "~> 1.3"
}

This file will install the required providers that will give us access to the AWS cloud.

Creating a vpc.tf file

In the eks-tf folder, create a new file named vpc.tf . Open the vpc.tf file and add the following Terraform code to configure the AWS VPC:

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

  name = "my-VPC"

  cidr = "10.0.0.0/16"
  azs = slice(data.aws_availability_zones.available.names, 0, 3)

  private_subnets = ["10.0.1.0/24","10.0.2.0/24","10.0.3.0/24"]
  public_subnets = ["10.0.4.0/24", "10.0.5.0/24","10.0.6.0/24"]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  public_subnet_tags = {
    "kubernetes.io/cluster/${local.cluster_name}" = "shared"
    "kubernetes.io/role/elb" = 1
  }

  private_subnet_tags = {
    "kubernetes.io/cluster/${local.cluster_name}" = "shared"
    "kubernetes.io/role/internal-elb" = 1
  }
}

In this code, Terraform will use the AWS VPC module to create a VPC that we name my-VPC . It will also create three private subnets, and public subnets and their tags.

Creating an eks-cluster.tf file

In the eks-tf folder, create a new file named eks-cluster.tf . Open the eks-cluster.tf file and add the following Terraform code to configure the AWS EKS cluster:

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "19.15.4"

  cluster_name = local.cluster_name
  cluster_version = "1.25"

  vpc_id = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets
  cluster_endpoint_public_access = true

  eks_managed_node_group_defaults = {
    ami_type = "AL2_x86_64"
  }

  eks_managed_node_groups = {
    one = {
      name = "node-group-1"
      instance_types = ["t3.small"]

      min_size = 1
      max_size = 3
      desired_size = 2
    }

    two = {
      name = "node-group-2"
      instance_types = ["t3.small"]

      min_size = 1
      max_size = 2
      desired_size = 1 
    }
  }

}

Terraform will use the terraform-aws-modules/eks/aws module to provision the AWS EKS cluster. Here, we will work with the eks_managed_node_groups.

Create a main.tf file

In the eks-tf folder, create a new file named main.tf . Open the main.tf file and add the following Terraform code:

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "19.15.4"

  cluster_name = local.cluster_name
  cluster_version = "1.25"

  vpc_id = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets
  cluster_endpoint_public_access = true

  eks_managed_node_group_defaults = {
    ami_type = "AL2_x86_64"
  }

  eks_managed_node_groups = {
    one = {
      name = "node-group-1"
      instance_types = ["t3.small"]

      min_size = 1
      max_size = 3
      desired_size = 2
    }

    two = {
      name = "node-group-2"
      instance_types = ["t3.small"]

      min_size = 1
      max_size = 2
      desired_size = 1 
    }
  }

}

This file defines the AWS Cloud availability zones and the AWS EKS Cluster name.

Creating outputs.tf file

In the eks-tf folder, create a new file named outputs.tf . Open the outputs.tf file and add the following Terraform code.

output "cluster_name" {
    description = "Amazon Web Service EKS cluster Name"
    value = module.eks.cluster_name
}

output "cluster_endpoint" {
    description = "Endpoint for Amazon Web Service EKS"
    value = module.eks.cluster_endpoint
}

output "region" {
    description = "Amazon Web Service EKS Cluster region"
    value = var.region
}

output "cluster_security_group_id" {
    description = "Security group ID for the AWS"
    value = module.eks.cluster_security_group_id
}

This file determines all the outputs displayed in your command line. Using the files, Terraform will provide the `cluster_name`, “cluster_endpoint“, “region” and “cluster_security_group_id“. Let’s run the Terraform commands to provision the Amazon EKS cluster.

Running the Terraform code

Terraform init command

This command will download the modules from the Terraform registry, and initialize the Terraform AWS EKS modules and the Terraform backend.

terraform init

Terraform plan command

The Terraform plan command will display the following AWS resources in your terminal

terraform plan

Terraform apply command

This command will provision the AWS EKS Cluster and all the AWS resources defined in the Terraform files:

terraform apply

When you apply the code, Terraform will ask you for confirmation for Terraform to provision the AWS resources. It will allow you to review the AWS resources displayed in your terminal. If you are satisfied, you will type yes and Terraform will provision these AWS resources.

Updating the .kubeconfig file

When you apply the code and your application is deployed, you need to update your .kubeconfig file for your system to perform operations on the Kubernetes cluster. To dxo this you apply the following code.

aws eks update-kubeconfig --name eks-PDRuPtsE --region us-east-1 --kubeconfig ~/.kube/config

After you have done this, run the following commands.

kubectl cluster-info

You would get the following results.

Now your Kubernetes cluster is ready to deploy your application.

Deploying the cluster

To destroy the AWS EKS Cluster and the AWS resources, run this command:

terraform destroy

You will type yes to confirm, and all the AWS resources for the EKS cluster will be destroyed.

Conclusion

In this article, we have taught you how to create an Amazon Elastic Kubernetes Service cluster using Terraform. We created multiple Terraform files, and terraform used these files to create the AWS EKS Cluster and provision the AWS resources. Finally, we used Terraform commands to create and provision the AWS EKS Cluster. You can easily follow this tutorial and create an Amazon Elastic Kubernetes Service (Amazon EKS) cluster using Terraform.