App Instance Deployment

Deploying an EC2 Instance

  1. Navigate to the EC2 service dashboard and click on Instances on the left-hand side.
  2. Click Launch Instances.
  3. Select the first Amazon Linux 2 AMI.
  4. Choose the T2.micro instance type (Free Tier eligible) and click Next: Configure Instance Details.
  5. Proceed without key-pair because we will use EC2 Instance Connect.
  6. Configure the instance details:
    • Select the correct Network, Subnet, and IAM role (previously created).
    • Use one of the private subnets created for this layer.
  7. Use the already created:
    • VPC and Private Subnet AZ-1.
    • Private Instance Security Group.
    • IAM Role.
  8. Click Launch Instance.

Image description

Connecting to the EC2 Instance

  1. Navigate to the EC2 dashboard and click on Instances.
  2. Once the instance is running, select it and click Connect.
  3. Go to the Session Manager tab and click Connect.

Note: If you cannot connect via Session Manager, verify that:

  • The instance can route to your NAT Gateway.
  • The IAM role has necessary permissions for EC2 Instance Connect.
  1. Once connected, switch to the ec2-user:
sudo -su ec2-user
  1. Check if the instance has internet access:
ping 8.8.8.8
  • If there is no response, check route tables and subnet associations.

Configuring the Database

  1. Install MySQL CLI:
sudo yum install mysql -y
  • If you encounter errors, 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
    
  1. Connect to the Aurora RDS writer endpoint:
mysql -h  -u  -p
  1. Create a database:
CREATE DATABASE webappdb;
  1. Verify the database:
SHOW DATABASES;
  1. Use the database and create a table:
USE webappdb;
   CREATE TABLE IF NOT EXISTS transactions(
       id INT NOT NULL AUTO_INCREMENT,
       amount DECIMAL(10,2),
       description VARCHAR(100),
       PRIMARY KEY(id)
   );
  1. Insert test data:
INSERT INTO transactions (amount,description) VALUES ('400','groceries');
  1. Verify the data:
SELECT * FROM transactions;
  1. Exit MySQL:
exit;

Image description

Configuring the App Instance

  1. Update the database credentials in DbConfig.js:
    • Fill in hostname, user, password, and database details.
    • Use the Aurora RDS writer endpoint as hostname.
    • Set database name as webappdb.
  2. Upload the app-tier folder to the S3 bucket.
  3. Install NVM (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
   source ~/.bashrc
  1. Install Node.js:
nvm install 16
   nvm use 16
  1. Install PM2 (Process Manager):
npm install -g pm2
  1. Download the application code from S3:
cd ~/
   aws s3 cp s3:///app-tier/ app-tier --recursive
  1. Install dependencies and start the app:
cd ~/app-tier
   npm install
   pm2 start index.js
  1. Verify that the app is running:
pm2 list
  • If errored, check logs:

     pm2 logs
    
  1. Enable PM2 auto-start:
pm2 startup
  • Copy and run the output command in your terminal.
  • Save the current processes:

     pm2 save
    

Testing the App Tier

  1. Test health check endpoint:
curl http://localhost:4000/health
  • Expected response:

     "This is the health check"
    
  1. Test database connection:
curl http://localhost:4000/transaction
  • Expected response:

     {"result":[{"id":1,"amount":400,"description":"groceries"}]}
    

PART-5