Deploy a Node Js App on AWS EC2

Deploy a Node Js App on AWS EC2

ยท

2 min read

Test the project locally

  1. Clone the project
git clone https://github.com/<user-name>/<repo-name>.git
cd <repo-name>
code .
touch .env
  1. Setup the following environment variables - (.env) file
PORT=""
  1. Initialise and start the project
npm install
npm run start

Set up an AWS EC2 instance

  1. Create an IAM user & login to your AWS Console

    • Access Type - Password

    • Permissions - Admin

  2. Create an EC2 instance

    • Select an OS image - Ubuntu

    • Create a new key pair & download .pem file

    • Instance type - t2.micro - (free tier eligible)

  3. Connect to the instance using ssh

ssh -i instance.pem ubunutu@<IP_ADDRESS>

Configure Ubuntu on remote VM

  1. Update the outdated packages and dependencies
sudo apt update
  1. Install Git - Guide by DigitalOcean
git --version
  1. Configure Node.js and npm - Guide by DigitalOcean
sudo apt install nodejs
node -v
sudo apt install npm

Deploy the project on AWS

  1. Clone the project in the remote VM
git clone https://github.com/<user-name>/<repo-name>.git
cd <repo-name>
vim .env
  1. Setup the following environment variables - (.env) file
PORT=""
  1. Initialise and start the project
npm install     #to install node_modules
npm run start

Project is deployed on AWS ๐ŸŽ‰

  1. Go check it live on :-
<your-public-ipv4-address-of-ec2>:<PORT>
#for example- 13.236.137.31:3000
  1. But you forgot to expose your port out to the world. For that my friend, we will have to edit the inbound rules in the security group of our EC2, in order to allow traffic from our particular port. To fix this, follow these quick steps:

    1. Go to "Security" and then "Security Groups."

    2. Edit the inbound rules.

    3. Add a rule for the specific port.

    4. Save the changes to allow traffic on that port.

  1. Repeat step 1. And booom.

ย