How to Create an EKS Cluster Using Terraform

How to Create an EKS Cluster Using Terraform

 


How to Create an EKS Cluster Using Terraform – Step-by-Step Guide

Amazon Elastic Kubernetes Service (EKS) is a fully managed service that simplifies running Kubernetes on AWS without managing the control plane yourself. If you’re a DevOps engineer or cloud enthusiast, knowing how to create an EKS cluster using Terraform is a must.

In this guide, we’ll cover everything you need — from prerequisites to deployment — so you can quickly set up a production-ready EKS cluster on AWS.


Why Use Terraform for EKS?

Terraform helps automate AWS infrastructure creation. Instead of manually creating resources, you write declarative code, making deployments repeatable, scalable, and version-controlled.

Key benefits include:

  • Infrastructure as Code (IaC): Easy versioning and collaboration

  • Reusable Modules: Write once, use anywhere

  • Cost Management: Automate cleanup to avoid surprises


Prerequisites

Before we begin, make sure you have:

  • An AWS account with admin access

  • AWS CLI installed and configured

  • Terraform installed (v1.5+ recommended)

  • A code editor like VS Code

  • Basic knowledge of AWS and Kubernetes


Step 1: Create a Terraform Project

Create a folder for your project:

mkdir eks-terraform cd eks-terraform

Inside this folder, create three files:

  • main.tf → Core infrastructure code

  • variables.tf → Variables for customization

  • outputs.tf → Outputs for easy access


Step 2: Configure Provider

In main.tf, start with the AWS provider:

provider "aws" { region = "us-east-1" }

Step 3: Use the EKS Terraform Module

HashiCorp provides a ready-made EKS module, making things easier. Add this code to main.tf:

module "eks" { source = "terraform-aws-modules/eks/aws" cluster_name = "my-eks-cluster" cluster_version = "1.30" subnets = ["subnet-123456", "subnet-654321"] vpc_id = "vpc-123456" node_groups = { default = { desired_capacity = 2 max_capacity = 3 min_capacity = 1 instance_types = ["t3.medium"] } } }

Tip: Replace vpc_id and subnets with your real values.


Step 4: Variables and Outputs

In variables.tf, define variables:

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

In outputs.tf, print the EKS cluster endpoint:

output "eks_cluster_endpoint" { value = module.eks.cluster_endpoint }

Step 5: Initialize and Apply

Run the following commands:

terraform init terraform plan terraform apply

When prompted, type yes to confirm.

Terraform will now create:

  • EKS Control Plane

  • Worker Nodes

  • Required IAM Roles


Step 6: Configure kubectl

Once the cluster is ready, update your kubeconfig:

aws eks --region us-east-1 update-kubeconfig --name my-eks-cluster

Check your nodes:

kubectl get nodes

If everything works, you have a running EKS cluster! 🎉


Step 7: Deploy a Test Application

To confirm, deploy an NGINX pod:

kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --port=80 --type=LoadBalancer

After a few minutes, get the external IP:

kubectl get svc

Open it in your browser — you should see the NGINX welcome page.


Best Practices for EKS with Terraform

  • Store state remotely: Use S3 + DynamoDB for Terraform state

  • Enable logging & monitoring: Integrate CloudWatch or Prometheus

  • Use modules: Keep your Terraform code modular and clean

  • Automate CI/CD: Use GitLab or GitHub Actions for deployments


Conclusion

That’s it! You’ve learned how to create an EKS cluster using Terraform step by step. With this setup, you can easily scale your infrastructure and deploy applications faster.

If you found this guide helpful, consider sharing it with others.

0 Response to "How to Create an EKS Cluster Using Terraform"

Post a Comment

Iklan Atas Artikel