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

Bolaji A. Adetoye
7 min readJun 17, 2023

--

Photo credit: serverguy.com

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.

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:
Virtual Private Cloud (VPC) created
Public and Private Subnets

Please 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)

Internet Gateway created

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

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.

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.

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”.

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.

  • 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

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.

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
  • 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
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

Copy the public IP address of the EC2 instance to the address bar with the port number 8080. If this is successful, the application will be displayed. Therefore the application running on the EC2 instance was able to connect to the database.

Summary

In this demo project, I created a VPC, private subnet for an RDS and a public subnet for an EC2 instance. I 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 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.

Please leave a comment and give me a clap if this is informative.

--

--

Bolaji A. Adetoye

IT Tech. Support | Aspiring MultiCloud & DevOps Engineer | AWS | Microsoft Azure | Google Cloud | Oracle Cloud