DEV Community

Bolaji A. Adetoye
Bolaji A. Adetoye

Posted on

On-premises workload (application and its database) migration to AWS using Amazon EC2 and RDS services.

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

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

Image description

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

Image description

Internet Gateway created

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

Image description

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

Image description

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

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

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"
Enter fullscreen mode Exit fullscreen mode

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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

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
Enter fullscreen mode Exit fullscreen mode

Image description

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;
Enter fullscreen mode Exit fullscreen mode
  • Creating a user 'wiki' in the "wikidb"
CREATE USER wiki@'%' IDENTIFIED BY 'admin123456';
GRANT ALL PRIVILEGES ON wikidb.* TO wiki@'%';
FLUSH PRIVILEGES;
EXIT;
Enter fullscreen mode Exit fullscreen mode

Unziping the application deployment file

Image description

unzip wikiapp.zip
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Let's now test or validate the application - Run python3 wiki.py

Image description

 
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!

Top comments (0)