This project uses Terraform to provision a complete Amazon EKS environment on AWS with Infrastructure as Code (IaC):
πΉ A VPC with two public subnets across two Availability Zones, an Internet Gateway and public routing.
πΉ A managed EKS cluster with a managed node group (IAM roles created automatically, names unique per deployment).
πΉ A demo workload: a 2-replica nginx Deployment exposed through an AWS Load Balancer via a Kubernetes Service.
πΉ Kubernetes (K8s) manages containerized applications efficiently.
πΉ EKS (Elastic Kubernetes Service) is a managed K8s solution on AWS.
πΉ Terraform simplifies infrastructure provisioning with reusable modules.
flowchart LR
U(["π€ User"]) -->|"HTTP :80"| LB["AWS Load Balancer"]
subgraph VPC["VPC 10.0.0.0/16"]
subgraph AZ1["Public Subnet 1 (AZ-1)"]
N1["Worker Node (t3.small)<br/>nginx Deployment Β· 2 replicas"]
end
subgraph AZ2["Public Subnet 2 (AZ-2)"]
N2["Worker Node (scales up to max 2)"]
end
CP["EKS Control Plane"]
IGW["Internet Gateway"]
end
LB -->|"node port"| N1
LB -.-> N2
U --> IGW
CP -.->|"manages"| N1
CP -.->|"manages"| N2
The repository follows a modular structure for better organization and reusability:
EKS-CLUSTER-TERRAFORM/
βββ modules/ # Terraform modules
β βββ eks/ # EKS module
β β βββ main.tf # EKS cluster, managed node group, IAM roles
β β βββ variables.tf # Variables for the EKS module
β β βββ outputs.tf # Outputs for the EKS module
β βββ vpc/ # VPC module
β β βββ main.tf # VPC, public subnets, IGW, routing
β β βββ variables.tf # Variables for the VPC module
β β βββ outputs.tf # Outputs for the VPC module
β
βββ .gitignore # Git ignore file
βββ .terraform.lock.hcl # Provider lock file (generated by `terraform init`, committed)
βββ kubernetes.tf # Demo nginx Deployment + LoadBalancer Service
βββ main.tf # Root configuration: wires the VPC + EKS modules together
βββ outputs.tf # Root outputs (cluster info, ready-to-run kubectl command)
βββ provider.tf # Provider versions + AWS/Kubernetes provider configuration
βββ variables.tf # Root variables (AWS region)
βββ README.md # Documentation (this file)
πΉ modules/vpc β Handles VPC and networking setup, tagged for Kubernetes load-balancer discovery.
πΉ modules/eks β Manages the EKS control plane, managed node group and IAM roles.
πΉ provider.tf β Provider configuration; the Kubernetes provider authenticates via aws eks get-token.
πΉ main.tf β Root Terraform script that calls the modules.
πΉ kubernetes.tf β Deploys the demo application into the cluster.
πΉ outputs.tf β Displays useful deployment details.
β Automated & Repeatable Deployments β Reduce human error.
β Infrastructure as Code (IaC) β Easily track changes.
β Dependency Awareness β Ensures resources are created in the correct order.
β Scalability & Reusability β Modular design for better maintainability.
| Tool | Version | Why |
|---|---|---|
| β Terraform | >= 1.3 |
Provisioning (download) |
| β AWS CLI v2 | latest | Credentials and cluster authentication (the Kubernetes provider calls aws eks get-token) |
| β kubectl | matches cluster | Interacting with the cluster (install) |
| β AWS Account | β | With credentials configured via aws configure or environment variables |
git clone https://github.com/NotHarshhaa/eks-cluster-terraform
cd eks-cluster-terraformThe default region is us-east-1. Change it in variables.tf or override it when running Terraform:
terraform plan -var="aws_region=eu-central-1"terraform init # Initialize Terraform backend + download providers terraform plan # Preview infrastructure changes terraform apply # Deploy infrastructure (takes ~15 min for EKS)
π Expected Outputs: EKS cluster name & endpoint, VPC/subnet IDs, and a ready-to-run configure_kubectl command.
Configure kubectl using the command Terraform printed, or:
aws eks update-kubeconfig --name <EKS_CLUSTER_NAME> --region <AWS_REGION>
Verify the cluster and demo app:
kubectl get nodes kubectl get deployment terraform-project102 kubectl get service terraform-project102
Grab the load balancer address and open it in your browser:
kubectl get service terraform-project102 -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' # β http://<ELB-HOSTNAME> (may take a few minutes to become available)
Everything is defined in main.tf and documented in the module variables.tf files. The most useful settings:
| Setting | Where | Default |
|---|---|---|
| AWS region | variables.tf |
us-east-1 |
| VPC / subnet CIDRs | main.tf β module "vpc" |
10.0.0.0/16, 2 Γ /24 |
| Node instance type | main.tf β module "eks" |
t3.small |
| Node count (desired / min / max) | main.tf β module "eks" |
1 / 1 / 2 |
| Kubernetes version | main.tf β cluster_version |
latest AWS default |
| SPOT nodes (cheaper) | main.tf β capacity_type |
ON_DEMAND |
| Public API access CIDRs | main.tf β public_access_cidrs |
0.0.0.0/0 |
| SSH access to nodes | main.tf β key_pair |
disabled |
π‘ Enable SSH to nodes by setting key_pair to the name of an existing EC2 key pair. Without a source security group this opens port 22 to 0.0.0.0/0, so prefer setting remote_access_source_security_group_ids too.
- The Kubernetes API is publicly reachable from
0.0.0.0/0for convenience. Restrictpublic_access_cidrsto your IP for anything beyond a demo. - Worker nodes run in public subnets to keep the setup simple. For production, deploy nodes into private subnets with NAT.
- SSH to nodes is disabled by default.
- IAM role names are prefixed with the cluster name, so re-deploying this stack into the same account never collides.
$0.10/h), the EC2 nodes and the load balancer all cost money β destroy when you're done ($80+/month if left running).
terraform destroy
Alternatively, if using Terraform Cloud, go to: Settings β Destruction & Deletion β Queue destroy plan.
| Symptom | Likely cause / fix |
|---|---|
aws eks get-token errors during plan/apply |
AWS CLI v1 is too old β install/update to AWS CLI v2 |
Service EXTERNAL-IP stays <pending> |
Load balancer subnet discovery failed β this repo tags public subnets with kubernetes.io/role/elb; check kubectl describe service terraform-project102 |
terraform apply fails on key_pair |
The named EC2 key pair doesn't exist in the region β create it or leave key_pair unset |
Node group fails to launch (e.g. in us-east-1e) |
Instance type unavailable in the chosen AZ β retry, or use a different instance_types value |
scaling_desired_size validation error |
Desired size must be between scaling_min_size and scaling_max_size |
1οΈβ£ Go to Terraform Cloud
2οΈβ£ Create a new Workspace and connect your GitHub repository.
3οΈβ£ Add the following environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
CONFIRM_DESTROY
βΉοΈ The Kubernetes provider shell-outs to
aws eks get-token, so the Terraform Cloud workers also need the AWS CLI v2 installed (available on recent worker images).
If you find this repository helpful and plan to use it for learning, please consider giving it a star β. Your support motivates me to keep improving and adding more valuable content! π
This project is crafted with passion by Harshhaa π‘.
Iβd love to hear your feedback! Feel free to open an issue, suggest improvements, or just drop by for a discussion. Letβs build a strong DevOps community together!
Stay connected and explore more DevOps content with me:
Want to stay up to date with the latest DevOps trends, best practices, and project updates? Follow me on my blogs and social channels!