Deployment of a Highly Available Employee Management Portal on AWS Cloud

Introduction:
In today’s blog, we will investigate the exciting voyage of deploying a multi-tiered employee management application on the AWS cloud. As a database administrator for an IT company, I was required to create a new database instance and attach it to the web server hosting the employee administration portal. Join me as I walk you through Amazon Web Services (AWS)’s step-by-step infrastructure creation process.

Dr Priyanka Gupta Research
10 min readJul 13, 2023
Image by Author (Priyanka Gupta)!

Working without cloud in the present world is unthinkable. Therefore, it's very important to know how to work with cloud in addition to the general/specific concepts that are required to fulfill a specific use-case that we have.

The process to set up a multi-tier application on AWS!

The step-by-step process to setup a multi-tier application on AWS is as follows.

Step 1: Launch the AWS Management Console.

  • To access the AWS Management Console (https://console.aws.amazon.com), open up your preferred web browser and visit the link, then click on the “Sign-In” option. Provide your AWS account information, such as your email address or account ID and password. (Please note that if you do not already have an AWS account, you need to sign up on the AWS website.)
  • Finally, log into the AWS Management Console, and you’ll have access to all the resources you need to launch and maintain your cloud-based operations. After logging in, you may create the database instance and set up the web server in accordance with the guidelines provided.

Note: Always keep your AWS credentials safe and use the right security settings to prevent unauthorized access and protect your infrastructure and data.

Step 2: Create an EC2 Instance by following these steps:

  • Navigate to EC2 (Elastic Cloud Computing) console.
  • To launch an instance, click “Launch Instance.”
  • Choose an Amazon Machine Image (AMI) that suits your requirements. Select an instance type based on your computing needs.
  • Here Amazon Linux 2023 is selected to launch the instance.
Image by Author (Priyanka Gupta)!
  • Configure the Instance-specific settings: here t2.micro is selected as an Instance type as it was available as a free tier. With this setting, we have selected 1 CPU and 1 Giga byte memory size.
  • Generate a key pair and attach it to the instance.
Image by Author (Priyanka Gupta)!
  • In the Network settings, allow HTTP access so that the webserver can be accessed from anywhere.
Image by Author (Priyanka Gupta)!
  • Now attach the GP2 EBS volume of 8 GB to the instance. (By Default Settings)
Image by Author (Priyanka Gupta)!
  • Now review the Configuration and launch the instance.
Webserver Launched — Image by Author (Priyanka Gupta)!

Step 3: Create an Instance of MySQL Database using AWS RDS:

  • Search “RDS” in the AWS services search bar to access the Relational Database Service (AWS RDS) console.
  • Select “Create database.”
  • Select “Standard Create” as the method for creating the database, this will allow you to get more control over the configuration and customization options for MySQL database instance. It provides advanced settings and options for fine-tuning database setup according to user-specific requirements as compared to “Easy Create”
  • In the Engine option select the MySQL as engine
Image by Author (Priyanka Gupta)!
  • Configure the database, including the DB instance identifier, set the master username, and the master password to access the database.
Image by Author (Priyanka Gupta)!
  • Used db.t2.micro as the DB type to access the free tier service, & keep the storage settings as it is (you can change the storage settings if required, but by default, it is giving 20 GB storage with autoscaling enabled, so that is enough. However, you can change the storage threshold if required).
Image by Author (Priyanka Gupta)!
  • Modify the other optional parameters as necessary.
  • Connect it to an existing EC2 instance. This will connect the DB automatically to the EC2 Instance in which the web server will be setup.
Image by Author (Priyanka Gupta)!
  • Select the correct VPC, security group, and subnet for the configuration. Select “no public access”, this will ensure the safety of the database as it prevents outside/public connections to the DB.
Image by Author (Priyanka Gupta)!
  • Keep the settings for backup, monitoring, and maintenance as it is because there is no change required there as per the task/use-case i.e., demonstration purpose (If you need to modify the settings, then you can do that as per your requirement).
  • In the additional configuration, I have mentioned an initial database name “priyanka_database” to ensure that this database is created in the RDS Instance when it is launched (you can give any other name to the database as per your requirement) & kept the rest of the settings as it is.
Image by Author (Priyanka Gupta)!
  • Review the configuration and then select “Create database” to initiate the establishment of the database instance.
RDS Database created — Image by Author (Priyanka Gupta)!

Step 4: Connect the Web Server to the Database’s Instance (without autoscaling):

  • Connect using SSH to the EC2 instance using the below-mentioned command.
ssh -i <key-pair path> ec2-user@<Public IP of the EC2 Instance>
  • Switch to the root user.
sudo su - 
Image by Author (Priyanka Gupta)!
  • Install the essential software packages (Apache Webserver, PHP, php-mysqli, mysql client) by using the following command.
dnf install httpd php php-mysqli mariadb105 -y
Image by Author (Priyanka Gupta)!
  • Start and enable the services of the webserver using the command executed in the below image.
Image by Author (Priyanka Gupta)!
  • Set the proper permissions for the webserver files & directories using the commands executed in the below image..
Image by Author (Priyanka Gupta)!
  • Retrieve the MySQL database instance’s endpoint from the RDS console.
Image by Author (Priyanka Gupta)!
  • Configure the database configurations in the EC2 instance so that the webserver can connect to it & the application can be hosted perfectly.
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
<?php

define('DB_SERVER', '<endpoint of the RDS Database>');
define('DB_USERNAME', '<username>');
define('DB_PASSWORD', '<password>');
define('DB_DATABASE', '<initial database name>');

?>

Note: Put the above code after placing the correct values used by you in the placeholders in the “conf.inc” file created above. Absolute path of the file is: “/var/www/conf/conf.inc”

Image by Author (Priyanka Gupta)!
  • Now in the by-default document root of the webserver i.e., “/var/www/html”, create a file with any name, I have used the name “page.php”, & write the below-mentioned code in that.

<?php include "../conf/conf.inc"; ?>
<html>
<body>
<h1>Sample page</h1>
<?php

/* Connect to MySQL and select the database. */
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

$database = mysqli_select_db($connection, DB_DATABASE);

/* Ensure that the EMPLOYEES table exists. */
VerifyEmployeesTable($connection, DB_DATABASE);

/* If input fields are populated, add a row to the EMPLOYEES table. */
$employee_name = htmlentities($_POST['NAME']);
$employee_address = htmlentities($_POST['ADDRESS']);

if (strlen($employee_name) || strlen($employee_address)) {
AddEmployee($connection, $employee_name, $employee_address);
}
?>

<!-- Input form -->
<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">
<table border="0">
<tr>
<td>NAME</td>
<td>ADDRESS</td>
</tr>
<tr>
<td>
<input type="text" name="NAME" maxlength="45" size="30" />
</td>
<td>
<input type="text" name="ADDRESS" maxlength="90" size="60" />
</td>
<td>
<input type="submit" value="Add Data" />
</td>
</tr>
</table>
</form>

<!-- Display table data. -->
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>ID</td>
<td>NAME</td>
<td>ADDRESS</td>
</tr>

<?php

$result = mysqli_query($connection, "SELECT * FROM EMPLOYEES");

while($query_data = mysqli_fetch_row($result)) {
echo "<tr>";
echo "<td>",$query_data[0], "</td>",
"<td>",$query_data[1], "</td>",
"<td>",$query_data[2], "</td>";
echo "</tr>";
}
?>

</table>

<!-- Clean up. -->
<?php

mysqli_free_result($result);
mysqli_close($connection);

?>

</body>
</html>


<?php

/* Add an employee to the table. */
function AddEmployee($connection, $name, $address) {
$n = mysqli_real_escape_string($connection, $name);
$a = mysqli_real_escape_string($connection, $address);

$query = "INSERT INTO EMPLOYEES (NAME, ADDRESS) VALUES ('$n', '$a');";

if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");
}

/* Check whether the table exists and, if not, create it. */
function VerifyEmployeesTable($connection, $dbName) {
if(!TableExists("EMPLOYEES", $connection, $dbName))
{
$query = "CREATE TABLE EMPLOYEES (
ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(45),
ADDRESS VARCHAR(90)
)";

if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
}
}

/* Check for the existence of a table. */
function TableExists($tableName, $connection, $dbName) {
$t = mysqli_real_escape_string($connection, $tableName);
$d = mysqli_real_escape_string($connection, $dbName);

$checktable = mysqli_query($connection,
"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");

if(mysqli_num_rows($checktable) > 0) return true;

return false;
}
?>

  • Now, hit the webserver IP/URL with “HTTP” protocol.
Image by Author (Priyanka Gupta)!
  • Now hit the Webserver URL with the “page.php” route. It will showcase the final application hosted on top of AWS.
Image by Author (Priyanka Gupta)!

Step 5: Implement Web Server Automatic Scaling:

This will do whatever Step 4 will do. However, this step 5 will additionally configure the autoscaling for the webserver. Therefore, if you want autoscaling to be configured for your webserver, then in that case, you can completely skip step 4, & follow this step 5.

  • Use the EC2 Auto Scaling tab to create an Auto Scaler for the Auto Scaling of the EC2 Instances to manage the load.
Image by Author (Priyanka Gupta)!
  • Create a launch template for the Auto Scaler as per the requirements.
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
  • In the “Advanced Details” section of the Launch Template, write the below script in the “user data”, so that the webserver is automatically properly configured in every auto-scaled instance.
#!/bin/bash
sudo dnf update -y
sudo dnf install -y httpd php php-mysqli mariadb105
sudo systemctl enable --now httpd
sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
mkdir -p /var/www/configuration
echo -e "<?php" > /var/www/configuration/configuration.inc

echo "define('DB_SERVER', 'Endpoint of DB');" >> /var/www/configuration/configuration.inc

echo "define('DB_USERNAME', 'username');" >> /var/www/configuration/configuration.inc

echo "define('DB_PASSWORD', 'password');" >> /var/www/configuration/configuration.inc

echo "define('DB_DATABASE', 'initial database name');" >> /var/www/configuration/configuration.inc

echo "?>" >> /var/www/configuration/configuration.inc

sudo wget https://raw.githubusercontent.com/HarshitDawar55/webserver-database-connection/main/page.php

sudo mv page.php /var/www/html/index.php

Note: In the above script, make sure to change the IP Address, username, password, & database name with the one that you have in your case.

Image by Author (Priyanka Gupta)!
  • Create an Auto Scaling group with the desired settings and configure it.
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
Image by Author (Priyanka Gupta)!
  • Define scaling policies using the required metrics. “Average CPU utilization” is used in this case where the target value is set to 50, which means if the average CPU Utilization crosses 50%, then a new Instance will be added.
Image by Author (Priyanka Gupta)!
  • Configure scaling triggers and instance’s “Desired”, “Minimum”, and “Maximum” limits. In the present case, they are been set to 1, 1, and 10 respectively.
Image by Author (Priyanka Gupta)!
  • Now, hit the webserver IP & then test the functionality of the webserver & Database (2 Tier Architecture in this case).

Note: If successfully connected, you can very easily store the employee data securely in the database. This is only for demonstration purpose, while implementing for the company, you have to change the code to do not display the data from the database on the webpage. Some API’s can also be created out of this project, & then can be used in the application/microservices.

Congratulations! You have successfully created a new database instance on AWS and connected it to a web server hosting the employee management portal. By leveraging the power of AWS, we have built a highly available, and scalable infrastructure that can handle traffic surges and securely store employee data. Remember to regularly monitor and optimize your resources to ensure the continued efficiency of your application. Happy managing!

--

--