Aws 1713927989
Aws 1713927989
in
AWS
Introduction
A three-tier application architecture is a common design pattern used
to develop and deploy applications, consisting of three layers:
-Presentation Tier
-Application Tier
-Data Tier
-Each tier serves a specific purpose and can be scaled, managed, and
deployed independently.
Three Tiers
Presentation Tier Application Tier Data Tier
• This tier is responsible for • This tier contains the business • This tier stores and manages
handling user interactions and logic and application data used by the application.
presenting information to processing components of the • Common components in this
users. system. tier include databases, data
• Common components in this • Common components in this warehouses, and data lakes.
tier include web servers, load tier include application servers, • AWS services commonly used
balancers, and content delivery APIs, and middleware. for the data tier include:
networks (CDNs). • AWS services commonly used -Amazon RDS
• AWS services commonly used for the application tier include: -Amazon DynamoDB
for the presentation tier -Amazon EC2 or AWS Lambda -Amazon Redshift
include: -Amazon API Gateway -
-Amazon EC2
-Elastic Load Balancing
(ELB)
-Amazon CloudFront
-Elastic Beanstalk
Architecture
Setup
• Download source code from below GitHub repository to your local
machine:
“https://github.com/aws-samples/aws-three-tier-web-architecture-
workshop.git”
2.Subnet Creation-
i) Create 6 subnets across 2 AZ.s within created VPC. That means
that three subnets will be in one availability zone, and three
subnets will be in another zone.
ii) Specify unique CIDR range for each subnet.
Internet Connectivity
Installation-
Install following package for mysql server using wget command:
wget https://repo.mysql.com/mysql57-community-release-el7.rpm
Initiate your DB connection with your Aurora RDS writer endpoint. In the following
command, replace the RDS writer endpoint and the username, and then execute it
in the browser terminal:
mysql -h CHANGE-TO-YOUR-RDS-ENDPOINT -u CHANGE-TO-USER-NAME –p
Enter password and connect to your database
Database Operations
i) Create a database called webappdb with the following command using
the MySQL CLI:
CREATE DATABASE webappdb;
ii) You can verify that it was created correctly with the following command:
SHOW DATABASES;
iii) Create a data table by first navigating to the database we just created:
USE webappdb;
iv) Create the following transactions table by executing command:
CREATE TABLE IF NOT EXISTS transactions(id INT NOT NULL
AUTO_INCREMENT, amount DECIMAL(10,2), description
VARCHAR(100), PRIMARY KEY(id));
Database Operations
v) Verify the table was created:
SHOW TABLES;
vi) Insert data into table for use/testing later:
vii) INSERT INTO transactions (amount,description) VALUES
('400','groceries’);
viii) Verify that your data was added by executing the following
command:
SELECT * FROM transactions;
ix) When finished, just type exit and hit enter to exit the MySQL client.
Configure App Instance
Open the application-code/app-tier/DbConfig.js file from the GitHub repo and edit for the hostname, user,
password and database.
1.S3 Bucket Creation-
i) Navigate to the S3 service and create a new S3 bucket.
ii) Give it a unique name, and then leave all the defaults as in.
iii) Upload the app-tier folder to the S3 bucket.
iv) Go back to your SSM session. Start by installing NVM (node version manager) using following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
v) Next, install a compatible version of Node.js and make sure it's being used.
nvm install 16
nvm use 16
vi) PM2 is a daemon process manager that will keep our node.js app running when we exit the instance.
npm install -g pm2
Configure App Instance
vi) Now we need to download our code from our s3 buckets onto our
instance. In the command below, replace BUCKET_NAME with the
name of the bucket:
cd ~/
aws s3 cp s3://BUCKET_NAME/app-tier/ app-tier –recursive
vii) Navigate to the app directory, install dependencies, and start the app with pm2.
cd ~/app-tier
npm install
pm2 start index.js
viii) To make sure the app is running correctly run the following: pm2 list
To look at the latest errors, use this command: pm2 logs
ix) Right now, pm2 is just making sure our app stays running when we leave the SSM session.
pm2 startup
x) Save the current list of node processes with the following command:
pm2 save
Test App Tier
1.On EC2 dashboard select Load Balancers under Load Balancing and
click Create Load Balancer.
2. Application Load Balancer is for our HTTP traffic so click the create button
for that option.
3.After giving the load balancer a name, be sure to select internal since this
one will not be public facing, but rather it will route traffic from our web
tier to the app tier.
4.Select the correct network configuration for VPC and private subnets.
5.Select the security group we created for this internal ALB. Now, this ALB
will be listening for HTTP traffic on port 80. It will be forwarding the traffic
to our target group that we just created, so select it from the dropdown,
and create the load balancer.
Launch Template
1. Create the Auto Scaling Group for our app instances. On EC2 dashboard
navigate to Auto Scaling Groups under Auto Scaling and click Create Auto
Scaling group.
2.Give your Auto Scaling group a name, and then select the Launch Template
we just created and click next.
3.On the Choose instance launch options page set your VPC, and the private
instance subnets for the app tier.
4.For this next step, attach this Auto Scaling Group to the Load Balancer we
just created by selecting the existing load balancer's target group from the
dropdown. Then, click next.
5.For Configure group size and scaling policies, set desired, minimum and
maximum capacity to 2. Click skip to review and then Create Auto Scaling
Group.
Web Tier Instance Deployment
• Follow the same steps used to connect to the app instance and
change the user to ec2-user. Test connectivity here via ping as well
since this instance should have internet connectivity:
sudo -su ec2-user
ping 8.8.8.8
Configure Web Instance
1. Start by installing NVM and node :
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16
2. Now we need to download our web tier code from our s3 bucket:
cd~/
aws s3 cp s3://BUCKET_NAME/web-tier/ web-tier --recursive
3. Navigate to the web-tier folder and create the build folder for the react app:
cd ~/web-tier
npm install
npm run build
4. NGINX can be used for different use cases like load balancing, content caching etc, but we will be
using it as a web server that we will configure to serve our application on port 80, as well as help
direct our API calls to the internal load balancer.
sudo amazon-linux-extras install nginx1 -y
Configure Web Instance
5. Navigate to the Nginx configuration file with the following commands and
list the files in the directory:
cd /etc/nginx
ls
6. Then, restart Nginx with the following command:
sudo service nginx restart
7. To make sure Nginx has permission to access our files execute this
command:
chmod -R 755 /home/ec2-user
8. And then to make sure the service starts on boot, run this command:
sudo chkconfig nginx on
To test if your entire architecture is working, navigate to your external
facing loadbalancer, and plug in the DNS name into your browser amd
then hit enter.
It will display your web page by performing appropriate function.
Thank you!