Deploying an E-commerce Website on Amazon EKS Using Terraform
Tags: aws, cloud, devops
Published: 2024-01-28 22:37:51

**Overview:** 
Amazon Elastic Kubernetes Service (Amazon EKS) is a managed Kubernetes service that makes it easy for you to run Kubernetes on AWS and on-premises. Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. Amazon EKS is certified Kubernetes-conformant, so existing applications that run on upstream Kubernetes are compatible with Amazon EKS.
Amazon EKS automatically manages the availability and scalability of the Kubernetes control plane nodes responsible for scheduling containers, managing application availability, storing cluster data, and other key tasks.
Amazon EKS lets you run your Kubernetes applications on both Amazon Elastic Compute Cloud (Amazon EC2) and AWS Fargate. With Amazon EKS, you can take advantage of all the performance, scale, reliability, and availability of AWS infrastructure, as well as integrations with AWS networking and security services, such as application load balancers (ALBs) for load distribution, AWS Identity and Access Management (IAM) integration with role-based access control (RBAC), and AWS Virtual Private Cloud (VPC) support for pod networking.
Deploying an e-commerce web application on Amazon EKS using Terraform involves several steps. Below is a step-by-step guide to help you set up an EKS cluster and deploy a simple e-commerce web app. 

**Step 1: Set Up Terraform Configuration**
Check the link below for the full  terraform configurations.

Terraform Codes Repo
https://github.com/7hundredtech/EKS-Terraform--Project/tree/main/EKS_Project

Container Repo
https://hub.docker.com/r/ojosamuel/e-commerce


We will be using Terraform modules for this deployment. Modules are a key feature of Terraform that promotes modularity, reusability, and maintainability in infrastructure as code. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jp6rn31nmgrvjgfissi2.png)

The following infrastructure will  be deployed using the terraform Modules. 

**VPC–** The Terraform codes in the vpc module will deploy a default VPC and default subnets  in the selected region if it doesn’t exit already. If it does exist, it maintains the default VPC and Subnets.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpkya747ictk7tky6j26.png)
**Security Group**– The  Terraform codes in the security group module  will deploy a security group for the EC2 instance opening port 22  for ssh.  This will enable access into the EC2 instance for further interaction with the EKS cluster.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gem7mjhyt6tqe8fw43w5.png)

**IAM Roles**–  The IAM Roles Module will deploy the required policy permissions for the EC2 instance, Amazon EKS Cluster and the EKS worker node group.  These Roles and attached policies are very vital to ensure that our EKS cluster are working perfectly.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og7j2j9iho8e7z0qv9wh.png)

**EKS and Worker Node Group–** The Terraform codes in the EKS Module will  deploy the EKS cluster and the Worker Node Group.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6cy8upay86oeh5spwmi.png)

**EC2 Instance–** The Terraform codes in the EC2 module will deploy an EC2 instance that will serve as the K8-Client Server for interacting with our Amazon EKS Cluster and the Worker Nodes.  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/um745p7d1ukcce9mracv.png)
**User Data–** We will be using the script in ec2.sh  to preinstall Docker, AWS CLI , Git and Kubectl on the Ec2 instance.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxh3o0wl5pazryp6hj4a.png)

**Step 2: Initialize and Apply Terraform Configuration**
Run _**terraform Init**_ – to initialize terraform.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjvhpyvapqq6cyt782if.png)
Run _**terraform plan**_ – To preview all resources to be deployed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hvi4yvm2gbduyuvs047.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vb270fcl9cnj2j4r0w9.png)
Run _**terraform apply**_ – To deploy all resources.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m7dy3ecnphbp6f9718y.png)
Terraform will  create the necessary AWS resources as indicated in the **main.tf** file.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yiwuu8yx73dxj8lthj18.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik2sz5v83m15ams1697t.png)
**Checking our Resources on AWS **

**IAM Roles**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw8v8wwmyqgzyvbj4xzq.png)

**EKS Cluster  and Worker Node Group**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnyac1l0cz29480kvasj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9d34ohe71d9gcly9f9mn.png)

**EC2 Instance**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ya5einbr01mm9qcn7tn.png)

**Step 3: Confirm Preinstalled tools and configure **kubectl** to Use the EKS Cluster**
**Connect to the EC2 instance:** I will be connecting to EC2 instance with my VScode IDE. ( compare the internal IP of the EC2 as shared above)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffa24n37gccxlr11exy8.png)

**Confirm preinstalled tools are up and running.**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/454uat2dje0e96ozwhkf.png)
**Authenticate with your AWS credentials.** 
Use your Access key ID  and your secret access key with programmatic access to authenticate. Note- Do not share your secrets with anyone.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo6jfy1cykouta4w1p9j.png)
**configure kubectl to Use the EKS Cluster**

Run  `aws eks --region us-east-2  update-kubeconfig --name eks-cluster1` to configure kubectl

Run `kubectl get nodes` command to confirm we have access to our EKS cluster worker nodes.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emdtssmud454pi9n2byj.png)
_No resource currently running in the cluster._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8syfzno0y18owwd6jfx.png)

**Step 4: Deploy a Sample E-Commerce Web App**

Create both **deployment.yaml** file and **service.yaml** file 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yetwcjkrmoxdn3dnsudf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11jclgygq53dag3h0x6r.png)
**Create Deployment**: Run `kubectl apply -f deployment.yaml` command. This will create a deployment , a ReplicaSet and the specified number of Pods.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjvce4yg4d1c3wwpizwl.png)
**Create Service**: Run kubectl apply -f service.yaml command. This will create a Load balancer type of service. A load balancer will be deployed immediately on AWS.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1dsuk3g4687sk7ntwiz.png)
**Load Balancer**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q0mxqbticxz6fiszw4m.png)

To access our e-commerce website, copy the load balancer dns endpoint url and paste it on your web browser

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hha2nvyavkmq7hrc0ch.png)
Congratulations!!! We have successfully deployed our e-commerce website on Amazon EKS.

Thanks for Reading

**Clean Up**
Run terraform destroy to remove all infrastructures deployed to avoid unexpected bills