Provisioning an EKS Cluster Using Terraform

Vinay K NVinay K N
3 min read

In this guide, you’ll learn how to provision an Amazon EKS (Elastic Kubernetes Service) cluster using Terraform, a powerful infrastructure-as-code tool. By the end of this tutorial, your Kubernetes cluster will be ready to deploy applications.

Prerequisites

Before you begin, ensure the following:

  • You have an active AWS account.

  • You understand basic Terraform concepts. (If not, check out this blog on Terraform fundamentals.)

  • You have a basic understanding of kubectl.

Why Use Terraform to Deploy EKS?

Although AWS provides native ways to create EKS clusters (via UI, CLI, or CloudFormation), using Terraform offers several advantages:

  • Unified Workflow: You can manage your entire AWS infrastructure and applications using the same Terraform codebase.

  • Lifecycle Management: Terraform handles create/update/delete operations for resources without requiring manual intervention.

  • Dependency Mapping: Terraform automatically manages the dependencies between resources, ensuring proper provisioning order.

Step 1: Clone the Repository

Start by cloning the Terraform project:

git clone https://github.com/onai254/terraform-eks-provision.git
cd terraform-eks-provision/main

Step 2: Understand the Project Structure

main.tf

Defines the core infrastructure: VPC, subnets, EKS cluster, and managed node groups.

Key features:

  • Creates a new VPC with public and private subnets.

  • Uses terraform-aws-modules for both VPC and EKS setup.

  • Provisions two managed node groups (node-group-1 and node-group-2) with 3 nodes each.

  • Installs the EBS CSI driver as an EKS add-on for persistent storage support.

output.tf

Outputs useful values after deployment:

output "cluster_endpoint" {...}
output "cluster_security_group_id" {...}
output "region" {...}
output "cluster_name" {...}

These outputs are critical for configuring kubectl and verifying your deployment.

terraform.tf

Specifies required providers and Terraform version:

terraform {
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 4.47.0" }
    random = { source = "hashicorp/random", version = "~> 3.4.3" }
    tls = { source = "hashicorp/tls", version = "~> 4.0.4" }
    cloudinit = { source = "hashicorp/cloudinit", version = "~> 2.2.0" }
  }
  required_version = "~> 1.3"
}

variables.tf

Defines configurable parameters:

variable "region" {
  default = "us-east-2"
}

variable "Cluster_name" {
  default = "viaixj"
}

variable "VPC_name" {
  default = "VPC_jshfjah"
}

You can override these values as needed for your environment.

Step 3: Initialize Terraform

Run the following command to initialize your working directory:

terraform init

This downloads required providers and modules, setting up your Terraform environment.

Step 4: Preview the Infrastructure Plan

Before applying the changes, inspect the plan

terraform plan

This shows what Terraform intends to create, update, or destroy.

Step 5: Apply the Configuration

To provision the infrastructure:

terraform apply

Review the plan, then type yes to confirm. Terraform will create all resources and display the output values.

Step 6: Configure kubectl

Once the cluster is created, you can configure kubectl using the output values:

aws eks --region $(terraform output -raw region) update-kubeconfig \
  --name $(terraform output -raw cluster_name)

Verify that kubectl is properly configured:

kubectl cluster-info
kubectl get nodes

You should see the control plane and worker nodes up and running.

Step 7: (Optional) Destroy the Cluster

To clean up the resources and avoid charges:

terraform destroy

Type yes when prompted to confirm.

Conclusion

With just a few commands, you’ve provisioned a production-ready EKS cluster using Terraform. This setup includes node groups, networking, IAM roles, and the EBS CSI driver.

Let’s connect and share our DevOps journeys! 🤝
Connect with me on LinkedIn

0
Subscribe to my newsletter

Read articles from Vinay K N directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinay K N
Vinay K N