About Deploy a simple node js app on EC2 ( Elastic Compute Cloud ) AWS instance.
Steps followed:
Part A)
1/ Install the nodejs on Linux machine with -> sudo dnf install nodejs
2/ Run the commands to check installation status & version :
node -v
npm -v
3/ Create a node.js app named simple-nodejs2.js and run it on local machine using below command :
node simple-nodejs2.js
It won't give any output on terminal, so go to browser and type http://localhost:3000 to test if it display output.
4/ If you get an error message like below means port 3000 is already being used by other service. So check which service is using that port :
throw er; // Unhandled 'error' event
Error: listen EADDRINUSE: address already in use :::3000
and so on...
ss -tulnp | grep 3000 # This will display PID of service using 3000 port
kill -9 257157
5/ Run the app again and it should works. Once nodejs works on local machine next is to deploy on AWS from the Github.
Part B)
1/ Clone below repository to your local machine or you can create your own repository too.
git clone https://github.com/yoginderbagga/nodejs.git
2/ Create a text file and add the code of your node.js [ simple-nodejs2.js ]
3/ Next go to AWS Console -> Select EC2 Instance -> Launch an Instance and fill below details :
Name and tags : Enter the name of your Instance
Application and OS Images (Amazon Machine Image) : Select Ubuntu
Amazon Machine Image (AMI) : Select Ubuntu Server ( Free Tier Eligible )
Instance type : t3.micro ( Free Tier Eligible )
Key Pair : Click create key pair and fill details. Select .pem and give a name.
Network settings : Check the "Allow SSH traffic from" option 0.0.0.0/0 ( Since its a test envirionment choose the option. Be careful if you need to allow SSH from specific host ) and click "Launch an Instance"
4/ Select the instance which is created and click "Connet" button. Next, go to terminal on your local linux Laptop and connect with AWS Instance using SSH
ssh -i my-key4.pem ubuntu@44.192.X.X # Replace the IP Address with Public IP displayed in EC2 Instance.
5/ Run below command to clone the git repo and install nodejs on your ubuntu AWS Instance
ubuntu@ip-172-31-67-215:~$ git clone https://github.com/yoginderbagga/nodejs.git
sudo -i [ To install it as sudo ( root user ) ]
apt update
apt install nodejs
6/ Finally run node js app from the EC2 Instance and try to access it from your Local Machine
node simple-nodejs2.js
On browser type type Public IP of your EC2 instance with port number, http://44.192.252.146:3000
You may not get the output and receive the error message. Its because the port needs to be bind with the public IP.
7/ Go to "Security" in EC2 Instance, search for "Inbound rules" and add a new inbound rule there to allow the port to be bind.
Add port range = 3000 and type Custom TCP ( default selected ) and source = 0.0.0.0/24
8/ Test from the local machine again, http://44.192.252.146:3000 and its good to go.
Envirionment:
Any Linux OS [ I am using Fedora Linux for Local machine and Ubuntu on AWS EC2 Instance ]
AWS
Nodejs & NPM
note : First try to running the same application on your local host machine to check syntax and all is correct and then deploy on the AWS.