0% found this document useful (0 votes)
23 views

Set Up EKS Cluster Using Terraform

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Set Up EKS Cluster Using Terraform

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Set Up EKS cluster using

Terraform

In this blog, I will show you how to create EKS cluster


using terraform Infrastructure As A Code.

Introduction:
Amazon Elastic Kubernetes Service (EKS) is a managed Kubernetes service
that makes it easy to run Kubernetes on AWS without needing to install and
operate your own Kubernetes control plane. Terraform is an Infrastructure as
Code (IaC) tool that allows you to define and provision infrastructure using a
high-level configuration language. In this blog, I will show you the steps how to
set up an EKS cluster using Terraform.
Why EKS and Terraform?
EKS abstracts Kubernetes complexities, offering AWS-managed clusters.
Terraform complements EKS by enabling the configuration of AWS resources,
networking, security, and Kubernetes settings using a unified, declarative
approach.

Together, EKS and Terraform empower teams to adopt efficient DevOps


practices, facilitating seamless application deployment, scaling, and updates
with heightened reliability and security.

Prerequisites
Before we start, ensure you have the following:

1. AWS Account: You need an AWS account to create EKS and other resources.
2. AWS CLI: Installed and configured with proper permissions.
3. Terraform: Installed on your local machine.
4. kubectl: Installed to interact with your EKS cluster.

In order to create an EKS cluster using terraform we don’t need to write code
from scratch, we can get the module from the terraform registry itself.

What are modules?

Modules are self-contained packages of Terraform configurations that are


managed as a group. Basically, there are .tf files where somebody has written
code to create any service and we can use that code.
Step 1: Create a directory for your Terraform configuration files:
Commands :

[ ]# mkdir eks-terraform

[ ]# cd eks-terraform

Step 2: Second, we need to clone the code from the github repo. To
clone it locally use the below command using any CLI

git clone https://github.com/Fawazcp/terraform_project-eks.git


Step 3 : Create an s3 bucket using AWS management console

• Once the bucket has been created then in the cloned repository, locate the
terraform.tf file. Open this file and replace the bucket name with your
actual S3 bucket name. This bucket is used by Terraform to store the state
file.
Step 4: Initialize Terraform
Initialize the Terraform working directory by running:

Command : terraform init


This command downloads the necessary provider plugins and modules.

Step 5: Apply the Configuration


To create the EKS cluster and all related resources:

Command : terraform apply


• Go to the EKS dashboard from the console and we can see the cluster has
been created.

- Cluster
- VPC
- EC2

- S3
• In order to generate the kube config file in the cluster we can use the
below command;

• aws eks update-kubeconfig --region <region-code> --name <cluster-


name>

Step 6: Configure kubectl :


After the Terraform apply command completes, you need to configure
kubectl to use the newly created EKS cluster.

Commands:

curl -LO "https://dl.k8s.io/release/$(curl -s


https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" chmod +x
kubectl sudo mv kubectl /usr/local/bin/

- Once the kubectl is installed now you can run the Kubernetes commands .
That’s it! 😍We have successfully created an EKS cluster using terraform.

Note: Amazon EKS is a managed service. EKS service does not come under free
tier; it is chargeable so make sure to delete the cluster once this project has been
completed.

Clean Up:
If you no longer need the EKS cluster and want to delete all the resources
created by Terraform, you can use the terraform destroy command. This will
remove all the infrastructure that was created:

You might also like