3 Tier Web Application in AWS
Published: 2024-06-19 11:01:05


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2ty29n7hou2uld2yxxh.png)


**What is 3 tier Architecture?**
Three tier architecture is a client server architecture pattern, that separates Application into 3 interconnected layers or tiers.

**Presentation or Web Tier:**
This is the topmost level of the application responsible for displaying the user interface and handling user interactions. It provides a graphical or web-based interface through which users can interact with the application.

**Application Tier:**
This middle layer contains the core functionality of the application, processing the business logic, making logical decisions, and performing computations. It acts as a bridge between the presentation tier and the data tier.

**Data Tier:**
The bottommost layer is responsible for data storage and management. It handles database operations such as querying, updating, and storing data.

Lets start implementing:
Task 1. Create VPC

Task 2. Create Subnets

Task 3. Setting up Internet GW: Create an Internet Gateway and attach it to your VPC.

Task 4. Create NAT GW:
1. Create a NAT Gateway in the public subnet with an Elastic IP.
2. Create a route in the private subnet route table to direct internet traffic to the NAT Gateway.

Task 5.Create Route Tables :

#### Public Route Table
1. Create a route table for the public subnet.
2. Add a route for `0.0.0.0/0` pointing to the Internet Gateway.

#### Private Route Table
1. Create a route table for the private subnet.
2. Add a route for `0.0.0.0/0` pointing to the NAT Gateway.

Task 6. Configure Launch Templates: 6.1. Create a Launch Template for the web tier EC2 instances, with User Data script.

6.2. Create a Launch Template for the application tier EC2 instances.

Task 7. Auto scaling groups:

1. Create an Auto Scaling Group for the web tier using the web tier Launch Template and specify the public subnet.
2. Create an Auto Scaling Group for the application tier using the application tier Launch Template and specify the private subnet.

Task 8. Database Tier:
1. Create an RDS instance in the database subnet.
2. Configure the RDS instance to use the Database Security Group.

Task 9. Testing the setup

Architecture Diagram of above implementation:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgfpdh8effwfad77arg6.png)



## Task 1. Creating VPC:
Login to AWS Management Console, in the search bar type VPC, once the page is open click on Create VPC
Select VPC only
Give a name of your choice
IPv4 CIDR as 10.0.0.0/16

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8p4de3w2zvkcol44g55r.png)


. Click on Create VPC

## Task 2. Create a Subnet
Click on Create Subnet
Select VPC Id as 3TierApp
Note : Here we will create 6 Subnets 2 Subnets for Web Tier, 2 Private Subnets for Application Tier and 2 Private subnets for Database Tier


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qidlutkc74n7bkcuaky.png)


Give the name of Subnet of your choice “webtier1-public”
IPv4 Subnet CIDR Block “10.0.32.0/24”
Enable Auto-assign IP settings
Click on Create Subnet

Note : Similarly create 5 new subnets
2nd Subnet with name “webtier2-public”, IPv4 subnet CIDR Block “10.0.0.0/24”, Enable Auto-assign IP settings

3rd Subnet with name “applicationtier1-private”, IPv4 subnet CIDR Block “10.0.160.0/24”

4th Subnet with name “applicationtier2-private” and IPv4 subnet CIDR Block “10.0.128.0/24”

5th Subnet with name “databasetier1-private” IPv4 subnet CIDR Block “10.0.96.0/24”

6th Subnet with name “databasetier2-private” IPv4 subnet CIDR Block “10.0.64.0/24” , select different AZ as us-east-1a

All the subnets are successfully created


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pg3usl79zf8z4xic37bb.png)


## Task 3. Setting up Internet GW
Go to Internet Gateway from left Navigation Panel and click on click Internet GW
Give the name of your choice “igw-3tierapplication”
Click on Create Internet GW

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xp99tvzjgyf9pepy45sy.png)


Attach the newly created IGW to VPC

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yflatez9qq7wjdzz9zhf.png)


Click on Attach internet gateway

## Task 4. Create NAT GW
From the left nagivation panel click on NAT gateways
Give the name for Nat Gateway “nat-3tierapp”
Select Subnet as which is connected to Internet GW, in our case we have associated webtier1-public to Internet GW so we will select “webtier1-public”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqee1yf702vb654va6jt.png)


Note : In order to create NAT GW you need to Allocate Elastic IP address and use the Elastic IP address while creating NAT GW


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f19qgv7ps8aldglposfr.png)


## Task 5.Create Route Tables
Lets create 2 route tables one for public and another one for private

Go to Route Tables and click on Create route table
Give a name as “route-private”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwztyvwexj1rvypkhmn7.png)


Create another route table and give name as “route-public” and select 3tierApp VPC
Go to “route-public” and click on subnet association
Click on Edit subnet association
Select “webtier1-public”and “webtier2-public” and click on Save association.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c10l3mzfs2i0h8okk9ix.png)


Go to Routes
Click on Edit Routes
Add new route, Destination as 0.0.0.0/0 and Target Select Igw, click on Save changes

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzyhaivvn7a05hbdd5jm.png)


Select other route table which is “route-private”
Click on Subnet associations
Select the two application subnets which we have created and click on Save associations

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okrc0j7pxvvfb5ps1473.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kng8d326qvkyp9pm2jsi.png)




* Click on edit routes and add NAT GW as Target and select 0.0.0.0/0 as destination

## Task 6. Configure Launch Templates
Go to EC2 and in the left navigation panel click on Launch Templates
Click on Create Launch Template, give any name of your choice
Check the box for Auto Scaling Guidance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmywqbfbrb35silxkueu.png)


Select the Amazon Linux 2023 AMI

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nwe424057ljmonws7rh.png)


Select instance type as “t2.micro”
Create new key pair
In Network Settings, Create new SG and give name as “webtierSG”
Give some description
And select the 3tier VPC
Inbound rules ssh and http from anywhere

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmfin60b72eb5bzuyamd.png)


Scroll to the bottom and click on Advanced Settings
Go to user data and paste the below script
`#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<html><body><h1>Tech Universe with Asma, Demo for 3Tier Application</h1></body></html>" > /var/www/html/index.html`

Click on Create Launch Template
**6.2 Create another Launch Template for Application**
Give name of your choice

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f3qm2ae5yl7yjxmoy9w.png)


Select AMI as Amazon Linux 2023
Instance type as t2.micro
Select the keypair which is created earlier
Create new security group
Create inbound rules for ssh, http and mysql connectivity
For SSH we need to give the source as “webtierSG”, it means that webtier machine is only allowed to access the application tier


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcf2mbvny83m78prn4tg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0cl2yx2794750obkuzz.png)



Click on Launch template

## Task 7. Auto scaling groups
Click on Auto scaling groups and click on create auto scaling group

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eracs76pj6vcf90xqklc.png)

Give a name of your choice
Select the Launch template which is created in previous step


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yw33gyns13ccm3v7qm60.png)



Click on Next and in Network, Select 3tierVPC
Select both the webtier-public subnets, click on Next


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k6unvvywk0sa93uwksj.png)


Select desired capacity type as 1,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkeuqbbfwrymortf5pqf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trla9uw6gytuhx507ai3.png)



Give tag as Name and WebTierMachine

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mw4d4i0zg0uz94eicdh.png)


Review everything and click on Create AutoScaling Group


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsjqvbj8vwnvsdbkbm8w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf0hppg6asv3iefq9vzg.png)



One of the instance gets created go and check the details
Go to tags and check that instance is created because of Auto Scaling group

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4dtkxgj8vchviabpuub.png)


Copy the public IP of WebTier and paste in browser , if you are able to see the below page, Web Tier is correctly setup.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z7nskmccxj6a0jza4kg.png)


## 7.2 Create Auto Scaling group for Application Tier

Create Auto Scaling group
Give the name of your choice
Select Launch template as “application-3tierapp”
Select VPC as 3tierVPC
Availability Zones select “applicationtier1-private and application2-private”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sml10zwsntkdv8lonjd2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb724wtw2ble5vjhnjbu.png)



Keep everything as it is.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgwhq3181nxxz8wv414d.png)


Click on Create AutoScaling group


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grl9o9464suehv1b5xww.png)



Go to EC2 Instance and Click on Instances and see if the new instance is launched because of newly created application tier autoscaling group

## Task 8. Database Tier
In the search bar search “RDS”
Click on “Create Database”
Select Standard Create and select “MySql”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn3auf5mdsm0hp52jjz2.png)


Select Free tier

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xt6tcq7541hbf3iozwe.png)


Scroll down and give the password

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m88ant56vibw8zj13ygm.png)


Instance configuration “db.t3.micro”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkmoft3o7lssbcslpblp.png)


Uncheck Storage autoscaling

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxxbzlqd42n7vnot80eq.png)


Connectivity choose 3TierApp VPC

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kn3gypndylv2vsdfl6b.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/478rk8xnkhbtosizw82g.png)


Create new DB Subnet Group and new security group

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5id43c2rhdv8rcoftaw2.png)

Click on Create Database
Database is created successfully

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tlqxki1ftngijggsvdj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdsrvh9tm10nrmjeb0bn.png)


Go to EC2 Instance and Click on Application Machine and click on Security Group, Open Security Group and click on Edit Inbound Rules

Click on Inbound rules, edit Mysql inbound rule and in source type database security group

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y46ja3o9c9nlhye7t0wf.png)


Now SSH into the Web Tier Machine from my laptop

once connected, type the below command and it confirms that web tier is running correctly.

`curl http://localhost`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sq2d372rec86175ve8a.png)



Now try to connect the application tier from the Web tier.
Select the application machine and you will see there is no public ip, copy the private IP and ping the IP from Webtier machine


There would be no response from Application Machine, there has to be icmp enabled for ping command to work correctly.
Now you have to add the private key to EC2 instance, for this you need to copy the content of private key from your personal laptop to the webtier machine.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebquj8xqbd1es2s0t47p.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp2qyyew0pgq2j5vhikv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozzw6rw5f5e41t8111hn.png)


Now we are able to connect Webtier Machine to Application Tier.

Lets connect Database from Application Machine.

First we need to install mysql on the Application machine, type the below command

sudo yum install mysql -y
mysql will get installed on application machine, but it does not have internet connectivity but NAT GW is attached to the instance.

Copy the endpoint of Database
database-1.cyrewq2xhdxm.us-east-1.rds.amazonaws.com

Initiate your DB connection with database endpoint. In the following command, replace the RDS endpoint and the username, and then execute it in the browser terminal:
mysql -h database-1.cyrewq2xhdxm.us-east-1.rds.amazonaws.com -P 3306 -u admin -p password 
You will then be prompted to type in your password. Once you input the password and hit enter, you should now be connected to your database.

NOTE: If you cannot reach your database, check your credentials and security groups.

Create a database , insert data with the help of SQL commands using the MySQL CLI.
Thats awesome , We successfully connected to our database from our application server.

We’ve successfully created a highly available, 3-tier application architecture.

Benefits of using 3 Tier Architeture on AWS:
Using a three-tier architecture on AWS provides several benefits, particularly when aligned with the AWS Well-Architected Framework. Here are some key advantages:

Scalability:
Auto Scaling: Each tier (web, application, and database) can be scaled independently based on demand, allowing efficient resource utilization and cost management.
2. High Availability and Fault Tolerance:

Multi-AZ Deployments: Services like RDS can be configured for Multi-AZ deployments, providing automatic failover to a standby instance in case of an outage.
3. Security:

Network Segmentation: Using VPCs and subnets to isolate different tiers enhances security by limiting access to sensitive components like databases.
Security Groups and NACLs: Fine-grained control over inbound and outbound traffic at the instance and subnet level.
4. Manageability:

Separation of Concerns: Dividing the architecture into three tiers (web, application, and data) simplifies management, updates, and troubleshooting.
Infrastructure as Code (IaC): Services like AWS CloudFormation and AWS CDK allow you to define and deploy infrastructure consistently and repeatably.
5. Cost Optimization:

Right-Sizing: Each tier can be sized independently based on its specific resource requirements, preventing over-provisioning.
Reserved Instances and Savings Plans: Cost savings on long-term commitments for predictable workloads.
6. Performance Efficiency:

Auto Scaling: Automatically adjusts capacity to maintain steady and predictable performance.
7. Service Integration: Easily integrates with other AWS services (e.g., S3 for storage, SNS for messaging) to extend functionality and support different use cases.

Environment Isolation: Development, staging, and production environments can be easily separated within the same architecture.
By leveraging these benefits, a three-tier architecture on AWS, following the Well-Architected Framework principles, ensures robust, efficient, and scalable application deployments.
Remember to delete your resources (ASG, VPC, Subnets, DB, NAT Gateway) and release all elastic IPs, so you don’t continue to get charged!
Thank you!
Appreciate your time reading! If you found value in this article, a round of applause (👏) would be fantastic to help others discover it. Feel free to share your insights and comments below, I’m eager to hear your thoughts!