❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Deploying a Two-Tier Web Application on AWS with MySQL and Apache

By: Ragul.M
12 March 2025 at 12:46

In this blog, I will guide you through step-by-step instructions to set up a two-tier architecture on AWS using VPC, Subnets, Internet Gateway, Route Tables, RDS, EC2, Apache, MySQL, PHP, and HTML. This project will allow you to host a registration web application where users can submit their details, which will be stored in an RDS MySQL database.

Step 1: Create a VPC
1.1 Login to AWS Management Console

  • Navigate to the VPC service
  • Click Create VPC
  • Enter the following details:
  • VPC Name: my-vpc
  • IPv4 CIDR Block: 10.0.0.0/16
  • Tenancy: Default
  • Click Create VPC

Image description

Step 2: Create Subnets
2.1 Create a Public Subnet

  • Go to VPC > Subnets
  • Click Create Subnet
  • Choose my-vpc
  • Set Subnet Name: public-subnet
  • IPv4 CIDR Block: 10.0.1.0/24
  • Click Create

2.2 Create a Private Subnet
Repeat the steps above but set:

  • Subnet Name: private-subnet
  • IPv4 CIDR Block: 10.0.2.0/24

Image description

Step 3: Create an Internet Gateway (IGW) and Attach to VPC
3.1 Create IGW

  • Go to VPC > Internet Gateways
  • Click Create Internet Gateway
  • Set Name: your-igw
  • Click Create IGW 3.2 Attach IGW to VPC
  • Select your-igw
  • Click Actions > Attach to VPC
  • Choose my-vpc and click Attach

Image description

Step 4: Configure Route Tables
4.1 Create a Public Route Table

  • Go to VPC > Route Tables
  • Click Create Route Table
  • Set Name: public-route-table
  • Choose my-vpc and click Create
  • Edit Routes β†’ Add a new route:
  • Destination: 0.0.0.0/0
  • Target: my-igw
  • Edit Subnet Associations β†’ Attach public-subnet

Image description

Step 5: Create an RDS Database (MySQL)

  • Go to RDS > Create Database
  • Choose Standard Create
  • Select MySQL
  • Set DB instance identifier: my-rds
  • Master Username: admin
  • Master Password: yourpassword
  • Subnet Group: Select private-subnet
  • VPC Security Group: Allow 3306 (MySQL) from my-vpc
  • Click Create Database

Image description

Step 6: Launch an EC2 Instance

  • Go to EC2 > Launch Instance
  • Choose Ubuntu 22.04
  • Set Instance Name: my-ec2
  • Select my-vpc and attach public-subnet
  • Security Group: Allow
  • SSH (22) from your IP
  • HTTP (80) from anywhere
  • MySQL (3306) from my-vpc
  • Click Launch Instance

Image description

Step 7: Install Apache, PHP, and MySQL Client
7.1 Connect to EC2

ssh -i your-key.pem ubuntu@your-ec2-public-ip

7.2 Install LAMP Stack

sudo apt update && sudo apt install -y apache2 php libapache2-mod-php php-mysql mysql-client

7.3 Start Apache

sudo systemctl start apache2
sudo systemctl enable apache2

Step 8: Configure Web Application
8.1 Create the Registration Form

cd /var/www/html
sudo nano index.html
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>
    <h2>User Registration</h2>
    <form action="submit.php" method="POST">
        Name: <input type="text" name="name" required><br>
        DOB: <input type="date" name="dob" required><br>
        Email: <input type="email" name="email" required><br>
        <input type="submit" value="Register">
    </form>
</body>
</html>

Image description

8.2 Create PHP Script (submit.php)

sudo nano /var/www/html/submit.php
<?php
$servername = "your-rds-endpoint";
$username = "admin";
$password = "yourpassword";
$dbname = "registration";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$dob = $_POST['dob'];
$email = $_POST['email'];
$stmt = $conn->prepare("INSERT INTO users (name, dob, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $dob, $email);
if ($stmt->execute()) {
    echo "Registration successful";
} else {
    echo "Error: " . $stmt->error;
}
$stmt->close();
$conn->close();
?>

Image description

Step 9: Create Target Group

  1. Go to the AWS EC2 Console β†’ Navigate to Target Groups
  2. Click Create target group
  3. Choose Target type: Instance
  4. Enter Target group name: my-target-group
  5. Select Protocol: HTTP
  6. Select Port: 80
  7. Choose the VPC you created earlier
  8. Click Next
  9. Under Register Targets, select your EC2 instances
  10. Click Include as pending below, then Create target group

Image description

Image description

Step 10: Create an Application Load Balancer (ALB)

  1. Go to AWS EC2 Console β†’ Navigate to Load Balancers
  2. Click Create Load Balancer
  3. Choose Application Load Balancer
  4. Enter ALB Name: my-alb
  5. Scheme: Internet-facing
  6. IP address type: IPv4
  7. Select the VPC
  8. Select at least two public subnets (for high availability)
  9. Click Next

Image description

Step 11: Test the Application

  1. Restart Apache sudo systemctl restart apache2
  2. Open your browser and visit: http://your-ec2-public-ip/
  3. Fill in the form and Submit
  4. Check MySQL Database:
mysql -u admin -p -h your-rds-endpoint
USE your_database;
SELECT * FROM table_name;

Image description

This setup ensures a scalable, secure, and high-availability application on AWS! πŸš€

Follow for more and happy learning :)

Deploying a Scalable AWS Infrastructure with VPC, ALB, and Target Groups Using Terraform

By: Ragul.M
11 March 2025 at 06:01

Introduction
In this blog, we will walk through the process of deploying a scalable AWS infrastructure using Terraform. The setup includes:

  • A VPC with public and private subnets
  • An Internet Gateway for public access
  • Application Load Balancers (ALBs) for distributing traffic
  • Target Groups and EC2 instances for handling incoming requests
  • By the end of this guide, you’ll have a highly available setup with proper networking, security, and load balancing.

Step 1: Creating a VPC with Public and Private Subnets
The first step is to define our Virtual Private Cloud (VPC) with four subnets (two public, two private) spread across multiple Availability Zones.
Terraform Code: vpc.tf

resource "aws_vpc" "main_vpc" {
  cidr_block = "10.0.0.0/16"
}
# Public Subnet 1 - ap-south-1a
resource "aws_subnet" "public_subnet_1" {
  vpc_id            = aws_vpc.main_vpc.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "ap-south-1a"
  map_public_ip_on_launch = true
}
# Public Subnet 2 - ap-south-1b
resource "aws_subnet" "public_subnet_2" {
  vpc_id            = aws_vpc.main_vpc.id
  cidr_block        = "10.0.2.0/24"
  availability_zone = "ap-south-1b"
  map_public_ip_on_launch = true
}
# Private Subnet 1 - ap-south-1a
resource "aws_subnet" "private_subnet_1" {
  vpc_id            = aws_vpc.main_vpc.id
  cidr_block        = "10.0.3.0/24"
  availability_zone = "ap-south-1a"
}
# Private Subnet 2 - ap-south-1b
resource "aws_subnet" "private_subnet_2" {
  vpc_id            = aws_vpc.main_vpc.id
  cidr_block        = "10.0.4.0/24"
  availability_zone = "ap-south-1b"
}
# Internet Gateway for Public Access
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main_vpc.id
}
# Public Route Table
resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.main_vpc.id
}
resource "aws_route" "internet_access" {
  route_table_id         = aws_route_table.public_rt.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.igw.id
}
resource "aws_route_table_association" "public_assoc_1" {
  subnet_id      = aws_subnet.public_subnet_1.id
  route_table_id = aws_route_table.public_rt.id
}
resource "aws_route_table_association" "public_assoc_2" {
  subnet_id      = aws_subnet.public_subnet_2.id
  route_table_id = aws_route_table.public_rt.id
}

This configuration ensures that our public subnets can access the internet, while our private subnets remain isolated.

Step 2: Setting Up Security Groups
Next, we define security groups to control access to our ALBs and EC2 instances.
Terraform Code: security_groups.tf

resource "aws_security_group" "alb_sg" {
  vpc_id = aws_vpc.main_vpc.id
  # Allow HTTP and HTTPS traffic to ALB
  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"]
  }
  # Allow outbound traffic
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

This allows public access to the ALB but restricts other traffic.

Step 3: Creating the Application Load Balancers (ALB)
Now, let’s define two ALBsβ€”one public and one private.
Terraform Code: alb.tf

# Public ALB
resource "aws_lb" "public_alb" {
  name               = "public-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.alb_sg.id]
  subnets           = [aws_subnet.public_subnet_1.id, aws_subnet.public_subnet_2.id]
}
# Private ALB
resource "aws_lb" "private_alb" {
  name               = "private-alb"
  internal           = true
  load_balancer_type = "application"
  security_groups    = [aws_security_group.alb_sg.id]
  subnets           = [aws_subnet.private_subnet_1.id, aws_subnet.private_subnet_2.id]
}

This ensures redundancy and distributes traffic across different subnets.

Step 4: Creating Target Groups for EC2 Instances
Each ALB needs target groups to route traffic to EC2 instances.
Terraform Code: target_groups.tf

resource "aws_lb_target_group" "public_tg" {
  name     = "public-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main_vpc.id
}
resource "aws_lb_target_group" "private_tg" {
  name     = "private-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main_vpc.id
}

These target groups allow ALBs to forward requests to backend EC2 instances.

Step 5: Launching EC2 Instances
Finally, we deploy EC2 instances and register them with the target groups.
Terraform Code: ec2.tf

resource "aws_instance" "public_instance" {
  ami           = "ami-0abcdef1234567890" # Replace with a valid AMI ID
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.public_subnet_1.id
}
resource "aws_instance" "private_instance" {
  ami           = "ami-0abcdef1234567890" # Replace with a valid AMI ID
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.private_subnet_1.id
}

These instances will serve web requests.

Step 6: Registering Instances to Target Groups

resource "aws_lb_target_group_attachment" "public_attach" {
  target_group_arn = aws_lb_target_group.public_tg.arn
  target_id        = aws_instance.public_instance.id
}
resource "aws_lb_target_group_attachment" "private_attach" {
  target_group_arn = aws_lb_target_group.private_tg.arn
  target_id        = aws_instance.private_instance.id
}

This registers our EC2 instances as backend servers.

Final Step: Terraform Apply!
Run the following command to deploy everything:

terraform init
terraform apply -auto-approve

Once completed, you’ll get ALB DNS names, which you can use to access your deployed infrastructure.

Conclusion
This guide covered how to deploy a highly available AWS infrastructure using Terraform, including VPC, subnets, ALBs, security groups, target groups, and EC2 instances. This setup ensures a secure and scalable architecture.

Follow for more and happy learning :)

❌
❌