Deploying A Multi-Tier Website Using AWS EC2



statement :

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Using Amazon EC2 eliminates your need to invest in hardware up front so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.

Problem Statement: Company ABC wants to move their product to AWS.

They have the following things set up right now:

1. MySQL DB

2. Website (PHP)

The company wants high availability on this product, therefore wants Auto Scaling to be enabled on this website

STEPS :

we need to setup a infra.

  1. Create a VPC :

  1. Create a Subnet :

create a 3 subnet , we will assign 2 subnet for public route table which has internet facing and 1 for private Route table .

  • we need to create subnet in two az ( so 1 pub and 1 pvrt in one az ) and ( another in different az )

i will tell why we have taken 3 subnets in total .

  1. Create and attach the IGW to the VPC

  1. Create a NAT gateway

which is assigned to private subet

  • We are taking 2 subnet as public
  1. create a 2 route table : 1 for public and 1 for private

  2. Assign the 2 subnets for for public

  1. Assign NAT Gateway to the Private Subnet and Associate the Private Subnet to the private Route Table

  2. Create a 2 EC2

-- Make sure that you use the VPC and subnets we have created

#one Ec2 is created from the public subnet and

#another Ec2 is created from the private subnet

  1. connect to the public Ec2 via SSH

  1. SSH ( connect ) to the Private from the Public ec2

cmd:
- vi key.pem:
copy paste the pem file content of the private ec2 and save it

cmd:
- ssh -i key.pem <username>@<public_ip_address>

  1. Create a Mysql Database
  • make sure that mysql is attached to the private ec2

  1. connect to the DB from priavte Ec2 :
  1. create a table
  • make sure that the column variables are firstname and email

  1. Install apache2 in private ec2
  • sudo apt install apache2

  • sudo systemctl start apache2

  1. Create a load balancer
  • create a ALB

  • and add the private ec2 as the target group

******* Now as i said in the beginning that why we have taken 3 subnet - ( to create the alb we need to specify the two subnet which is in different az )*******

  • copy and paste the DNS in browser :

  1. Now change the source code that is ( index.html )
  • cd /var/www/html

  • rm index.html

  • vi index.php

  • and add the below code

<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<body background="images/2.png" style="background-repeat:no-repeat;
background-size: 100% 100%">
<br><br><br><br>
<div class="container">
  <div class="jumbotron vertical-center">
      <table class="grid" cellspacing="0">
          <tr>
  <td colspan="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  <td colspan="4">
      <form method="post">
  <div class="form-group" action="post">
    <label for="firstname">Name:</label>
    <input type="text" class="form-control" name="firstname">
  </div>
  <div class="form-group">
    <label for="email">Email:</label>
    <input type="text" class="form-control" name="email">
  </div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-success">Submit</button>
</form></td>  
  <td colspan="4"></td>
</tr>
</table>
</div>
</div>
<?php
$firstname=$_POST['firstname'];
$email=$_POST['email'];
$servername = "database-1.c521xebtvy55.us-east-1.rds.amazonaws.com";
$username = "admin";
$password = "1234Qwer";
$db = "projectmysql";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['firstname']) && isset($_POST['email'])){
$sql = "INSERT INTO data (firstname,email)
VALUES ('".$firstname."', '".$email."')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
</body>
</html>
  • changes need to done in the above code
$servername = "database-1.c521xebtvy55.us-east-1.rds.amazonaws.com";
$username = "admin";
$password = "1234Qwer";
$sql = "INSERT INTO data (firstname,email)
  • under
    - servername = give the endpoint of DB
    - username = DB username
    - password = password specified for DB
    - sql = change the data with the name of the table
  1. install the php dependencies
 sudo add-apt-repository -y ppa:ondrej/php
 sudo apt install php5.6 mysql-clinet php5.6-mysqli
 sudo apt install php5.6 mysql-client php5.6-mysqli
  1. Refresh the page of ALB DNS link

  1. check the values in DB

    • login in to the Db
Use DB;
show tabels;
select * from <table_name>

  1. Attach a Auto-Scalling
  • Take Ami of the ec2

  • Create a Launch Configuration :

    • specify the AMI

  • Go to Auto-Scalling

    • Attach the existing Load Balancer

    • and create a Auto_scalling

*********Now as per the requirement the application is highly available*********