Guide To Automating AWS EKS Cluster Setup With Terraform
Guide To Automating AWS EKS Cluster Setup With Terraform
Prerequisites
Required Tools:
Architecture:
1. Folder Structure
project-root/
provider "aws" {
region = var.aws_region
}
# Create a VPC
resource "aws_vpc" "eks_vpc" {
cidr_block = var.vpc_cidr_block
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "${var.cluster_name}-vpc"
}
}
# Create Subnets
resource "aws_subnet" "example_subnet" {
count = length(var.subnet_cidr_blocks)
vpc_id = aws_vpc.eks_vpc.id
cidr_block = var.subnet_cidr_blocks[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = {
Name = "${var.cluster_name}-subnet-${count.index}"
}
}
vpc_config {
subnet_ids = aws_subnet.example_subnet[*].id
}
}
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "eks.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
role = aws_iam_role.eks_role.name
policy_arn = each.value
}
output.tf
output "eks_cluster_name" {
description = "Name of the EKS cluster"
value = aws_eks_cluster.eks_cluster.name
}
output "eks_cluster_endpoint" {
description = "EKS Cluster endpoint"
value = aws_eks_cluster.eks_cluster.endpoint
}
output "eks_cluster_arn" {
description = "EKS Cluster ARN"
value = aws_eks_cluster.eks_cluster.arn
}
variable.tf
variable "aws_region" {
description = "AWS region to deploy resources"
default = "ap-south-1"
}
variable "cluster_name" {
description = "Name of the EKS cluster"
default = "alvin-eks-cluster"
}
terraform init
terraform plan
Before executing the terraform apply command, I retrieved and reviewed the EKS output.
terraform apply
To validate the EKS cluster:
1. Navigate to the **Amazon EKS console**.
2. Review the cluster's status and configuration details to ensure it has been created successfully.
terraform destroy
Conclusion
This configuration demonstrates a complete setup for deploying a scalable and secure EKS cluster on
AWS. It’s designed for flexibility, allowing easy modifications to suit specific requirements. If you're
interested in modern DevOps practices or cloud-native solutions, this is a great starting point!