AWS EKS Automation with Terraform: Infrastructure as Code (IaC)
AWS EKS Automation with Terraform
The golden rule of managing cloud infrastructures: "Don't click in the console, write Code!" The Infrastructure as Code (IaC) approach ensures your infrastructure is reproducible, versionable, and auditable. Using Terraform when setting up a complex service like AWS EKS is almost a necessity.
In this guide, we will examine how to set up a production-ready EKS cluster using Terraform. You can check our Terraform Consultancy services for professional support on Terraform.
Why Terraform?
- Modularity: You avoid writing hundreds of lines of code by using ready-made modules for EKS.
- State Management: Tracks the current state of your infrastructure and updates only the changed parts.
- Dependency Management: Knows that EKS cannot be created before VPC is created and sets the order automatically.
EKS Setup Steps with Terraform
1. Provider Settings
First, define the AWS provider.
provider "aws" {
region = "eu-central-1"
}
2. VPC Module
EKS requires a specific network structure. The AWS VPC module makes our job easier.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "my-eks-vpc"
cidr = "10.0.0.0/16"
azs = ["eu-central-1a", "eu-central-1b"]
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
enable_vpn_gateway = true
tags = {
"kubernetes.io/cluster/my-cluster" = "shared"
}
}
3. EKS Module
The community-supported terraform-aws-modules/eks/aws module incredibly simplifies EKS setup.
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.0"
cluster_name = "my-cluster"
cluster_version = "1.30"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
eks_managed_node_groups = {
general = {
min_size = 1
max_size = 3
desired_size = 2
instance_types = ["t3.medium"]
}
}
enable_cluster_creator_admin_permissions = true
}
Execution
terraform init
terraform apply
After these commands, you will have a ready EKS cluster with its VPC, Security Groups, IAM Roles, and Worker Nodes in about 15-20 minutes.
Conclusion
Managing EKS with Terraform eliminates manual errors and allows you to make infrastructure changes (such as increasing node count or upgrading version) with just a code change and an apply command.