3-Tier Architecture (Part-4)

3-Tier Architecture (Part-4)

Publish Date: Mar 31
1 0

AWS Three-Tier Architecture: App Instance Deployment

Table of Contents

App Instance Deployment

  1. Navigate to the EC2 service dashboard and click on Instances on the left-hand side.
  2. Click Launch Instances.

Image description

  1. Select the Amazon Linux 2 AMI.

Image description

  1. Choose t2.micro (Free Tier Eligible) and click Next: Configure Instance Details.
  2. Proceed without key-pair since we will use EC2-instance login.
    Image description

  3. Configure the instance details:

    • Select the correct Network (VPC) and Private Subnet (AZ-1). Image description
  • Attach the IAM role created earlier.
    Image description

  • Use the private instance security group created before.
    Image description

  1. Click Launch Instance.

Connect to Instance

  1. Navigate to the Instances list in the EC2 dashboard.
  2. Once the instance state is running, select it and click Connect.
    Image description

  3. 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.
  1. Once connected, switch to ec2-user:
   sudo -su ec2-user
Enter fullscreen mode Exit fullscreen mode
  1. Check if the instance can access the internet:
   ping 8.8.8.8
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Connect to RDS Database

mysql -h YOUR-RDS-ENDPOINT -u YOUR-USERNAME -p
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Insert Test Data

INSERT INTO transactions (amount, description) VALUES ('400', 'groceries');
SELECT * FROM transactions;
Enter fullscreen mode Exit fullscreen mode

Exit MySQL:

exit
Enter fullscreen mode Exit fullscreen mode

Configure App Instance

Update Database Credentials

  1. Open application-code/app-tier/DbConfig.js.
  2. Update the hostname, user, password, and database.
  3. Save the file.

Upload App Code to S3

  1. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check if App is Running

pm2 list
Enter fullscreen mode Exit fullscreen mode

If the status is errored, check logs:

pm2 logs
Enter fullscreen mode Exit fullscreen mode

Enable Auto-Start for PM2

pm2 startup
Enter fullscreen mode Exit fullscreen mode

Copy the exact command from the output and run it.

Save the PM2 process list:

pm2 save
Enter fullscreen mode Exit fullscreen mode

Test App Tier

Health Check Endpoint

curl http://localhost:4000/health
Enter fullscreen mode Exit fullscreen mode

Expected response:

"This is the health check"
Enter fullscreen mode Exit fullscreen mode

Database Connection Test

curl http://localhost:4000/transaction
Enter fullscreen mode Exit fullscreen mode

Expected response:

{"result":[{"id":1,"amount":400,"description":"groceries"}]}
Enter fullscreen mode Exit fullscreen mode

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)

Comments 0 total

    Add comment