๐Ÿ’ป

Hosting Node.js Application on AWS EC2 Instances

Jul 8, 2024

Hosting Node.js Application on AWS EC2 Instances

Introduction

  • This video demonstrates hosting a Node.js application on AWS EC2.
  • Using a sample code contributed by a GitHub open-source contributor, Chance.
  • Made modifications to the original code as per requirements.
  • Useful for testing simple deployments on cloud environments.
  • README notes provided for steps.

Setting Up EC2 Instance

  • Created an EC2 instance named node.js application; platform: Ubuntu.
  • Separate video available on creating an EC2 instance.
  • Connection established using EC2 instance connect method.
  • Commands to switch to super user: sudo su -

Application Code

  • Creates an HTTP server listening on port 80.
  • Expected output: 'monk in Cloud' when accessed via public IP in browser.

Steps to Host Application

Step 1: Install Node.js and npm using NVM

  • Become super user.
  • Run the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
  • Activate NVM: source ~/.nvm/nvm.sh
  • Install the latest version of Node.js: nvm install node
  • Verify installations: node -v npm -v

Step 2: Install Git

  • Update the system: sudo apt-get update -y
  • Install Git: sudo apt-get install git -y
  • Verify Git version: git --version

Step 3: Clone Repository

  • Clone required repository: git clone <repository-url>
  • Navigate to cloned directory: cd node.js-on-ec2

Step 4: Install Dependencies

  • Install npm packages: npm install

Step 5: Start Application

  • Start the Node.js application: npm start
  • Verify itโ€™s running on port 80. Access via public IP of EC2. Expected output: 'monk in Cloud'

Domain Mapping with Route 53

  • Domain purchased via Freenom, example: a monk in cloud.ml.
  • Create a hosted zone in Route 53.
  • Add name server entries from Route 53 to domain registrar (Freenom).
  • Create A Record:
    • Points to EC2 public IP.
  • Create CNAME Record:
    • Alias to public IP.
  • Verify domain in browser.

Conclusion

  • Successfully hosted Node.js app on EC2.
  • Additional resources: Route 53 for domain mapping.
  • Call to action: Subscribe and share.