Provisioning AWS EKS Cluster with Terraform – Tutorial

Learn how to provision an AWS EKS cluster with Terraform in this comprehensive step-by-step guide. We'll cover installation, configuration, and best practices.

Provisioning AWS EKS Cluster with Terraform – Tutorial

Table of contents

  • What is AWS EKS?
  • Why Use Terraform with AWS EKS?
  • How to Provision an AWS EKS Cluster with Terraform
    • Step 1 - Install Required Tools
    • Step 2 - Project Setup
    • Step 3 - Configure AWS Provider
    • Step 4 - Set Up VPC
    • Step 5 - Deploy EKS Cluster
    • Step 6 - Variable Definitions
    • Step 7 - Deploy Your Infrastructure
    • Step 8 - Connect to Your Cluster
    • Step 9 - Clean Up Resources
  • Best Practices and Security Considerations
  • Key Points

AWS EKS provides managed Kubernetes clusters as a service. If you’re on AWS and want to avoid getting into the details of setting up a Kubernetes cluster from scratch, EKS is the way to go!

In this guide, you will learn how to provision an AWS EKS Kubernetes cluster with Terraform. Let’s start with the basics.

Video Tutorial

Watch this video tutorial for a visual guide on setting up EKS with Terraform:

How to Deploy EKS Cluster using Terraform

What is AWS EKS?

Amazon Elastic Kubernetes Service (EKS) is a managed Kubernetes service that simplifies the deployment, management, and scaling of containerized applications using Kubernetes on AWS. It eliminates the need to maintain your own Kubernetes control plane, allowing you to focus on your applications rather than infrastructure management.

Why Use Terraform with AWS EKS?

Terraform is an ideal tool for provisioning EKS clusters because it:

  • Provides declarative infrastructure as code
  • Ensures consistent and repeatable deployments
  • Manages complex dependencies efficiently
  • Enables version control of your infrastructure
  • Facilitates collaboration in team environments

How to Provision an AWS EKS Cluster with Terraform

Let’s walk through the process of setting up an EKS cluster using Terraform.

Step 1 - Install Required Tools

Before we begin, you’ll need to install three tools:

# Install Terraform
brew install terraform

# Install AWS CLI
brew install awscli

# Install kubectl
brew install kubernetes-cli

If you’re not on macOS, you can find installation instructions here:

Step 2 - Project Setup

Create a new directory for your Terraform configuration:

mkdir eks-terraform
cd eks-terraform

You’ll need these files:

eks-terraform/
├── main.tf         # Main EKS configuration
├── variables.tf    # Variable definitions
├── outputs.tf      # Output definitions
├── providers.tf    # Provider configuration
└── terraform.tfvars # Variable values

Step 3 - Configure AWS Provider

Create providers.tf with the AWS provider configuration:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

Step 4 - Set Up VPC

Create the VPC configuration in main.tf. This will set up the networking foundation for your EKS cluster:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.14.0"

  name = "eks-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["us-west-2a", "us-west-2b", "us-west-2c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
  single_nat_gateway = true

  tags = {
    Environment = "Production"
    Terraform   = "true"
  }
}

Step 5 - Deploy EKS Cluster

Add the EKS cluster configuration to main.tf:

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "18.0.0"

  cluster_name    = var.cluster_name
  cluster_version = "1.24"

  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets

  eks_managed_node_groups = {
    default = {
      min_size     = 1
      max_size     = 3
      desired_size = 2

      instance_types = ["t3.medium"]
    }
  }
}

Step 6 - Variable Definitions

Create variables.tf to define the variables used in your configuration:

variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}

variable "cluster_name" {
  description = "Name of the EKS cluster"
  type        = string
  default     = "my-eks-cluster"
}

Step 7 - Deploy Your Infrastructure

Now let’s deploy the EKS cluster. Run these commands in sequence:

# Initialize Terraform
terraform init

# Review the changes
terraform plan

# Apply the configuration
terraform apply

Step 8 - Connect to Your Cluster

After the cluster is created, configure kubectl:

aws eks update-kubeconfig --name my-eks-cluster --region us-west-2
kubectl get nodes

Step 9 - Clean Up Resources

When you’re done with the cluster, clean up to avoid unnecessary charges:

terraform destroy

Best Practices and Security Considerations

When running EKS in production, consider these best practices:

  1. Network Security

    • Deploy worker nodes in private subnets
    • Implement strict security groups
    • Enable VPC flow logs for monitoring
  2. Access Management

    • Use RBAC for Kubernetes access control
    • Implement IAM roles for service accounts
    • Regularly audit access permissions
  3. Cost Management

    • Use Spot Instances for non-critical workloads
    • Implement auto-scaling based on demand
    • Regularly clean up unused resources

Key Points

  • EKS simplifies Kubernetes cluster management on AWS
  • Terraform provides a reliable way to provision and manage EKS clusters
  • Always follow security best practices
  • Implement proper monitoring and logging
  • Use infrastructure as code for consistency
  • Keep your cluster and dependencies updated

For a more advanced setup, consider exploring:

  • Multi-cluster management
  • Service mesh implementation
  • GitOps workflows
  • Custom networking policies
  • Advanced monitoring solutions