Implementing a 3 tier AWS Architecture
Tags: aws, devops, cloud
Published: 2023-04-08 17:43:25

In today's digital age, it's becoming increasingly important to design and implement a reliable, scalable, and secure infrastructure for your web application. A 3-tier architecture is a common approach that divides the application into three logical layers: presentation, application, and database. In this article, we'll walk through the steps to design and implement a 3-tier AWS architecture.

# Step 1: Define the Architecture

Before you start building, it's essential to define the architecture.In the past Week, using Draw.io I designed an architecture. 
![Design Architecture ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uoawyr8kun70l2k76qbz.png)
 
It Starts by identifying the resources required for each tier, such as compute, storage, and networking resources. Next, decide on the best services to use to implement each tier. AWS provides a vast array of services that you can use to implement your architecture. 

The presentation tier is responsible for handling the user interface and user interaction. The application tier handles business logic and processing, while the database tier handles data storage and retrieval. For the presentation tier, consider using I used an EC2 to host the web server. For the application tier, you I also used Amazon EC2. For the database tier, consider using Amazon RDS multi AZ.

# Step 2: Implementing the Architecture

## Set up the VPC
Set up the networking infrastructure for your architecture. You'll need to create a Virtual Private Cloud (VPC) to isolate your application from the internet and other resources in your AWS account. Also created subnets within the VPC to segregate each tier of the architecture and also using security groups and network access control lists (ACLs) to control access to each tier.

* CLI command to create a VPC named cloudforce,  with 10.1.0.0/22 CIDR block 

```
aws ec2 create-vpc \
    --cidr-block 10.1.0.0/22 \
    --tag-specifications ResourceType=vpc,Tags='[{Key=Name,Value="Cloudforce"}]'

```
* Create Public and Private subnets in the VPC

* Public Subnet for the web server in AZ us-east-1a, CIDR Block 10.1.0.0/26

```
aws ec2 create-subnet \
    --vpc-id vpc-04e49362baefd6887 \
    --cidr-block 10.1.0.0/26 \
    --availability-zone us-east-1a \
    --tag-specifications ResourceType=subnet,Tags='[{Key=Name,Value="PublicWebServerVPC2"}]'

```
* Private Subnet for the App server in AZ us-east-1b, CIDR Block 10.1.2.64/26

```
aws ec2 create-subnet \
    --vpc-id vpc-04e49362baefd6887 \
    --cidr-block 10.1.2.64/26 \
    --availability-zone us-east-1b \
    --tag-specifications ResourceType=subnet,Tags='[{Key=Name,Value="PrivateAppServer2"}]'

```

Repeat the Process to create 1 more Private and public subnet, ensure your have the appropriate CIDR block for each with no conflicts and the availability Zone

![Created Subnets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6xtegnipslwic8bu9mt.png)

### You would also need Route tables and Internet Gateway

CLI Code to Create an Internet Gateway

```
aws ec2 create-internet-gateway \
    --tag-specifications ResourceType=internet-gateway,Tags='[{Key=Name,Value="cloudforce-igw"}]'

```

CLI Code to attach the IGW

```
aws ec2 attach-internet-gateway --vpc-id "vpc-04e49362baefd6887" --internet-gateway-id "igw-0252d45a6bd3b915f" --region us-east-1

```
* And create appropriate route tables for both the private and public subnets

## Set up the Presentation Tier

The presentation tier is the first tier of your application that users will interact with. The app was using a web server in deployed in an EC2 Instance

CLI Code for the web Server 
Things to Specify
* Image ID
* Instance Type
* Key pair name
* Subnet ID
* Security Groups
* User data which would install and run an apache web server

```
aws ec2 run-instances \
--image-id ami-06e46074ae430fba6 \
--count 1 \
--instance-type t2.micro \
--key-name cloudforce \
--subnet-id subnet-0e902686c4d2890d7 \
--security-group-ids sg-0bbc0431234c68fcd \
--tag-specifications ResourceType=instance,Tags='[{Key=Name,Value="cloudforce-webserver2"}]' \
--user-data https://clouduserdata254.s3.amazonaws.com/user-data.txt

```

User data

```
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd.service
systemctl enable httpd.service
echo "<html><body><h1>Welcome to my web server</h1></body></html>" > /var/www/html/index.html

```

## Set up Deploy the Application Tier

The application tier is responsible for handling business logic and processing. The App used as its EC2 is a flexible compute service that lets you launch and manage virtual servers in the cloud.

Deploy an EC2 App Server, 

```
aws ec2 run-instances \
--image-id ami-06e46074ae430fba6 \
--count 1 \
--instance-type t2.micro \
--key-name cloudforce \
--subnet-id subnet-04fcbc024f42beb38 \
--security-group-ids sg-0bbc0431234c68fcd \
--tag-specifications ResourceType=instance,Tags='[{Key=Name,Value="Cloudforce-Appserver2"}]' 

```

![Deployed EC2 Instane ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6sswcg4rcrlfp3kmilc.png)

## Deploy the Database Tier

The database tier is responsible for storing and retrieving data. You can deploy your database tier using Amazon RDS, Amazon DynamoDB, or Amazon Aurora. RDS is used for this as its a managed relational database service that can run popular database engines such as MySQL, PostgreSQL, and Oracle. 

I used the console to deploy the RDS Mysql database using the following steps:
1. Create an Amazon RDS Instance: The first step is to create an Amazon RDS instance. In the AWS RDS service. Click on "Create database" 
2. Choose the Database Engine: MySQl

![Create RDS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urwdn9pc3pd1f3wh55px.png)

3. Specify Database Details
In this step, you need to specify the database details such as the database name, username, and password. You also need to choose the database instance class and storage type. Make sure to choose a Multi-AZ deployment option under the "Deployment Type" section. This will ensure that your database is replicated to a standby instance in a different AZ for high availability.

![RDS Configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfgps6p7512z7c8la37t.png)

![RDS Configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlew90icayc22bpzoslj.png)


4. Configure Advanced Settings
Under the "Advanced settings" section, you can configure additional settings such as the VPC (Virtual Private Cloud) and security group for your database instance. You can also enable automatic backups and set the retention period for backups. It's recommended to enable backups and choose a retention period that suits your application's requirements.

![RDS Configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abbtzreyawvxaqhpp8l2.png)

![RDS Configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb5qbbm9990x6uzfliud.png)

5. Launch the Database Instance
After configuring all the settings, click on "Create database" to launch the RDS instance. It may take a few minutes to create the instance.


A 3-tier architecture can provide a scalable, reliable, and secure infrastructure for your web application. With AWS, you have access to a vast array of services that can help you implement each tier of your architecture. By following the steps outlined in this article, you can design and implement a robust 3-tier AWS architecture for your application.