DevHuzaifa's Portfolio
5 minUpdated Aug 4, 2026

Automating EKS Cluster Setup with Terraform: A Complete Guide

A Complete Guide to automate EKS cluster setup with terraform

#EKS#Terraform#AWS


Are you tired of manually setting up Kubernetes clusters on AWS? You’re not alone! Managing cloud infrastructure can be tricky, and that’s where Terraform comes in handy. In this guide, I’ll walk you through how to automate the setup of an EKS (Elastic Kubernetes Service) cluster on AWS using Terraform. The best part? You’ll have a running cluster in just a few steps — let’s jump right in!

eks


Why Terraform and EKS?

Before we dive into the technicalities, let’s answer a simple question: Why use Terraform to set up EKS?

  • Elastic Kubernetes Service (EKS): Amazon’s managed Kubernetes service makes running Kubernetes on AWS a breeze by handling control planes for you.
  • Terraform: Infrastructure as Code (IaC) lets you define cloud resources in declarative code. Terraform automates your setup, making it easy to scale, modify, and reproduce your infrastructure.

Together, they form a DevOps powerhouse, allowing you to spin up entire clusters without leaving the command line.


Prerequisites

Before you begin, make sure you have the following ready:

  1. AWS CLI installed and configured with your credentials.
  2. Terraform installed. (You can grab it here).
  3. kubectl installed to interact with your Kubernetes cluster.
  4. Sufficient IAM permissions to create AWS resources like VPCs, EKS clusters, etc.
# main.tf
provider "aws" {
region = "us-west-2" # Choose your preferred AWS region
}

# Create a VPC for the EKS Cluster
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "eks-vpc"
cidr = "10.0.0.0/16"

azs = ["us-west-2a", "us-west-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"]
enable_nat_gateway = true
}

# EKS Cluster
module "eks" {
source = "terraform-aws-modules/eks/aws"
cluster_name = "my-eks-cluster"
cluster_version = "1.25"
subnets = module.vpc.private_subnets
vpc_id = module.vpc.vpc_id

node_groups = {
eks_nodes = {
desired_capacity = 2
max_capacity = 3
min_capacity = 1

instance_type = "t3.medium"
}
}


Terraform Code in an IDE


Step 2: Initialize and Apply Terraform Configuration

Once you’ve written your Terraform configuration, it’s time to deploy it. Run the following commands in your terminal:


terraform init # Initializes Terraform and downloads necessary provider modules
terraform plan # Shows a detailed plan of what Terraform will create
terraform apply # Applies the plan and creates the EKS infrastructure


Terraform Plan Output


Once the apply is successful, you should see output like:

Apply complete! Resources: X added, X changed, X destroyed.



Step 3: Configuring kubectl to Access the EKS Cluster

Terraform will output the EKS cluster’s configuration once it’s up and running. To interact with your cluster using kubectl, configure it by running:

aws eks --region us-west-2 update-kubeconfig --name my-eks-cluster


kubectl get nodes Command Output


Results of running kubectl get nodes. This will display your EKS worker nodes, confirming that your cluster is ready to use.

If everything is set up correctly, you should see the nodes listed, indicating the cluster is up and running!


Step 4: Deploying a Simple Application to EKS

Now that your cluster is up, let’s deploy a simple Nginx application as a test. Create a YAML file for deployment:

# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80

Apply the deployment:

kubectl apply -f nginx-deployment.yaml


Step 5: Managing and Scaling the EKS Cluster

One of the great features of using Terraform is how easy it is to manage and scale your infrastructure. Let’s say you need to scale the number of nodes:

  1. Update the desired_capacity in the Terraform configuration under node_groups.
  2. Run terraform apply again, and Terraform will handle scaling the nodes automatically.


Conclusion

And that’s it! You’ve successfully automated the deployment of an EKS cluster using Terraform. With just a few lines of code and some CLI commands, you can deploy, manage, and scale Kubernetes clusters with ease.