DEV Community

Subham Nandi
Subham Nandi

Posted on

Deploying a Node Js Application on AWS EC2

GitHub Link - https://github.com/SUBHAM-NANDI/Project1-Deploying-NodeJs-Application-on-AWS

Testing the project locally

  • Clone this project on the local machine.
git clone https://github.com/SUBHAM-NANDI/AWS-Session.git
Enter fullscreen mode Exit fullscreen mode
  • Open the project on VSCode.

  • Create an environment variable(.env) file.

 touch .env
Enter fullscreen mode Exit fullscreen mode

This will create a hidden .env file.

Environment variable files are essential for securely managing configuration settings and secrets across different environments (development, testing, production). They separate configuration from code, enhance security by preventing hardcoding of sensitive data, ensure consistency, simplify deployment, and support easy environment-specific configurations. This centralized management approach promotes maintainability and flexibility, making managing and updating settings easier without altering the codebase.

  • Setup the following environment variables - (.env) file
DOMAIN= ""
PORT=3000
STATIC_DIR="./client"

PUBLISHABLE_KEY=""
SECRET_KEY=""
Enter fullscreen mode Exit fullscreen mode

Create a Stripe account for free and copy the publishable and secret keys, respectively.
Document link - https://docs.stripe.com/keys

  • Initialise all dependencies and start the project
npm install 
npm run start
Enter fullscreen mode Exit fullscreen mode

Set up an AWS EC2 instance

  • Create an IAM user & login to your AWS Console

    • Access Type - Password
    • Permissions - Admin
  • Create an EC2 instance

    • Select an OS image - Ubuntu
    • Create a new key pair & download .pem file
    • Instance type - t2.micro
  • Connecting to the instance using ssh

Open an SSH client.
Locate your private key file. The key used to launch this instance is node.js-app-deploy.pem
Run this command, if necessary, to ensure your key is not publicly viewable.
Change the permission of the .pem file using the below cmd.

chmod 400 "instance.pem"
Enter fullscreen mode Exit fullscreen mode

Connect to the ec2 instance using the below cmd

ssh -i instance.pem ubunutu@<PUBLIC IP_ADDRESS>
Enter fullscreen mode Exit fullscreen mode
  • Updating the outdated packages and dependencies
sudo apt update
Enter fullscreen mode Exit fullscreen mode
sudo apt install git
Enter fullscreen mode Exit fullscreen mode
sudo apt install nodejs
sudo apt install npm
Enter fullscreen mode Exit fullscreen mode

Deploying the project on AWS

  • Clone this project in the remote VM
git clone https://github.com/SUBHAM-NANDI/AWS-Session.git
Enter fullscreen mode Exit fullscreen mode
  • Create and open an environment variable(.env) file in the project folder.
touch .env
vim .env
Enter fullscreen mode Exit fullscreen mode
  • Setup the following environment variables - (.env) file
DOMAIN= ""
PORT=3000
STATIC_DIR="./client"

PUBLISHABLE_KEY=""
SECRET_KEY=""
Enter fullscreen mode Exit fullscreen mode

For this project, we'll have to set up an Elastic IP Address for our EC2 & that would be our DOMAIN

  • Initialise and start the project
npm install
npm run start
Enter fullscreen mode Exit fullscreen mode

NOTE - We will have to edit the inbound rules in the security group of our EC2, in order to allow traffic from our particular port
Go to the EC2 instance and go to security. Click on Security groups and then edit the Inbound Roules.
Add a separate Inbound rule with *Type - Custom TCP * and post range - 3000 and then save.

Project is deployed on AWS 🎉

IMPORTANT - THIS NODE.JS APP HAS BEEN TAKEN FROM SOMEWHERE ELSE.

Top comments (0)