Automating EKS Cluster Setup with Terraform: A Complete Guide
A Complete Guide to automate EKS cluster setup with terraform
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!

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:
- AWS CLI installed and configured with your credentials.
- Terraform installed. (You can grab it here).
- kubectl installed to interact with your Kubernetes cluster.
- Sufficient IAM permissions to create AWS resources like VPCs, EKS clusters, etc.
# main.tfprovider "aws" {region = "us-west-2" # Choose your preferred AWS region}# Create a VPC for the EKS Clustermodule "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 Clustermodule "eks" {source = "terraform-aws-modules/eks/aws"cluster_name = "my-eks-cluster"cluster_version = "1.25"subnets = module.vpc.private_subnetsvpc_id = module.vpc.vpc_idnode_groups = {eks_nodes = {desired_capacity = 2max_capacity = 3min_capacity = 1instance_type = "t3.medium"}}

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 modulesterraform plan # Shows a detailed plan of what Terraform will createterraform apply # Applies the plan and creates the EKS infrastructure

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

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.yamlapiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deploymentspec: 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:
- Update the
desired_capacityin the Terraform configuration undernode_groups. - Run
terraform applyagain, 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.