Multi-Tier-Website Using AWS EC2

Description about the project : The company 'ABC' wants to move their product to AWS. They have the following things setup right now :

  1. MySQL DB
  2. Website PHP
The company wants the high availability on this product, therefore wants Auto Scaling to be enabled on this website.

Steps to solve :

1.Launch an EC2 instance
2.Enable Auto Sacling on these instances (minimum 2)
3.Create a RDS instance
4.Create Database & Table in RDS instances:
(a) Database name :intel 
(b) Table name : data
(c) Database password : farro123
5.Change Hostname in website
6.Allow traffic from EC2 to RDS instance
7.Allow all traffic to EC2 instance 

The whole Architecture of this project will be like this ๐Ÿ‘‡





Step1:  launch a EC2 instance  (virtual machine) 
  • with a key (.pem file) 
  • security group with port 80 for HTTP ,22 for SSH,3306 for MySQL enable 
  • chose default VPC and Public Subnets  
  • install Apache2 and PHP on it to host the website
  • use #apt install apache2 for install apache web server
  • use #apt install php libapache2-2-mod-php php-mysql -y

Step2: Create a RDS instance 
  • Engine : select MySQL(latest version)
  • Use private subnet for hosting RDS
  • DB instance identifier : "database-2"
  • Master name : "intel"
  • Password : "farro123"
  • Security group : attach a new or existing SG that allows MySQL(3306) inbound from EC2.
  • DB name : "intel" 
  • let other things be default
  • Click create database 
After sometime you will get your RDS endpoint , copy that RDS endpoint and connect it with your EC2 .
How to test the connection of RDS from EC2 :
step1; on your ec2 type ---  apt install -y mysql-client 
step2: Mysql -h <RDS-endpoint> -P 3306 -u (master_name) -p (password_of_rds)

 Step3: Create image (AMI) and Template :

  •  Now we will use the running EC2 instance for creating image (ami-aws-multi-tier)


  •  Then we will create a launch template (aws-project-temp)


  • Then we will create an Auto Scaling group using this template. 
  • Now you can  terminate the main EC2 and only use the Auto scaling instances.


Step4: Create a Auto scaling group "AWS-Project-1"
  • Desired capacity :2
  • minimum :1
  • maximum capacity:4
Create 2 dynamic auto scaling policy :
  •  add-ec21 instances if the CPU utilization goes over 60%.
  •  remove-ec2 1 instance if the CPU utilization goes below 40%.


So that there are always at least 2 EC2 instances are running and if traffic increases , AWS automatically adds more instances and vice versa.

Testing of Auto scaling :

๐Ÿ‘‰Run a stress test inside your instance # stress-ng --cpu 2or4 --timeout 300s it will run the load test for 300 seconds and pushes CPU  utilization to 100%.

Before the stress code it has only one EC2 running:

๐Ÿ‘‰After 1 minute its already Scaling out : increasing the number of instances 
Step5: Creating a database and table : Either you can create a initial database while creating a RDS  or connect to ec2 using RDS endpoint and in MySQL monitor use 
  • create database intel;
  • use intel;
  • create table data (id INT etc)
Step6: Changing the hostname of website : Copy PHP websites files into the root directory used by the Apache web server -/var/www/html : Naming 
  • Fetch_table_name.php
  • Create_table.php
  • README.md
  • index.html
Files link in GitHub ๐Ÿ‘‰ https://github.com/bilsgotchills/Multi-tier-Website-using-EC2.git

 
๐Ÿ”†To change the host name in website edit both the files fetch_table_name and create _table.php :
change the
  •  $servername  = "RDS endpoint"
  •  $username  =  "your username in RDS"
  • $password = "password you set in RDS "
  •  $dbname = "your db name "


  • Save the file
  • Restart the Apache2 server 
Now use the public IP of EC2 to test it  ----   #http://<your-public-ip>/ 


Step7: Allow traffic from EC to RDS instance
  • Note the security group name of yours RDS instance 
  • Go to EC2 -- security group - select the security group used by your RDS
  • Edit the inbound rules -- MySQL(3306)
  • Source will be custom --EC2 instance security group   

This will ensure that your EC2 can communicate with your RDS.
Step8: Allow all traffic to EC2 instance 
  • Go to your EC2 > security group 
  • Click on your EC2 security group 
  • Edit the inbound rules 
  • Type : All traffic
  • Protocol: All
  • Port : All
  • Source : 0.0.0.0/0(IPv4)
This means anybody on the internet can reach your EC2 .

Conclusion : This project successfully demonstrated the deployment of a secure and scalable multi-tier architecture on AWS. 

Learnings : 
  • Better understanding of security groups
  • Settings up of Cloud watch alarms
  • AMI and Templates
  • Connecting of RDS with EC2 instances

Challenges faced : 
  • Setting up of Auto scaling polices
  • Changing the hostname in website
  • Setting up of security groups.


Comments

Popular posts from this blog

Text-to-Speech Narrator on AWS

AWS-Event-Announcement-project