Deploy a 3 Tier Architecture On AWS

Venkateswara Reddy - Jul 20 '23 - - Dev Community

Image description

Requirements:

  1. Auto Scaling Groups
  2. Application Load Balancer
  3. Target Groups
  4. Launch Template
  5. AMI
  6. Ec2
  7. Amazon Aurora DB & subnet Group
  8. NAT
  9. Elastic IP
  10. Route Table
  11. Internet Gateway
  12. VPC
  13. Subnets
  14. Snapshots

Detailed steps : https://lnkd.in/gpJ9ZsUE
Detailed video: https://youtu.be/amiIcyt-J2A

1.GIT-CLONE:
Create a directory in local and Clone the code from github by using GitBash URL is:
git clone https://github.com/aws-samples/aws-three-tier-web-architecture-workshop.git
2.S3 BUCKET:
create a s3 bucket make sure use correct region through out the project we r using only one region and let the all settings by default and create s3 bucket.
3.IAM-ROLE:
create an IAM role with AmazonSSMManagedInstanceCore and AmazonS3ReadOnlyAccess
4.VPC:
create a vpc with cidr 10.0.0.0/16
5.SUBNETS:
create 6 subnets in 2 different available zones and each availability Zone 3 Subnets and each AZ 2 PRIVATE and 1 PUBLIC Subnet will be there. those are like below:

1.
NAME:Public-web-subnet-AZ1
CIDR:10.0.0.0/24
REGION:US_EAST_1A
2.
NAME:Public-web-subnet-AZ2
CIDR:10.0.1.0/24
REGION:US_EAST_1B
3.
NAME:Private-subnet-AZ1
CIDR:10.0.2.0/24
REGION:US_EAST_1A

4.
NAME:Private-subnet-AZ2
CIDR:10.0.3.0/24
REGION:US_EAST_1B
5.
NAME:Private-DB-subnet-AZ1
CIDR:10.0.4.0/24
REGION:US_EAST_1A
6.
NAME: Private-DB-subnet-AZ2
CIDR:10.0.5.0/24
REGION:US_EAST_1B

6.INTERNET GATEWAYS:
create an internet gateway with name: _________.
7.NAT GATEWAYS:
create 2 NAT gateways in 2 different AZ each AZ one NAT Gateway should create. In AZ1 and AZ2 Names like public-web-subnet-AZ1 and public-web-subnet-AZ2 respectively and allocate EIP for both while creating itself and then create finally 2 NAT gateways.
8.ROUTE TABLE:
create 3 Route Tables
1.
Name: public-RT
VPC: custom
Edit Routes: Destination is  0.0.0.0/0 with Target IGW
Subnet Association : 2 public Subnets  Public-Web-Subnet-AZ1 & Public-Web-Subnet-AZ2
2.
Name: private -RT-AZ1
VPC: Custom
Edit Routes: Destination is 0.0.0.0/0 and Target is NAT Gateway of AZ1
Subnet Association: Private-subnet-Az1
3.
Name: private -RT-AZ2
VPC: Custom
Edit Routes: Destination is 0.0.0.0/0 and Target is NAT Gateway of AZ2
Subnet Association: Private-subnet-AZ2

9.SECURITY GROUP:
We have to create 5 SGs.
1.
Name: InternetFacing-lb-SG
Description: external load balancer sg
VPC: custom
Inbound rules-1 : Type HTTP and port range is 80 and Source Type AnyWhere-IPV4 and Source  0.0.0.0/0
Inbound rules-2 : Type Custom TCP and port range is  0 and Source Type AnyWhere-IPV6 and Source  0.0.0.0/0
2.
Name: WebTier-SG
Description: webtier sg
VPC: custom
Inbound rules-1 : Type HTTP and port range is 80 and Source Type Custom and Source  InternetFacing-lb-SG
Inbound rules-2 : Type HTTP and port range is  80 and Source Type My_IP and Source  IP(ofter selecting My-ip By default ip will come)
3.
Name: Internal-LB-SG
Description: internal sg
VPC: custom
Inbound rules-1 : Type HTTP and port range is 80 and Source Type Custom and Source  WebTier-SG
4.
Name: Privateinstance-SG
Description: Privateinstance sg
VPC: custom
Inbound rules-1 : Type Custom TCP and port range is 4000 and Source Type Custom and Source  Internal-lb-SG
Inbound rules-2 : Type Custom TCP and port range is  4000 and Source Type My_IP and Source  IP(ofter selecting My-ip By default ip will come)
5.
Name: DB-SG
Description: DB sg
VPC: custom
Inbound rules-1 : Type Mysql/Aurora and port range is 3306 and Source Type Custom and Source  PrivateInstance-SG

10.DATABASE:

_

Note: Amazon RDS is more chargeable be care full and delete as soon as possible the project has completed.
_

(I)SUBNET GROUP:
Search for amazon rds and select RDS and go to subnet groups and create db subnet group
Name: threetier-sb-subnetgroup
VPC: Custom
Availability zones: US-EAST-1A & US-EAST-1B
Subnets : Private -DB-Subnet-AZ1(10.0.4.0/24) & Private -DB-Subnet-AZ2(10.0.5.0/24)

Then finally we can create.

(II)DATABASE:
Select database and Click on create database
• Name:_________
• Select standard Creation method.
• Engine option is: Aurora(MySQL)
• Template: Dev/test
• Master Password: _________ and reenter password.
• Cluster storage configuration : aurora standard
• Instance configuration: DB instance class: Default
• Availability & Durability : default
• Connectivity: Computer resource: don’t connect ec2 instance and IPV4
• VPC: Default
• DB subnet Group: threetier-sb-subnetgroup (we created earlier)
• Public Access: NO
• VPC SG: DB-Sg
• Database Authentication: Password Authentication: Default
• Monitoring: Turn on performance Insight untick
• Finally Click on create database.

11.APP TIER INSTANCE DEPLOYMENT:

Create an ec2 instances with
name: mywebserver1
Instance Type: t2.micro
Key Pair: default ()
VPC: custom
Subnet: private-subnet-az1
SG: privateinstance SG
Public_ip:Disable(default)
IAM role : whatever we created
Proceed without private key and public IP. then create finally.

login the ec2 instance connect through Connect using SSM session.

sudo -su ec2-user
ping 8.8.8.8

sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
sudo yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
sudo yum install mysql
mysql -h database-1-instance-1.cpgu4lddlcef.us-east-1.rds.amazonaws.com -u admin -p

(mysql -h CHANGE-TO-YOUR-RDS-ENDPOINT -u CHANGE-TO-USER-NAME -p)
--now we are inside the database we can create databses by using the following commands

create DATABASE webappdb;
show databases;
use webappdb;

CREATE TABLE IF NOT EXISTS transactions(id INT NOT NULL
AUTO_INCREMENT, amount DECIMAL(10,2), description
VARCHAR(100), PRIMARY KEY(id));

show tables;
INSERT INTO transactions (amount,description) VALUES ('400','groceries');

(DELETE FROM customers WHERE customer_id = 123;)
SELECT * FROM transactions;

12.S3Bucket:
We have already created s3 bucket so in that we have to upload apptier folder before that we have to configure our DBconfig.js file then upload.

module.exports = Object.freeze({
    DB_HOST : 'database-1-instance-1.cpgu4lddlcef.us-east-1.rds.amazonaws.com',
    DB_USER : 'admin',
    DB_PWD : 'venkatesh',
    DB_DATABASE : 'webappdb'
});
Enter fullscreen mode Exit fullscreen mode

In mysql inside we have to exit then we should run some commands like

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16
npm install -g pm2

cd ~/
aws s3 cp s3://BUCKET_NAME/app-tier/ app-tier –recursive
cd ~/app-tier
npm install
pm2 start index.js
pm2 list
pm2 logs
pm2 startup
[PM2] To setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v16.0.0/bin /home/ec2-user/.nvm/versions/node/v16.0.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user —hp /home/ec2-user

pm2 save
curl http://localhost:4000/health
"This is the health check"
curl http://localhost:4000/transaction
{"result":[{"id":1,"amount":400,"description":"groceries"},{"id":2,"amount":100,"description":"class"},{"id":3,"amount":200,"description":"other groceries"},{"id":4,"amount":10,"description":"brownies"}]}

13.AMI:
Create AMI out of this ec2 instance.
AMI name : AppTierImage and all settings are default only and create IMAGE.

14.Target group:
Create target group with
Name: AppTierTargetGroup
VPC: custom
HTTP is 4000 port and and Healthy threshold 2 and rest all as usual then create a Target Group.

15.Load Balancer:
Create Load Balancer with configurations
Name : App-Tier-internal-lb

then ofter this we have to choose INTERNAL Type
VPC: custom
Subnet : private-subnet-az1 and private-subnet-az2
SG : INTERNAL_LB_SG and HTTP port and 80 and choose ApppTierTargetGroup and create finally .

16.Launch template:
Cretae Launch Template with
Name : AppTier-LaunchTemplate
AMI : MYAMI is APPTierImage
Instance type : t2.micro
KeyPair: default that is don’t include in launch template
Subnet: default don’t select any subnet
SG : Private instance-SG
IAM ROLE: ____Then create.

17.AUTO SCALING GROUPS:

Create ASG with the
Name : AppTierASG
LaunchTemplate : AppTier-launchTemplate
VPC: custom
AZ : private-subnet-AZ1 and private-subnet-AZ2
LoadBalancer : Choose existing Load Balancer
Target Group : AppTierTargetGroup
Desired &Minimum & Maximum instances: enter 2 in all …..and then create finally.

18.NGINX FILE:

Open nginx file what ever the data we downloaded from github in that data nginx file is there inside that file in one of the line [replace-with-internal-lb-dns] will be there.
we have to replace that with our load balancer DNS name. then save.

Upload nginx file and webtier directory in same s3 bucket.

Image description

19.Instance Creation:

Create a ec2 instance with
Name : Demowebserver
OS: Amazon Linux image
Instance Type: t2.micro
Keypair : proceed with out Key
VPC: custom
Subnet : public-web-subnet-az1
Public ip : enable
SG: webtier-sg
IAM role :____is what ever we created
and then create instance Finally.

login this demowebserver and use the following commands

Sudo -su ec2-user
Ping 8.8.8.8
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16
cd ~/
aws s3 cp s3://BUCKET_NAME/web-tier/ web-tier --recursive
cd ~/web-tier
npm install
npm run build
sudo amazon-linux-extras install nginx1 -y
cd /etc/nginx
ls

sudo yum install nginx -y

cd /etc/nginx
ls

sudo aws s3 cp s3://project1demo/nginx.conf . for coping only nginx file from s3 bucket.

sudo service nginx restart
sudo vi nginx.conf for removing square brackets where ever we updated we forgot while editing in the local.

Image description

Image description

sudo service nginx restart this command should work properly it shouldn’t throw any error.
sudo service nginx status
chmod -R 755 /home/ec2-user
sudo chkconfig nginx on

20.CRETE IMAGE AMI:

Create an IMAGE on top of DemoWebServer Name is WebServerImage

21.CRETE Target Group:

Create target Group
Name : WebServer-TG
VPC: custom_vpc
HealthCheckPath :/health and then create TG

22.CRETE Load Balancer:

Create Load Balancer select AutoScaling Grouping
Name : WebTier-external-lb
and then select SCHEME is internet type.
VPC: custom VPC
AZ1:Public-web-subnet-AZ1
AZ2: Public-web-subnet-AZ2
SG: InternetFacing-lb-Sg
Listener & Routing: HTTP and port is 80 and WebServer_TG then click on create ALB

23.CREATE Launch Template:

Create Launch Template
Name: WebServerImage
Instance Type: t2.micro
KeyPair: Default only
VPC & Subnet: Default only
SG: Web-tier-SG
IAM: select ROLE what ever we created
Then click on create Launch Template.

24.CREATE Auto Scaling Group:

Search for Auto Scaling Group and click on create Auto Scaling Group
Name: WebServerASG
LaunchTemplate: WebServer Launch Template
VPC: Custom
AZ: Public-Web-Subnet-AZ1 & Public-Web-Subnet-AZ2
LB: select existing LB then select WEBSERVER-TG
Desired & Minimum & Max select all are 2 instances
Click on create ASG

25.FINAL OUTPUT

• Click on Load Balancer and choose Web-Tier-External-lb DNS Name that is web-tier-external-lb-3549283109.us-east-1.elb.amazonaws.com.

Image description

• Copy the DNS name and paste in the google.

Image description

• Click on 3 Horizontal Lines we will get like below

Image description

• Here under Amount we can enter amount and under DESC we can enter description and click on ADD the data will add.

26.DELETING PROGRESS:

• Auto Scaling Groups
• Application Load Balancer
• Target Groups
• Launch Template
• AMI
• Ec2
• Amazon Aurora DB & subnet Group
• NAT
• Elastic IP
• Route Table
• Internet Gateway
• VPC
• Subnets
• Snapshots

. . . . . . . . . . . . .