AWS Three-Tier Architecture: App Instance Deployment
Table of Contents
- App Instance Deployment
- Connect to Instance
- Configure Database
- Configure App Instance
- Test App Tier
- Conclusion
App Instance Deployment
- Navigate to the EC2 service dashboard and click on Instances on the left-hand side.
- Click Launch Instances.
- Select the Amazon Linux 2 AMI.
- Choose t2.micro (Free Tier Eligible) and click Next: Configure Instance Details.
Proceed without key-pair since we will use EC2-instance login.
-
Configure the instance details:
- Select the correct Network (VPC) and Private Subnet (AZ-1).
- Select the correct Network (VPC) and Private Subnet (AZ-1).
- Click Launch Instance.
Connect to Instance
- Navigate to the Instances list in the EC2 dashboard.
Once the instance state is running, select it and click Connect.
Go to the Session Manager tab and click Connect (Opens a new browser tab).
Note: If you cannot connect via Session Manager, ensure:
- Your instance can route to NAT Gateway.
- The IAM role has the necessary permissions.
- Once connected, switch to
ec2-user
:
sudo -su ec2-user
- Check if the instance can access the internet:
ping 8.8.8.8
- If you cannot reach the internet, check Route Tables and Subnet Associations for NAT Gateway routing.
Configure Database
Install MySQL CLI
sudo yum install mysql -y
If the above fails, try:
sudo wget https://dev.mysql.com/get/mysql57-community-release-e17-11.noarch.rpm
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
sudo yum install https://dev.mysql.com/get/mysql57-community-release-e17-11.noarch.rpm
Connect to RDS Database
mysql -h YOUR-RDS-ENDPOINT -u YOUR-USERNAME -p
Enter your password when prompted.
Create a Database and Table
CREATE DATABASE webappdb;
SHOW DATABASES;
USE webappdb;
CREATE TABLE IF NOT EXISTS transactions (
id INT NOT NULL AUTO_INCREMENT,
amount DECIMAL(10,2),
description VARCHAR(100),
PRIMARY KEY(id)
);
SHOW TABLES;
Insert Test Data
INSERT INTO transactions (amount, description) VALUES ('400', 'groceries');
SELECT * FROM transactions;
Exit MySQL:
exit
Configure App Instance
Update Database Credentials
- Open
application-code/app-tier/DbConfig.js
. - Update the hostname, user, password, and database.
- Save the file.
Upload App Code to S3
- Upload the
app-tier
folder to the S3 bucket created earlier.
Install Node.js and Dependencies
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16
npm install -g pm2
Download and Run App
cd ~/
aws s3 cp s3://BUCKET_NAME/app-tier/ app-tier --recursive
cd ~/app-tier
npm install
pm2 start index.js
Check if App is Running
pm2 list
If the status is errored, check logs:
pm2 logs
Enable Auto-Start for PM2
pm2 startup
Copy the exact command from the output and run it.
Save the PM2 process list:
pm2 save
Test App Tier
Health Check Endpoint
curl http://localhost:4000/health
Expected response:
"This is the health check"
Database Connection Test
curl http://localhost:4000/transaction
Expected response:
{"result":[{"id":1,"amount":400,"description":"groceries"}]}
If both tests pass, the app layer is configured correctly.
Conclusion
This completes the App Tier setup for our AWS Three-Tier Architecture.
➡️ Proceed to Part 5 to continue the implementation:
Continue to the next Part AWS Three-Tier Architecture (Part-5)