📝Introduction
This post demonstrates how to create an AWS EKS cluster using Terraform.
Note: If you choose to use this Cloud Provider, you will be charged for the EKS resources used. So, don't forget to delete all resources at the end.
For this lab, the resources were created on AWS using Terraform, creating 3 Nodes, 2 Node groups, 1 VPC and 3 Subnets on the 3 AZs, 1 Cluster SG and an additional SG, also enabling an AWS EBS within the cluster, following the instructions from the GitHub repo shared below.
The benefit of using Terraform or another IaC tool(e.g. Pulumi or Chef) is it can be used to create code that provisions all sorts of resources, including Kubernetes clusters. Terraform provides a unified workflow that allows for full resource lifecycle management. In addition, the use of reusable modules simplifies the infrastructure creation process and reduces provisioning time.
📝Log in to the AWS Management Console
Using your credentials, make sure you're using the right Region. In my case, I chose us-east-1
.
Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.
You can use link1 and link2 for it.
📝GitHub repository
Go to the GitHub repo to access the files used in this Lab on this link.
📝Prerequisites:
AWS account
Kubernetes CLI installed
Kubectl installed
AWS CLI installed
Terraform installed
VS Code or similar IDE
📝Set AWS Credentials
Download AWS CLI v2:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Unzip the file:
unzip awscliv2.zip
See where the current AWS CLI is installed. It should be /usr/bin/aws
.
Update it:
sudo ./aws/install --bin-dir /usr/bin --install-dir /usr/bin/aws-cli --update
Check the version of AWS CLI:
aws --version ####It should now be updated.
Configure the CLI:
aws configure
For AWS Access Key ID
, paste your access key ID.
For AWS Secret Access Key
, paste your secret access key.
For Default region name
, enter <your-region>
.
For Default output format
, enter json
.
📝Install kubectl
Use the following command to check if you already have kubectl installed in your IDE:
kubectl version --client
If you do not have kubectl installed yet, follow these instructions.
After installing, check your Kubectl version, running the above command again.
📝Configure Working Directory
Create a directory where you want to house your project, then navigate to the directory and create the following files (Available on the GitHub repo link):
terraform.tf
variables.tf
outputs.tf
main.tf
You can customize the files with your settings.
Terraform.tf
-> This code defines the required providers, their sources, and the required versions to be used.Main.tf
-> This code defines the provider configurations with a Kubernetes module and defines the AWS region via a variable. The data block pulls the available availability zones within the set region and creates a data set of these zones. The locals variablecluster_name
uses a random string to create a unique EKS cluster name. The random string constraints are defined in the resource block. Also, it has the VPC module defines our CIDR block, availability zones, and public and private subnet CIDRs. We then enable a NAT gateway and DNS hostnames. The EKS module is also added, where the cluster name is defined using the local variable we created earlier. We can connect the EKS cluster to the VPC and subnets defined earlier by calling the VPC ID and private subnet IDs from the VPC module. Define the AMI type using the EKS-managed node group defaults argument. Next, define the managed node groups, including the instance types, minimum, maximum, and desired size.Variables.tf
-> This code defines the region variable that we referenced earlier. Change the default region to the one you want to use.Outputs.tf
-> This code defines the outputs we want printed to the CLI.
📝Deploy Kubernetes Cluster with Terraform
Before deploying the Cluster, we must first initialize the backend, modules, and provider plugins that we defined in our code.
Run the following command:
terraform init
Kindly check your configuration file syntax using the following command:
terraform validate
If you want to check a plan of the resources that will be created based on your infrastructure code, run the following command:
terraform plan
Next provision the resources using the following command:
terraform apply -auto-approve
Note - It can take a while to provision an EKS cluster (usually 10 to 15 minutes), so be patient 😊.
When the resources are successfully created you will see the output above previously defined on the Outputs.tf
file.
📝Configure kubectl
After creating your cluster, you must configure kubectl
to be able to interact with it. You can do this using the following command:
aws eks update-kubeconfig --region <region> --name <EKS_cluster_name>
Replace <region>
and EKS_cluster_name>
with the region youre working in and the name that was created for the cluster. You can pull this name from your outputs in your CLI.
Now, we can interact with our cluster and obtain some information by running the following command:
kubectl cluster-info
We can check the nodes that were created on the cluster by running the following command:
kubectl get nodes
Also, you can go to the AWS EKS console to verify your cluster and nodes were created:
Please do not forget to delete all the resources created in this Lab to avoid receiving a high charge for using the resources.
Run the following command to delete all resources:
terraform destroy
Congratulations you have completed this hands-on lab covering the basics of the Kubernetes cluster on AWS, creating an EKS cluster using the Terraform IaC tool.
Thank you for reading. I hope you understood and learned something helpful from my blog.
Please follow me on Cloud&DevOpsLearn and LinkedIn franciscojblsouza
Top comments (0)