Project 4: Lift & Shift a 3 tier web application to AWS

Semo
8 min readJun 6, 2024

--

In this project I will migrate a 3 tier web application from an on-premises environment to Amazon Web Services (AWS) using lift-and-shift technology.

The objective is to leverage AWS services to ensure seamless transitions, minimize downtime, and maintain application performance.

The above picture represent the web application on premises , now I need to migrate it on AWS .

I will perform this steps to migrate on premises web applications to AWS.

I will divide it into different phases .

First Phase : Introduction

Second Phase : Security Group & Key Pair

Third Phase : Ec2 instance

Fourth Phase : Build & Deploy artifact

Fifth Phase : Load balancer

Sixth Phase : Validate

Let’s Start

First Phase: I will explain about the application architecture and all the services , which is used by web app.

As we can see in the image

Here you can see that the web application is using nginx (also using load balancer in nginx) , apache tomcat as a web server and also using Rabbit MQ, Mysql db and memcached.

So I will replace nginx(loadbalancer ) with Elastic Load Balancer and I will use apache tomcat as a webserver in EC2 instance.

I will install Rabbit MQ , Memcahced and Mysqldb in different EC2 instances.

If I use all the different services in a single EC2 instance then it will cause error or if it goes down then our complete application goes down.

That was the first phase , now we will discuss about second phase.

Second Phase: Create security groups for different different EC2 servers.

First I created a security group for my elastic load balancer.

Inbound rules for security group for ELB

Then I created a security group for webapp instance, where my web application code will be running.

Inbound rule for webapp instance

Then I created a security group for rabbitmq , mysqland memcached (Backend)

After that I created a keypair which I will use as a key pair while creating EC2 instances.

Click on Create key pair option.

Third Phase : Now time to create all EC2 instances .
First I created EC2 instance for the web application

Before creating the EC2 servers, I prepared scripts for each server , which will be defined in the user data section , it will help to save time.

For main server the script is given below:-

#!/bin/bash
sudo apt update
sudo apt upgrade -y
sudo apt install openjdk-11-jdk -y
sudo apt install tomcat9 tomcat9-admin tomcat9-docs tomcat9-common git -y

For memcached server , the script is given below:-

#!/bin/bash
sudo apt update
sudo apt install -y memcached
sudo systemctl start memcached
sudo systemctl enable memcached
sudo systemctl status memcached
sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/memcached.conf
sudo systemctl restart memcached
sudo ufw allow 11211/tcp
sudo ufw allow 11111/udp
sudo systemctl restart ufw

For RabbitMq instance , the script is given below :

#!/bin/bash
# Update package lists and upgrade packages
sudo apt update
sudo apt upgrade -y
# Install necessary packages
sudo apt install wget -y
# Install RabbitMQ
sudo apt-get install rabbitmq-server -y
# Start and enable RabbitMQ service
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
# Allow RabbitMQ port through firewall
sudo ufw allow 5672/tcp
# Configure RabbitMQ to allow external connections
sudo rabbitmqctl add_user test test
sudo rabbitmqctl set_user_tags test administrator
sudo rabbitmqctl set_permissions -p / test ".*" ".*" ".*"
# Restart RabbitMQ service
sudo systemctl restart rabbitmq-server

For mysql server , the script is given below:-

#!/bin/bash
# Set the database password
DATABASE_PASS='admin123'
# Update package lists and install necessary packages
sudo apt update
sudo apt install -y git zip unzip mariadb-server
# Starting & enabling mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb
# Clone the project repository
cd /tmp/
git clone -b main https://github.com/hkhcoder/vprofile-project.git
# Restore the dump file for the application
sudo mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASS';"
sudo mysql -u root -p"$DATABASE_PASS" -e "DROP USER IF EXISTS ''@'localhost';"
sudo mysql -u root -p"$DATABASE_PASS" -e "DROP USER IF EXISTS ''@'$(hostname)';"
sudo mysql -u root -p"$DATABASE_PASS" -e "DROP DATABASE IF EXISTS test;"
sudo mysql -u root -p"$DATABASE_PASS" -e "FLUSH PRIVILEGES;"
sudo mysql -u root -p"$DATABASE_PASS" -e "CREATE DATABASE accounts;"
sudo mysql -u root -p"$DATABASE_PASS" -e "GRANT ALL PRIVILEGES ON accounts.* TO 'admin'@'localhost' IDENTIFIED BY 'admin123';"
sudo mysql -u root -p"$DATABASE_PASS" -e "GRANT ALL PRIVILEGES ON accounts.* TO 'admin'@'%' IDENTIFIED BY 'admin123';"
sudo mysql -u root -p"$DATABASE_PASS" accounts < /tmp/vprofile-project/src/main/resources/db_backup.sql
sudo mysql -u root -p"$DATABASE_PASS" -e "FLUSH PRIVILEGES;"
# Restart mariadb-server
sudo systemctl restart mariadb
# Starting the firewall and allowing mariadb access from port 3306
sudo ufw allow 3306/tcp
sudo systemctl enable ufw
sudo systemctl start ufw

Now time to create all of the EC2 instances one by one.

Using the same key pair that was created in phase 2

Also select the same security group , which is created in phase 2

Paste the script in the user data section as shown before

I have created 4 instances .

Now time to check if the script I defined in user data section is working or not.

I will login into my webapp instance first to check tomcat server is installed or not

As we can see that tomcat9 is in active state, it means the script is working.

Now I will check each of my backend instances.

First Mysql

It is working .

Now I will check whether memcached server is working or not.

It is also working . Now I will check for rabbitmq server.

It is also working.

It means all the scripts are working.

Now I will use Route 53 to redirect the traffic .
First I need to create a private hosted zone, then I will create records for my backend servers

Must remember I will use privateIP of the servers , here mc means memcached

I will create 3 records, one for rabbitmq and second for memcached and the third for mysql.

Now after making changes in the route53 records , I need to update it inside the applications.properties section. Here I need to add the db01.semo.co.uk and same for memcached & rabbitmq .

Now phase 3 is completed .

Phase 4 : Build the artifact and store in a S3 bucket .

I will clone the code from the github repo which is mentioned below:

https://github.com/hkhcoder/vprofile-project/tree/aws-LiftAndShift

clone the github repo in local system and open with any editor. I am using visual studio code editor.

Now we build the code with the help of mvn install because I am using maven for code building.

After that I need to push the artificat to S3 with the help of the command given below:

aws s3 cp vprofile-v2.war s3://semoliftshift04062024 
### this command is used to copy the file from source to destinations.

First I will create a new bucket with the help of command which is given below:-

aws s3 mb s3://semoliftshiftbucket04062024 ##semoliftshiftbucket04062024 is the name of the bucket

I have successfully stored the artifact in my S3 bucket .

Now I need to push the code into the EC2 app server.

For that first I will create the IAM role that I will attach with my EC2 instance so it can access the S3 bucket.

Then I will connect to my EC2 instance and pull the code which is store in the S3 bucket.

After that I need to copy the artifact to /tmp/ location

aws s3 cp s3://awsliftshift/vprofile-v2.war /tmp

After I need to stop tomcat9 with the command

systemctl stop tomcat9

After that delete the root directory

rm -rf var/lib/tomcat9/webapps/ROOT

After that copy the .war file from tmp location where it was stored earlier.

cp /tmp/vprofile-v2.war /var/lib/tomcat9/webapps/ROOT.war

Then finally I need to start tomcat9 with the command is given below:

systemctl start tomcat9

Here is some highlight…

Phase 4 is completed

Phase 5: Create a load balancer .

First step is to create a target group then create an application load balancer.

While creating the load balancer , I selected the security group which I have created in phase 1.

I have successfully created a load balancer.

Phase 5 is completed

Phase 6: Validate

we need to copy our elb endpoint and copy it to our browser

As you see the frontend is working

Time to check the backend

The database is working

And when I click on any of the users it shows that it is also cached successfuly

--

--

Semo
Semo

Written by Semo

0 Followers

UK based Junior Cloud Engineer with 3 AWS certifications, Azure Fundamentals www.linkedin.com/in/mohammed-zitouni-234b34240

No responses yet