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.
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 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
- After successful output apply the changes using below command.
terraform apply
- After verifying type “yes” to confirm and apply.
- Cluster will take some time to initialize . You should see success output as below.
- You can also verify deployment by visiting console and opening clusters.
How To Create Amazon EKS Cluster Using Terraform?
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.