In this guide, we’ll walk through the steps to set up a scalable two-tier application on AWS. This involves creating a VPC, subnets, security groups, an RDS database, EC2 instances, an Auto Scaling group, and setting up monitoring and notifications with CloudWatch and SNS.
Step 1: Creating a VPC
- Go to the AWS Management Console and navigate to the VPC service.
2. Click “Create VPC” and provide the necessary details:
- Name tag: demo
- IPv4 CIDR block: 10.0.0.0/16
- Tenancy: Default
3. Click “Create VPC”.
Step 2: Creating a Subnet
- In the VPC Dashboard, go to the Subnets section.
2. Click “Create subnet” and fill in the details:
- VPC: Select demo
- Name tag: demo1
- Availability Zone: Choose one (e.g., us-east-1a)
- IPv4 CIDR block: 10.0.1.0/24
3. Click “Create subnet”.
Step 3: Setting Up Security Groups
- Navigate to the EC2 Dashboard, then to Security Groups.
2. Click “Create security group” and provide the following details:
- Name tag: demo
- Description: Security group for my application
- VPC: Select demo
3. Add the following inbound rules:
- HTTP: Port 80
- HTTPS: Port 443
- SSH: Port 22
- MYSQL: Port 3306
4. Click “Create security group”.
Step 4: Creating an RDS Database
- Go to the RDS service and click on Create database.
2. Select the database creation method and choose the DB type (e.g., MySQL).
3. Choose Free Tier for the instance specifications.
4. Provide the username eg.. admin and password.
5. Configure the network details:
- VPC: Select demo
- Subnet group: demo1
- give public access — YES
6. Provide a DB name and click on Create database.
Step 5: Configuring the Application
- Copy the RDS endpoint URL.
2. Update your PHP application with the RDS connection details
<?php
define('DB_SERVER', 'your-rds-endpoint-url');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'dbname');
?>
Step 6: Setting Up EC2 and Auto Scaling
- In the EC2 Dashboard, navigate to Auto Scaling Groups.
2. Create an Auto Scaling Group (ASG) and provide the name and launch template details:
- Name: demo
- Launch template: Create a new launch template
- OS: Ubuntu
- Instance type: Choose your instance type (e.g., t2.micro)
- Key pair: Create a new key pair and download it
3. Configure network settings to use demo and demo.
4. In Advanced details, add the user data script to configure the instance
This is the script i have used, this will install the dependences for the server and clone my code from my git repo. the apache2 will be installed in our instances and the code will be placed in /var/www/html/ dir . If you need to use this — change your git link.
#!/bin/bash
sudo su
# Update package list
apt update -y
# Install Apache web server
apt install apache2 -y
# Install PHP and necessary PHP modules
apt install php libapache2-mod-php php-mysql -y
# Install MySQL client
apt install mysql-client -y
# Install archive management tools
apt install rar unrar zip unzip -y
# Install Git
apt install git -y
# Navigate to the web server's root directory
cd /var/www/html/
# Clone the GitHub repository
git clone https://github.com/Vimal007Vimal/AWS-2-tier-application.git
# Remove the default Apache index file
rm -f index.html
# Move the contents of the cloned repository to the web server's root directory
cd AWS-2-tier-application
mv * /var/www/html/
# Navigate back and remove the empty directory
cd ..
rmdir AWS-2-tier-application
# Restart and enable Apache web server
systemctl restart apache2
systemctl enable apache2
here click on the create launch template
Here we can see the template review , click next we have set the network setting. Give our VPC and subnet .click next
5. Attach a new load balancer
select load balancer settings
Configure the health checks.
here give the decired capacity of instances
6. Define the scaling policies and review the settings.
here if you want to add notification
add tags if you need
Review the settings
7. Create the Auto Scaling Group.
here we can see our ASG have been created
here we can see our target group have been created
here we can see our Load balancers have been created
here we can see our instances have been created
Step 7: Testing and Verification
- Copy the DNS name of the Load Balancer and open it in a web browser.
2. Verify the application is accessible and functioning correctly.
3. Connect to one of the instances via EC2 Instance Connect and verify database access
Select your connection type — I'm using ec2 instances connect
so we have connected to our instances.
now go to our RDS service and copy our DB endpoint link
mysql -u admin -h <rds-endpoint-url> -p
- Run SQL commands to ensure the database is working.
SHOW DATABASES;
use this to see our databases.
USE databasename;
use this command to use our database.
now go to our load balancer DNS link and add some data
SHOW TABLES;
use this commands to see the tables in data base.
SELECT * FROM tablename;
use this command to display the data in the table.
IF you don’t wont to do this in your instances ,you can also in your own system
Step 8: Setting Up Monitoring and Notifications
- Go to the SNS service and create a new topic:
- Name: demo
- Type: Standard
click create topic
2. Create a subscription with your email.
select email service.
3. Confirm the subscription from your email.
Here we can see our subscription
Now we need to set the alarm for that go to cloud watch
Select create alarm
- Select the metric (e.g., status check failed for instances).
Select auto scaling ( because our instance is running from the template of auto scaling group )
Select the metric you need ( here I have selected status check failed instances)
Now give next
Select the conditions
Here select our topic we created earlier
Now we can see our mail
Name the alarm and review the settings.
Create the alarm.
Here we can see our alarm
In our case if a instance is running with no usage it will be mailed like this