Steps to Create EKS Cluster & Node Group AWS Services Using Terraform

Hello, In this Blog, we are discussing how to create eks cluster & node group using terraform. Terraform is a free & open source infrastructure setup tool, which is created by HashiCorp. We can easily create & destroy any resources using command line terminal. It is a simple & easy to use tool.

There are some steps to install & run the code using terraform & create the aws resources like eks cluster & node group.

Step 1: Update the System.

apt-get update

Step 2: First Install Terraform on system so click on Link https://www.hackerxone.com/2021/05/06/how-install-configure-terraform-ubuntu/

  • Check terraform version.

terraform version

Step 3: Before creating the EKS cluster & node-group we needs to create EKS cluster & node-group Role so click on link https://www.hackerxone.com/2021/12/07/steps-to-create-eks-cluster-node-group-role-on-aws-using-terraform/

Step 4: Create a folder.

mkdir project

Step 5: To change the directory & Create a .tf file.

cd project

  • Create a file & write the terraform code to create EKS & NodeGroup on AWS.

vim eks.tf

  • Paste the following code:

provider "aws" {
region = "type-region"
access_key = "type_aws_access_key"
secret_key = "type_aws_secret_key"
}
# Security group for network traffic to and from AWS EKS Cluster.
resource "aws_security_group" "eks-cluster" {
name = "eks-securitygroup-name"
vpc_id = "vpc_id"
# Egress allows Outbound traffic from the EKS cluster to the Internet
egress { # Outbound Rule
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Ingress allows Inbound traffic to EKS cluster from the Internet
ingress { # Inbound Rule
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Creating the EKS cluster
resource "aws_eks_cluster" "eks_cluster" {
name = "eks-cluster-name"
role_arn = "cluster-IAM-Role-ARN"
version = "1.21"
# Adding VPC Configuration
vpc_config { # Configure EKS with vpc and network settings
security_group_ids = ["${aws_security_group.eks-cluster.id}"]
subnet_ids = ["subnetID-1","subnetID-2"]
}
}
# Create EKS cluster node group
resource "aws_eks_node_group" "node" {
cluster_name = "eks-cluster-name"
node_group_name = "node-group-name"
node_role_arn = "node-group-IAMRole-ARN"
subnet_ids = ["subnetID-1","subnetID-2"]
disk_size  =  "10"
remote_access {
ec2_ssh_key = "key-name" ## For example ec2_ssh_key = "test"
}
scaling_config {
desired_size = 1
max_size = 1
min_size = 1
}
}

Step 6: To Initialize the working directory.

terraform init

  • Here is the command output.

  • To check the configuration.

terraform plan

  • Here is the command output.

  • To verify the configuration.

terraform validate

  • Here is the command output.

root@ip-172-31-42-12:/home/ubuntu/project# terraform validate
Success! The configuration is valid.

  • To run the code.

terraform apply

  • Here is the command output.

  • Type yes & Enter.

  • If we getting”No cluster found for name: cluster-name”error then Apply the code again because EKS cluster is not Active mode.

  • Finally,EKS & Node-group created.

Step 7: Open AWS Console & Check Elastic Kubernetes Service Cluster & Node Group.

  • Go to Elastic Kubernetes Service.
  • Click on cluster.
  • Cluster is Ready.

  • Then Open the cluster & check node group.
  • Node-Group is Added on cluster.

  • Successfully,EKS cluster & node-group has been created on aws account using terraform.

Leave a Reply