EC2 Instance Setup

  • Open EC2 Console: Navigate to the AWS Management Console and open the EC2 service.
  • Launch Instance: Click “Launch Instance” to start creating a new EC2 instance.
  • Configure Instance:
    • Name the instance.
    • Choose Ubuntu (or preferred OS).
    • Select instance type: t2.micro is good for small projects.
  • Create Key Pair: Name it, choose RSA, and download the .pem file.
  • Security Group:
    • Allow SSH traffic from Anywhere (restrict later if needed).
    • Allow HTTP traffic from the internet.
  • Launch the Instance: Click “Launch” and wait for the instance to start.

SSH Access

  • Move .pem File & Set Permissions:
    cd /path/to/folder
    chmod 400 filename.pem
  • SSH into Instance:
    ssh -i <filename>.pem ubuntu@<Public IPv4 address>
  • Verify Connection:
    whoami

Installing Node.js

  • Update System:
    sudo apt update -y
  • Install Node.js and npm:
    sudo apt install nodejs -y
    sudo apt install npm -y

Cloning GitHub Repository and Environment Setup

  • Clone Repo:
    git clone <repo link>
  • Set Up Environment:
    cd project
    vim .env  # or nano .env
    npm install
  • Fastify Note: Add host: '0.0.0.0' in index.ts if using Fastify.

Using pm2 to Run Node.js App

  • Install pm2:
    sudo npm install -g pm2
  • Start App with pm2:
    pm2 start dist/index.js
    pm2 start dist/index.js --interpreter $(which node)  # if needed
  • Logs and Monitoring:
    pm2 log index
    pm2 flush
    pm2 monit
  • Delete App:
    pm2 delete index

AWS Security Group Configuration

  • Edit Inbound Rules:
    • Add Custom TCP rule for app port (IPv4 + IPv6).
  • Edit Outbound Rules:
    • Ensure necessary outbound traffic is allowed.

Elastic IP Setup

  • Allocate Elastic IP:
    • Go to EC2 sidebar → Elastic IPs → Allocate.
  • Associate Elastic IP:
    • Attach it to your EC2 instance.
  • Permanent Public IP: This IP remains constant across reboots.

Redis Setup on AWS

Option 1: Elastic Cache

Use AWS Elastic Cache for managed Redis.

Option 2: Manual Setup (on EC2)

  • Follow EC2 Setup Steps: (Same as earlier, reuse existing .pem or create new one.)
  • SSH into Instance

Install Redis

  • Update & Install:
    sudo apt update
    sudo apt install redis-server
  • Enable Redis:
    sudo systemctl enable redis-server
  • Edit Config:
    sudo vim /etc/redis/redis.conf
    Change:
    bind 0.0.0.0 -::1
    protected-mode no
    
  • Start Redis:
    sudo systemctl start redis-server

Configure Redis Security Group

  • Add two Inbound Rules:
    • Port: 6379
    • Protocol: Custom TCP
    • Source: Anywhere (IPv4 and IPv6)

Test Redis Connection

On your local machine:

redis-cli -h <Public IP> -p 6379

If it fails, restart Redis:

sudo systemctl restart redis-server

Docker Installation on EC2

  • Update System:
    sudo apt update
  • Install Dependencies:
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
  • Add Docker GPG Key:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Add Docker Repo:
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
  • Install Docker:
    sudo apt install docker-ce
  • Add User to Docker Group:
    sudo usermod -aG docker ${USER}
    sudo chmod o+rw /var/run/docker.sock
  • Check Docker Status:
    sudo systemctl status docker
  • Test Docker Installation:
    docker run hello-world

Note: If this fails, close the terminal and SSH again.