Task # 3 Deploying a Backend NestJS Project on AWS EC2 Instance

Task Overview:

  • Launch an AWS EC2 instance.

  • Connect to the instance via SSH.

  • Download and set up a basic NestJS project from GitHub.

  • Run the server and access your API.

Step 1: Launching an EC2 Instance

  1. Log in to the AWS Console:

  2. Navigate to EC2:

    • In the search bar at the top, type EC2 and click on the EC2 service.

  3. Launch an EC2 Instance:

    • Click on the “Launch Instance” button.

    • Name your instance (e.g., “NestJS Instance”).

  4. Choose an Amazon Machine Image (AMI):

    • Select an AMI. For this guide, choose Amazon Linux 2 or Ubuntu (both are common for deploying applications).
  5. Choose an Instance Type:

    • For basic projects, the free-tier-eligible t2.micro instance is sufficient.

  6. Configure Security Group:

    • Create a new security group that allows:

      • SSH (port 22) for your IP address (for connecting to the instance).

      • HTTP (port 80) or HTTPS (port 443) to allow web traffic.

    • You can later configure it to allow more ports if needed.

  7. Create or Select a Key Pair:

    • Create a new key pair or use an existing one. The key pair is needed to connect via SSH. Download it and store it securely.

  8. Launch the Instance:

    • Click on “Launch” to spin up the instance.

Step 2: Connect to the EC2 Instance Using SSH

  1. Obtain the Public IP Address:

    • Once the instance is launched, go to your EC2 dashboard and copy the public IP address of the instance.

  2. SSH into Your Instance:

      1. Open your EC2 instance

        1. Select and click on Connect

        2. Open Terminal (on your local machine).

        3. Connect to the Instance:

          • Be in the folder, where your key pair file is saved

          • Use the above commands one by one to connect via SSH:

          • for e.g

                ssh -i "your-key-pair.pem" ubuntu@your-ec2-instance-public-dns
            
  3. Basic System Update (optional but recommended):

    • Once logged into the instance, update your system:
    sudo yum update -y   # For Amazon Linux 2
    sudo apt-get update  # For Ubuntu

Step 3: Set Up Node.js on EC2

  1. Install Node.js:

    • Depending on your AMI, run one of the following commands to install Node.js and npm:

https://nodejs.org/en/download/package-manager

  1. Might face issue in NVM , that it is not found, so do remember to active NVM
    Activate NVM:

    • After installation, run the following command to activate NVM:

        source ~/.bashrc
      
  2. Verify NVM installation:

    • To ensure NVM is properly installed, run:

        nvm --version
      
  3. Check if Node.js is installed:

     node -v
     npm -v
    

Step 4: Download the NestJS Project from GitHub

  1. Install Git (if it's not already installed):

     sudo yum install git -y  # For Amazon Linux 2
     sudo apt-get install git -y  # For Ubuntu
    
  2. Clone Your NestJS Repository:

    • Replace <your-github-repo> with the URL of your GitHub repository.
    git clone https://github.com/<your-username>/<your-github-repo>.git
  1. Navigate to Your Project Folder:

     cd <your-github-repo>
    
  2. Install Dependencies:

    • Install the dependencies for your project using npm:
    npm install

Step 5: Deploy the NestJS Application

  1. Run the NestJS Application:

    • Start your NestJS application using the following command:
    npm run start

This will start the NestJS server on its default port (usually 3000).

  1. Verify the Application is Running:

    • Check the logs to make sure the server has started successfully. You should see a message like:
    Nest application successfully started on port 3000

Step 6: Access the API

  1. Modify Security Group:

    • Ensure that port 3000 is open in the EC2 security group so that you can access your API.

    • Go to your EC2 dashboard → Select your instance → Click on the Security tab → Edit the security group and add an inbound rule for Custom TCP on port 3000 for your IP or 0.0.0.0/0 (for public access).

  2. Access the API from Your Browser:

    • Open your browser and type:
    http://<your-public-ip>:3000

This should display your NestJS API. You can hit the different endpoints of your API from here.

Summary of Steps:

  1. Launch EC2 instance and configure security groups.

  2. Connect via SSH using your key pair.

  3. Install Node.js, Git, and other required dependencies.

  4. Clone your NestJS project from GitHub and install dependencies.

  5. Run the NestJS server and access the API via the public IP.

By following this guide, you will be able to launch and deploy your NestJS project on an AWS EC2 instance, connect to it, and make it accessible via the public IP.