A simple Node.js application deployed on AWS EC2 to demonstrate the ease of setting up and running a Node.js server on AWS.
Using AWS EC2 for hosting your Node.js application offers several benefits:
- Scalability: Easily scale your application based on traffic and resource demands.
- Reliability: EC2 instances are hosted on Amazon's robust and secure cloud infrastructure.
- Cost-effective: With flexible pricing models (including free tier for small projects), you can start small and scale as needed.
- Global Reach: Deploy your application in different AWS regions around the world to reduce latency and enhance user experience.
Follow these steps to get your Node.js application running on your AWS EC2 instance.
First, install NVM (Node Version Manager) and then use it to install the latest version of Node.js:
sudo su -
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh # Activate NVM
nvm install node # Install Node.js
node -v # Check Node.js version
npm -v # Check npm version
Install Git:
sudo apt-get update -y
sudo apt-get install git -y
Clone the node.aws
Repository:
git clone https://github.com/ahadalireach/node.aws.git
cd node.aws # Navigate to the project directory
To install the required dependencies, run:
npm install
Start the Node.js server by running:
npm start
Go to your instance's public IP address to check if the application works. The URL should look like this: http://<your-ec2-public-ip>
.
If your application is not accessible via the public URL, follow these steps to ensure the security group is correctly configured:
- Open the Amazon EC2 Console.
- Navigate to Instances and select your EC2 instance.
- In the instance details, find the Security section and click on the security group name (e.g.,
sg-xxxxxxxx
). - Click on the Inbound Rules tab, then click the Edit inbound rules button.
- Click Add Rule and set the following:
- Type: HTTP
- Protocol: TCP
- Port Range: 80
- Source: 0.0.0.0/0 (This allows access from any IP. You can restrict it to specific IPs if needed).
- Click Save rules.
- After updating the security group, wait a few moments and try accessing the application again via your EC2 instance's public IP.