Open In App

How To Create Amazon EKS Cluster Using Terraform?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Amazon EKS Cluster allows to building of containerized applications without the overhead of managing, scaling, and deploying. Terraform makes it easy to deploy an EKS cluster to AWS by making it IaaC. By using terraform a template can be created for multiple clusters and their management. Let’s see how we can deploy an EKS cluster in Terraform.

Primary Terminologies

  • Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
  • EKS: Managed Kubernetes service for deploying containerized applications.
  • IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.

Steps To StepUp Amazon EKS Cluster Using Terraform

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of Terraform.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.

To know about how to setup Terraform on Linux/Windows refer this – Article

Step 2 : Set up AWS CLI

  • Download the AWS CLI setup from official website
  • Run the installer and follow the steps to install.

Step 3 : Configure AWS CLI

  • Copy or create AWS access key from AWS console it is required in next step.
  • Open terminal or cmd and run below command to configure credentials of AWS as it will be required for terraform.
aws configure
  • Provide access keys copied in previous step and also give default region if different.

AWS configuration

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • add terraform block to code with aws as required provider with latest version. You can find the latest version at hashicorp registry.

Terraform registry

  • Terraform block should look like below. you can add required version to avoid invalidation.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}
  • Now add provider as aws like below. Specify other details as required.
provider "aws" {
region = "us-east-1"
}
  • At last add cluster configuration to the file.
  • We are giving only required details in the configuration. For creating cluster your account shoud have service role for amazon eks.
  • Go to your account and copy role arn from details.
  • Finally add vpc configuration that specify subnets for deployment. You can also make endpoint for cluster public by specifying endpoint_public_access to true.
  • configuration should look like below.
resource "aws_eks_cluster" "gfg_cluster" {
role_arn = "<YOUR ARN STRING>"
name= "gfg_eks_cluster"
version = "1.27"
vpc_config {
subnet_ids = ["subnet-042133d5c32b3d4af","subnet-07de5988fef95802d"]
endpoint_public_access = true
}
}
  • Final code in main.tf should look like as follows:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}

provider "aws" {
region = "us-east-1"
}

resource "aws_eks_cluster" "gfg_cluster" {
role_arn = "<YOUR ARN STRING>"
name= "gfg_eks_cluster"
version = "1.27"
vpc_config {
subnet_ids = ["subnet-042133d5c32b3d4af","subnet-07de5988fef95802d"]
endpoint_public_access = true
}
}

Step 5 : Apply the terraform code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

Apply the terraform code

  • After successful output apply the changes using below command.
terraform apply
  • After verifying type “yes” to confirm and apply.

configuring the apply changes

  • Cluster will take some time to initialize . You should see success output as below.

Successful creation of AWS EKS

  • You can also verify deployment by visiting console and opening clusters.

AWS EKS cluster

Conclusion

We have successfully deployed a amazon EKS cluster with the help of terraform in this article. the configuration described can be further modified to make chages to the cluster and aws. This is how terraform allows reusable and modifiable configuration of infrastructure.

Amazon EKS Cluster And Terraform – FAQ’s

What Are The Essential Terraform Providers And Modules For EKS Clusters?

The main Terraform provider for EKS is the aws provider. Additionally, there are community modules available for EKS that simplify the process, such as terraform-aws-modules/eks/aws.

How Do I Configure Worker Nodes In An EKS Cluster Using Terraform?

Worker nodes are typically managed as an Auto Scaling Group (ASG) in Terraform. You can define the desired instance type, number of instances, and other configurations within the ASG resource in your Terraform script.

Can I Use Terraform To Manage EKS clusters Across Multiple AWS Regions?

Yes, you can use Terraform to manage EKS clusters in multiple regions. Define separate Terraform configurations for each region and ensure that you manage the state and resources accordingly.

How Can I Update An Existing EKS Cluster With Terraform?

Use the Terraform taint command to mark the EKS cluster resources for recreation, and then apply the Terraform configuration to implement changes. Alternatively, modify the Terraform configuration file and apply the changes.

How do I Handle Terraform State Management For EKS Clusters?

Store Terraform state remotely using a backend like Amazon S3 and lock it using a state-locking mechanism to prevent conflicts when multiple users are managing the same infrastructure.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads