On-premises workload (application and its database) migration to AWS using Amazon EC2 and RDS services.
Tags: aws, devops, appmigration, rds
Published: 2023-06-17 19:57:36

Introduction:
In this project, we will go through the process of migrating a python web application and its database data using the Lift & Shift (rehost) model to AWS following these migration steps of Planning, Execution and Go-live as required.


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


Solution Architecture Diagram.
Before creating our EC2 and RDS instances, we need to create a VPC and its subnets(public and private subnets). Let's follow the steps below.
Creating VPC and the Subnets:


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


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



Virtual Private Cloud (VPC) createdPublic and Private SubnetsPlease note that AWS RDS Subnet Group is a collection of subnets that you can use to deploy your RDS database in a VPC.
Your VPC must have at least 2 subnets and these subnets must be in different Availability Zones.
Now, we need an internet gateway which will allow the traffic from the internet to reach our Application server (EC2 instance) and vice versa. This means our corporate users (clients) will be able to access the application server over the internet and the server will also be able to download required packages or updates from the internet when initiated. The internet gateway must be attached to the VPC.
Creating an Internet Gateway, attaching it to a VPC and creating a Route

VPC - Internet Gateway: igw-web-infrastructure01 | Action: Attach to VPC (vpc-web-infrastructure01)

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

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


Internet Gateway created

VPC - Route Table | Routes | Edit routes
Add route: 0.0.0.0/0 | Target: Internet Gateway (igw-web-infrastructure01)

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


Route added

Any traffic from the internet will now be routed through the internet gateway.
Creating the EC2 Instance:

Now, we can create our EC2 instance using a free-tier eligible (t2.micro) Ubuntu server, create a key-pair to securely connect to the instance, edit the "Network setting" and select the appropriate VPC and public subnet for the instance. Enable "Auto-assign Public IP". Create a set of firewall rules (security group) that control the traffic for the instance.
Accept the default storage configuration settings as this is sufficient for the application to be deployed. Then launch the instance.

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

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



> Note that in this demo project, rules with source of 0.0.0.0/0 allow all IP addresses to access the instance. For best practice, I recommend setting security group rules to allow access from known or trusted IP addresses only. You should not do this in a production environment.

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


EC2 Instance created

Creating RDS:

RDS - Choose a database creation method - Standard create
Engine type: MySQL | Engine version: MySQL 5.7.xx | Template: Free tier
Choose a name for the database instance "web-infra-db01". choose a password for the default Master username "admin". For the purpose of this demo project, ensure db.t2.micro is selected for the DB instance class. Accept the default storage and connectivity settings. Ensure the project's VPC is selected. Keep the default security group. Select the Availability zone of the EC2 instance "us-east-1a".Accept all other default settings and click "create database".

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


RDS MySQL instance created

Connecting to the EC2 instance:

Incase you do not have git bash installed already, download it from here https://git-scm.com/downloads
To securely connect to the EC2 instance, run the comand below from the git bash. Ensure that you are currently in the directory where the private key was saved before running the secure shell command.

```
ssh -i "your-pivate-key-name" ubuntu@"EC2-public-IP-address"
```

If this runs successfully, you will be welcomed with a similar window with system information of your EC2 instance as shown below.


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


Installing the dependencies for the web application:

Here, I will create a bash script to automate the installation of the required libraries and dependecies for the application.
At the EC2 instance secure shell home directory, create a file called "app-requirement.sh" and assign execute permission to the script file. Using a preferred shell enviroment text editor, list all the required commands to be executed in the bash script and run the script.

```
touch app-requirement.sh && sudo chmod u+x app-requirement.sh
```

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


Go Live section:
Creating a Security Group for RDS

VPC | Security | SG - Create a new security group e.g EC2–RDS-sg01 which will allow access to MySQL by the application running at EC2 instance.
Select the VPC created while setting up the infrastructure i.e "vpc-web-infrastructure01".
Set the Inbound rules - Inbound rules | Add rule | Type: MYSQL/Aurora | Destination: 0.0.0.0/0.
Associating the SG (EC2-RDS-sg01) to the RDS instance (web-infra-db01):

RDS | DB Instances | web-infra-db01 | Modify - Connectivity | SG: EC2-RDS-sg01 - Continue.
Select "Apply immediately" and click "Modify DB instance" button.
Confirm this was effected.
- Connecting to the EC2 instance
Ensure that a test SSH connection to the EC2 instance is successful.
Downloading the Aplication and the 'Dump' files from database:

The deployment files from the application and the database dump files were exported from the on-premises application server and database server to Amazon S3. Therefore, we would need to download these files from the S3 bucket to the EC2 instance home directory.

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


It is now time to open a remote connection to MySQL RDS instance replacing the placeholder "rds_endpoint" with the endpoint name from RDS instance.

```
mysql -h <rds_endpoint> -P 3306 -u admin -p
```

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



Creating a DB 'wikidb' and importing data to it:

```
show databases;
create database wikidb;
show databases;
use wikidb;
show tables;
source dump.sql;
show tables;
select * from articles;
```
- Creating a user 'wiki' in the "wikidb"

```
CREATE USER wiki@'%' IDENTIFIED BY 'admin123456';
GRANT ALL PRIVILEGES ON wikidb.* TO wiki@'%';
FLUSH PRIVILEGES;
EXIT;
```
Unziping the application deployment file

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


```
unzip wikiapp.zip
```
Editing the file 'wiki.py'

Change the directory to the app folder and edit the connection strings for the database in the MySQL configuration section of the file. Ensure that the host detail is replaced with the RDS MySQL database endpoint and save the file.

```
cd wikiapp/
vi wiki.py
```
Let's now test or validate the application - Run python3 wiki.py

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

 
Copy the public IP address of the EC2 instance to the address bar with the port number 8080. If this is suucessful, application will be displayed. Therefore our application running on the EC2 instance was able to connect to the database. And we are able to login and create content in the application.


**Summary**
In this demo project, we created a VPC, private subnet for the RDS and public subnet for EC2 instance. We also created an Internet gateway to allow traffic from the internet to reach the instance. I created an EC2 instance and RDS instance. I connected through secure shell (ssh) to the EC2 instance and installed all the dependencies for the web application using a script file.I downloaded the application deployment files and database dump files from S3 bucket to the EC2 instance, connected to the RDS MySQL database and created a database, imported the data into the database, created a user to connect to the database,edited the app deployment file to replace the endpoint from the op-premises database to point to the RDS database.I validated the application using the EC2 instance IP address with the required port number.

Thanks for your time and I hope it is worthwhile.

Cheers!