How to deploy Three Tier Architecture in AWS Using Terraform
Terraform is generally used with deploying the Three-Tier Architecture in AWS as a potent way of designing an application scalable, resilient, and secure, this architecture decomposes the overall application structure into three layers: the Presentation Tier (frontend), the Logic Tier (backend), and the Data Tier (database). The other categories of IaC tools include the configuration, setup, and operations of Infrastructure that is deployed into AWS automatically without any consistency and efficiency lapse by use of Terraform.
Primary Terminologies
- Three-Tier Architecture: A software architecture design pattern that divides an application into three logical and physical computing tiers: Presentation, Logic, and Data.
- Presentation Tier: The front end—a web or mobile frontend.
- Logic Tier: The server-side tier in which the business logic is executed.
- Data Tier: A place where data resides, which can be a database or storage system.
- AWS (Amazon Web Services): A broad set of computing, storage, and miscellaneous offerings related to services.
- Terraform: An open-source Infrastructure as Code tool that you use to define and manage the infrastructure in a configuration language through a declarative process.
- VPC (Virtual Private Cloud): A virtual network dedicated to your AWS account, wherein you can launch AWS resources.
- Subnets: These are the subdivisions within a VPC that divide the network into smaller networks, usually public and private subnets.
- Security Groups: Virtual firewalls in AWS used to manage inbound and outbound traffic access to AWS resources.
- RDS: Relational Database Service within AWS that provides a managed service for databases.
- Elastic Load Balancer (ELB): A managed service that automatically distributes incoming application traffic across a set of targets.
Step-by-Step Process to Deploy Three-Tier Architecture Using Terraform
Step 1: Install Terraform
Install terraform on local machine by using following commands
- Install yum-config-manager to manage your repositories.
sudo yum install -y yum-utils
- Use yum-config-manager to add the official HashiCorp Linux repository.
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
- Install Terraform from the new repository.
sudo yum -y install terraform

Step 2: Define the VPC and Networking
- Create a
main.tf
file to define your VPC, Subnets, and Security groups.
VPC
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "<your-desired-name>" {
cidr_block = var.vpc_cidr
instance_tenancy = "default"
tags = {
Name = "your-desired-name"
}
}

Subnets
#public-subnet1 creation
resource "aws_subnet" "public-subnet1" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet1_cidr
map_public_ip_on_launch = "false"
availability_zone = "us-east-1a"
tags = {
Name = "public-subnet1"
}
}
#public-subnet2 creation
resource "aws_subnet" "public-subnet2" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet2_cidr
map_public_ip_on_launch = "false"
availability_zone = "us-east-1b"
tags = {
Name = "public-subnet2"
}
}
#private-subnet1 creation
resource "aws_subnet" "private-subnet1" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet3_cidr
availability_zone = "us-east-1b"
tags = {
Name = "private-subnet1"
}
}
#private-subnet2 creation
resource "aws_subnet" "private-subnet2" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet4_cidr
availability_zone = "us-east-1c"
tags = {
Name = "private-subnet2"
}
}
#private-subnet3 creation
resource "aws_subnet" "private-subnet3" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet5_cidr
availability_zone = "us-east-1b"
tags = {
Name = "private-subnet3"
}
}
#private-subnet4 creation
resource "aws_subnet" "private-subnet4" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet6_cidr
availability_zone = "us-east-1c"
tags = {
Name = "private-subnet4"
}
}

Security Groups.
#security group for front end tier
resource "aws_security_group" "web-sg" {
vpc_id = aws_vpc.siva.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-sg"
}
}

Step 3: Creation of Internet Gateway
#IGW creation
resource "aws_internet_gateway" "siva-gateway" {
vpc_id = aws_vpc.siva.id
}

Step 4: Route table creation
#route table
resource "aws_route_table" "route" {
vpc_id = aws_vpc.siva.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.siva-gateway.id
}
tags = {
Name = "route to internet"
}
}
#route 1
resource "aws_route_table_association" "route1" {
subnet_id = aws_subnet.public-subnet1.id
route_table_id = aws_route_table.route.id
}
#route 2
resource "aws_route_table_association" "route2" {
subnet_id = aws_subnet.public-subnet2.id
route_table_id = aws_route_table.route.id
}

Step 4: Deploy EC2 Instances
- Create EC2 instances for the Web and App layers using Terraform.
resource "aws_instance" "ecomm" {
ami = "ami-07761f3ae34c478d"
instance_type = "t2.micro"
key_name = "yourkeyname"
vpc_security_group_ids = [aws_security_group.web-sg.id]
subnet_id = aws_subnet.public-subnet1.id
associate_public_ip_address = true
user_data = file("data1.sh")
tags = {
name = "EC2-1"
}
}
resource "aws_instance" "food" {
ami = "ami-07761f3ae34c478d"
instance_type = "t2.micro"
key_name = "yourkeyname"
vpc_security_group_ids = [aws_security_group.web-sg.id]
subnet_id = aws_subnet.public-subnet2.id
associate_public_ip_address = true
user_data = file("data2.sh")
tags = {
name = "EC2-2"
}
}

Step 5: Deploy the RDS Database
Set up an RDS instance for the Data Tier.
#rds file
resource "aws_db_subnet_group" "rds-subnet" {
name = "rds-subnet"
subnet_ids = [aws_subnet.private-subnet1.id, aws_subnet.private-subnet2.id]
tags = {
Name = "Db subnet group"
}
}
resource "aws_db_instance" "rds-subnet" {
allocated_storage = 10
db_subnet_group_name = aws_db_subnet_group.rds-subnet.id
engine = "mysql"
engine_version = "8.0.35"
instance_class = "db.t2.micro"
multi_az = true
username = "admin"
password = "password"
skip_final_snapshot = true
vpc_security_group_ids = [aws_security_group.db-sg.id]
}

Step 6: Configure Load Balancer
Deploy an Elastic Load Balancer (ELB) to distribute traffic to your web servers.
#load balancer
resource "aws_lb" "external-alb" {
name = "External-LB"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.web-sg.id]
subnets = [aws_subnet.public-subnet1.id, aws_subnet.public-subnet2.id]
}
resource "aws_lb_target_group" "target_elb" {
name = "ALB-TG"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.siva.id
health_check {
path = "/health"
port = 80
protocol = "HTTP"
}
}
resource "aws_lb_target_group_attachment" "ecomm" {
target_group_arn = aws_lb_target_group.target_elb.arn
target_id = aws_instance.ecomm.id
port = 80
depends_on = [
aws_lb_target_group.target_elb,
aws_instance.ecomm,
]
}
resource "aws_lb_target_group_attachment" "food" {
target_group_arn = aws_lb_target_group.target_elb.arn
target_id = aws_instance.food.id
port = 80
depends_on = [
aws_lb_target_group.target_elb,
aws_instance.food,
]
}
resource "aws_lb_listener" "listener_elb" {
load_balancer_arn = aws_lb.external-alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_elb.arn
}
}

Step 7: DNS Link for Loadbalancer
By using this block we can get DNS link
#DNS of LoadBalancer
output "lb_dns_name" {
description = "DNS of Loadbalancer"
value = aws_lb.external-alb.dns_name
}

Step 8: Apply the Configuration
Run the following Terraform commands to deploy your infrastructure:
terraform init # Initialize the Terraform environment
terraform plan # Review the resources that will be created
terraform apply # Deploy the infrastructure
terraform init

terraform plan

terraform apply

Step 9: Verify the Deployment
- After the deployment, check the AWS Management Console to verify that your instances, load balancer, and RDS database are up and running.
- Here is the resources created by terraform
VPCs

Route Tables

Subnets

Instances

Internet Gateways

RDS Database

Load Balancers

Application

Conclusion
Terraform allows you to achieve a very good and modern solution in building secure cloud applications that are well balanced and scalable. The three-tier architecture, essentially in AWS, powered by Terraform, leverages its capability to implement Infrastructure as Code for the setup automation of distinct layers—Presentation, Logic, and Data—within an AWS environment. This means that you get consistent deployments, easy scalability, and enhanced security through isolation for each defined tier. The automation in Terraform minimizes human error, keeps management simple, and allows version control of your infrastructure. At the base of this modern application architecture, there lies a strong structure supporting high availability and fault tolerance. Mastery of Terraform by a cloud engineer or developer in deploying such complex architectures is the key to optimizing operations on the cloud for applications that work seamlessly.