Create AWS EKS using Terraform Hands-On
Tags: beginners, containers, aws, cloud
Published: 2024-03-24 18:08:43

### **📝Introduction**

This post demonstrates how to create an AWS EKS cluster using Terraform.

_<mark>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.</mark>_

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`.

_<mark>Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.</mark>_

You can use [**link1**](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [**link2**](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_CreateAccessKey) for it.

### **📝GitHub repository**

Go to the **_GitHub repo_** to access the files used in this Lab on this [**link**](https://github.com/fjblsouza/K8s-HandsOn-Labs).

### **📝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.

![]
(https://cdn.hashnode.com/res/hashnode/image/upload/v1705267058544/b88fc989-55fe-4eab-bde3-58c5644a293b.png "Img")

### **📝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](https://github.com/fjblsouza/K8s-HandsOn-Labs)):

- `terraform.tf`

- `variables.tf`

- `outputs.tf`

- `main.tf`

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705267410811/c3372f63-b587-431e-90ee-528ed7b90d7a.png)

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705267460315/db3f3f53-7b13-447f-97fb-9f377c037e30.png)

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 variable `cluster_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
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705268427917/ec572dba-b18e-4cb5-a1e8-dc91856b2ac3.png)

Kindly check your configuration file syntax using the following command:

```
terraform validate
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705268497718/b21b5cc4-a790-4f33-a6b5-b66eb641cbba.png)

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.

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705268737210/b022aa37-f3be-4e58-a17b-074de56894f2.png)

### **📝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>
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705268852631/60473b97-977a-4226-9df1-2567869be670.png)
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.

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705268918857/9cc72140-1ea9-46e4-90a9-781fbcfbf0eb.png)
Now, we can interact with our cluster and obtain some information by running the following command:

```
kubectl cluster-info
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269004863/60b00bfe-7677-4d64-ad74-4c262801e2f0.png)

We can check the nodes that were created on the cluster by running the following command:

```
kubectl get nodes
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269135853/2e74c90d-157b-4521-9d1d-ea5665f5b6aa.png)

Also, you can go to the AWS EKS console to verify your cluster and nodes were created:

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269212862/52088cdb-3b5c-4f6f-9ac3-614925655495.png)

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269282325/48c75081-54b5-4258-a3dc-16b41e5c3122.png)

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269232657/1474bee2-e69b-417c-8c76-e1338794c367.png)

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269294834/8153e594-16d8-488a-96f0-63700d7f4160.png)

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705269310756/a9e34f0b-42ad-4e74-88cd-482ea3a87105.png)

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
```

!["Img"](https://cdn.hashnode.com/res/hashnode/image/upload/v1705270134095/a6a79556-858c-42c3-9d19-d6f8263ba752.png)

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**]
(https://clouddevopslearn.hashnode.dev/k8s-hands-on-lab-create-aws-eks-using-terraform) and LinkedIn [**franciscojblsouza**](https://www.linkedin.com/in/franciscojblsouza)