# Deploying a Python-based Microservice Application on AWS EKS

"Unlock the power of AWS EKS with this step-by-step guide! Learn how to effortlessly deploy a Python-based microservice application on Amazon's Elastic Kubernetes Service (EKS), harnessing the scalability and efficiency of containers in the cloud. Dive into the essentials of seamless deployment, optimizing your workflow for a robust, scalable, and dynamic application environment."

Credits for Video [**Nasiullha Chaudhari**](https://www.linkedin.com/in/nasiullha-chaudhari/)

YouTube video: [**https://youtu.be/jHlRqQzqB\_Y?si=oVjnBLWD5JEs\_dFW**](https://youtu.be/jHlRqQzqB_Y?si=oVjnBLWD5JEs_dFW)

GitHub Repo: [https://github.com/N4si/microservices-python-app](https://github.com/N4si/microservices-python-app)

Articles Credit: [**Ajay Kumar Yegireddi**](https://hashnode.com/@se7enAj)

For EKS,IAM,Node creation via Script refer video by Ajay Kumar: [https://www.youtube.com/watch?v=5-PZnYaoZUM](https://www.youtube.com/watch?v=5-PZnYaoZUM)

For Starting with we need to have some pre-requisite.

1. AWS Instance-- Kindly go with t2.medium OS Ubuntu and Version would be 20.04 because its is stable version for kubectl.
    
2. HELM
    
3. AWS CLI
    
4. Python
    
5. Install kubectl
    
6. Database: MongoDB and PostgreSQL
    

Once you done with creation of AWS instance installation of required package mentioned above you can wit creating of IAM role.

# Created a EC2 Instances

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704033129793/c4f5c494-6e2f-40ca-9353-0a9e88585288.png align="center")

## IAM role creation

Click on Search filed, type IAM

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704033352882/de422d44-8362-41a6-abf3-664488ce1b13.png align="center")

Click on "Role"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704033758045/e85f0dab-b82f-499b-befd-6e19080e7023.png align="center")

Click on AWS Service

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704033880301/2a90327b-b793-4235-9b0d-f21d56be6492.png align="center")

Search for "EC2" and Click on "Next"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704034071170/8f5e6bf8-501d-40ad-b73c-ec46b38e42ba.png align="center")

Now, search of **<mark>AdministratorAccess</mark>** & select as seen on below image.

and Click on "Next"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704034365923/b6b4a664-d7c6-4cd3-ac19-8f6a6491c91d.png align="center")

On the Role name, give name as you require and

Click on **<mark>Create Role</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704034676537/3bfa9b6b-7232-44e6-b3b0-28c05040df4f.png align="center")

After creation of IAM role go to **<mark>EC2 Instance</mark>**

and follow the below step

Select instance --&gt; Actions --&gt; Security --&gt; Modify IAM role

& click on Update IAM role.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704035091119/ef59ed59-c31b-4a69-afef-4fbd09bb2f46.png align="center")

Now you can connect to the server.

Once you login to the server you can fork the repo and make a clone of it by executing below command.

#git clone "repo url"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704035626424/cd68b678-efcd-4569-a6e7-3855ecdae169.png align="center")

Check Version by executing below command.

```basic
# Check Versions
terraform -version
kubectl version --client --short
aws --version
helm version --short
python3 --version
psql --version
mongod --version
docker version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704080722639/17176746-d24b-485e-b4c5-a1ee98cfc068.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704080900194/47d0c6a1-edb1-460b-9911-ea05bc905578.png align="center")

Now go inside Eks-terraform to provison Eks cluster with terraform files

Now you can see terraform files and make sure to change your region and <mark>S3 bucket name in the backend file.</mark>

Now initialize

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704081309953/9e435d7a-d04c-4680-855c-8c87d28babc7.png align="center")

Validate the code

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704082617823/29cb1af4-7e39-468b-9f09-2aecb848adbf.png align="center")

Now let's see plan

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704082968297/2b524769-76a4-4d9c-80b4-41f072f214fe.png align="center")

Let's apply to provision by executing below command

#terraform apply --auto-approve

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704098821923/00e90866-72ce-49da-9fd7-7f875474fd78.png align="center")

It take some time to complete the process. After completion there will be one EKS cluster, IAM role for EKS and Node Server will be created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704098863782/df733fc9-b53a-42b3-a8f7-5693059c95f5.png align="center")

EC2 Instance:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704099070959/ef538fa9-b3f1-44e7-87c4-63cbe0afaa61.png align="center")

EKS Cluster created

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704099169126/442b5b1b-7265-4da5-98f2-28069aa35053.png align="center")

IAM Role:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704099290074/dc8254c9-94d0-4568-98ad-dd61c5111eae.png align="center")

Will update the Kubernetes configuration by executing below command

#aws eks update-kubeconfig --name --region

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704099529879/1d4c689c-380b-40f7-b282-17bf8749d425.png align="center")

Let's go inside the Helm\_charts

```basic
cd Helm_charts
ls
```

Now go inside MongoDB and apply the k8s files

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704106758818/cbdf63f9-2321-4873-a108-83711a2616e4.png align="center")

No execute the below command

```basic
kubectl get all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704107210748/5b0a876f-c51b-448b-8246-18faf2b7deb1.png align="center")

To check the Persistent volume

```basic
kubectl get pv
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704107349339/c423de74-c1a2-481b-bc15-ae82e029eb37.png align="center")

Then go to Postgres folder and edit the init.sql file

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704107500769/38b938c1-9e9d-4ff0-9b26-1b918b2a62f4.png align="center")

Enter your email and password

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704108493206/35220b58-e21e-456a-9e59-fbe2b0ddb445.png align="center")

Apply the Kubernetes files using Helm

```basic
helm install postgres .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704108715849/8372460e-64d2-46c6-86bc-b7ed177af55d.png align="center")

Let's see the pods and deployments

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704108925176/a2b42d00-d179-4bff-992d-8b0d034022ce.png align="center")

Add some rules to Node Server

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704109334028/2b562d54-02f1-49ef-82d9-9e31c376ab18.png align="center")

Now copy the public IP of the Node group ec2 instance and execute below command.

```basic
mongosh mongodb://<username>:<pwd>@<nodeip>:30005/mp3s?authSource=admin
#username use nasi
#pwd nasi1234    #if you want to update them go to mongo secrets.yml file and update
#nodeip #use your node ec2 instance ip
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704109716118/a8a0087c-d845-4981-b1c0-6e5138c13c38.png align="center")

Connect to the Postgres database and copy all the queries from the "init.sql" file.

Now add the init.sql file and Use your mail for values

```basic
CREATE TABLE auth_user (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    email VARCHAR (255) NOT NULL,
    password VARCHAR (255) NOT NULL
);

--Add Username and Password for Admin User
-- INSERT INTO auth_user (email, password) VALUES ('thomasfookins007helby@gmail.com', '123456');
INSERT INTO auth_user (email, password) VALUES ('<User your mail>', '123456');
```

Now provide \\d to see data tables

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704110310747/5f6be2b7-918f-4fa6-8966-e46f76103b00.png align="center")

Now provide the below command to see the authentication user

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704110393296/952dbda4-8726-4b0e-82eb-f9efe81f7a15.png align="center")

Now go to RabbitMQ directory

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704110550520/d7f1c3a1-9766-48f9-9c36-c8d3ebd606d0.png align="center")

And execute below command

```basic
helm install rabbit .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704110665957/f2ab381d-c4ff-42a4-89ec-0fa86c72258e.png align="center")

Now see the deployments and pods

```basic
kubectl get all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704110932491/24509896-3b60-4275-8169-8c1060869f42.png align="center")

Now copy the public IP of the node group Ec2 instance

```basic
<Node-ec2-public-ip:30004>
```

You will get this page Just login

```basic
username is guest
password is guest
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111172500/447e2b86-3832-4da0-8cfe-d49df791a715.png align="center")

This will the dashboard once you will login

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111238533/18c13395-0020-4e9f-b5ee-d32a7709e7db.png align="center")

Click on Queues

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111351456/2026e137-e99b-4b05-9deb-81f64d842acc.png align="center")

After Queue for Mp3 and Video it will show like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111714544/a24b5352-9292-433a-ad05-1af097c4c918.png align="center")

### **Microservices**

Got to SRC directory as shown below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111832609/9b228bdf-b7b2-46be-9405-fc6cf25e4971.png align="center")

Go to directory as shown below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704111964669/694fb144-163e-43ec-a371-e15cd1cd3df8.png align="center")

Now let's see whether it created pods or not

```basic
kubectl get all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112062389/575463fd-1192-4e8f-a734-501b7b667eb8.png align="center")

Now come back to another Microservice and do the same process

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112181802/3330488b-7662-484a-a2b9-2d61aa2db3a6.png align="center")

And execute below command

```basic
kubectl apply -f .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112318201/559d94a4-12b4-4dc1-be78-ab17b601f5ae.png align="center")

Now come back to another microservice and do the same process

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112472645/09425951-792c-4590-8220-3c0452c1be0e.png align="center")

And execute the below command

```basic
kubectl apply -f .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112704956/ae5893e2-e0da-4302-88ae-74af9fd53801.png align="center")

Check for deployments and service

```basic
kubectl get all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112831476/92e4efe5-9123-48fb-a06d-c3b3c410da38.png align="center")

### **GMAIL PASSWORD**

Now let's create a Password for Gmail to get Notifications

Open your Gmail account in the browser and click on your profile top right.

Click on Manage your Google account

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704112976554/8b82af59-4a86-4501-baa4-2096dad2b375.png align="center")

Now click on Security

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704113060047/2457457f-cb12-47e4-a8f6-854be08afd54.png align="center")

Two-step verification should be enabled  
If not, enable it

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704113261244/30d557da-69e2-4143-be7a-c72e8cd6b52e.png align="center")

Now click on the search bar and <mark>search for the APP</mark>

Click on App passwords

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704113339883/416469d2-1a72-409d-b017-ac857badc24e.png align="center")

Come back to Putty and update the secret.yaml file for notification service microservice

```basic
cd ../..
cd notification-service
cd manifest
sudo vi secret.yaml
#change your mail and password that Generated.
```

vi secret.yaml

apiVersion: v1 kind: Secret metadata: name: notification-secret stringData: GMAIL\_ADDRESS: "[projectsdevops8@gmail.com](mailto:projectsdevops8@gmail.com)" GMAIL\_PASSWORD: "your generated password" type: Opaque

Now apply the manifest files

```basic
kubectl apply -f .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704114088542/ab5ef364-4272-4b34-9ae1-21e711301d74.png align="center")

Now come to the assets directory

```basic
cd /home/ubuntu/microservice-python-app
cd assets
ls
curl -X POST http://nodeIP:30002/login -u <email>:<password>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704116458590/57630233-4f72-487b-b51a-e37a881011a4.png align="center")

Copy the token and paste it inside a base 64 decoder you will get this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704116875997/5fae34cd-de80-4cbd-874a-917a72dd065d.png align="center")

  
change JWT Token and node ec2 IP.

```basic
 curl -X POST -F 'file=@./video.mp4' -H 'Authorization: Bearer <JWT Token>' http://nodeIP:30002/upload
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704118082266/3b954d89-e904-4d05-9263-05ea9e5d5146.png align="center")

It will send an ID to the mail

Let's check the mail

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704118126265/dfd12729-9367-4a28-bb0d-1d80de514d0b.png align="center")

Copy that ID and paste it inside the below command with the JWT token and fid

```basic
curl --output video.mp3 -X GET -H 'Authorization: Bearer ' "http://nodeIP:30002/download?fid="
#change Bearer with JWT token
#nodeIp
#fid token at end
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704118361875/14d23abb-3d2e-4893-ad10-8623828e49fa.png align="center")

It will download an MP3 file

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704118465974/9585a526-518a-46c4-870e-0065242fa41e.png align="center")

You can see it's created mp3 file and you can play it.

```basic
aws s3 cp your-file s3://your-bucket/your-prefix/
```

You can copy it s3 bucket and download and you can listen to it.

### **Destroy**

Now go inside Eks-terraform directory

To delete eks cluster

```basic
terraform destroy --auto-approve
```

It will take 10 minutes to delete the cluster

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704118655194/f7a77124-8d9f-4a62-9b98-08d9fa00f453.png align="center")

Once that is done  
Remove your Ec2 instance and Iam role.

# Thank You...!
