-
Notifications
You must be signed in to change notification settings - Fork 0
Deploying Node.js App on an AWS EC2 Instance
This guide outlines the essential steps for deploying a Node.js application on an AWS EC2 Linux instance. Opting for EC2 to host a Node.js server presents numerous advantages, making it a popular choice for web application deployment. The service not only offers flexibility but also grants users full control over their environment. With a wide range of different options, we are able to tailor EC2 to meet the requirements of our application.
The steps involving creating an AWS EC2 instance are detailed in the following page:
There are several methods in which you can SSH into your EC2 instance
You can SSH into the Linux instance from a local machine that runs a Linux or macOS by using an SSH Client of your choice.
- In a terminal window, use the ssh command to connect to the instance. You will need the name of the private key (.pem file) that was generated when you first created your EC2 instance and the public DNS name of your instance. The command will be in the format of:
ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name
- You will then be prompted to answer if you would like to continue connecting. Entering "yes" will then connect you to the instance.
Amazon EC2 Instance Connect offers a streamlined and secure method for establishing connections to Linux instances using SSH. AWS Identity and Access Management (IAM) policies and principals are used to control SSH access. This method effectively bypasses the need of handling and sharing SSH keys, which simplifies key management. Using EC2 Instance Connect will also allow us to access the instance from anywhere. Thus, EC2 Instance Connect will be our preferred method of accessing our instance.
When trying to connect to the instance, we are brought directly to this page where we are given different options to connect to the instance.
Once connected, you are brought into a Linux instance where the Github repository resides.
After you SSH into the EC2 instance, you need to change the user to a super user in order to have the necessary permissions by using the command:
sudo su
We install nvm (node version manager) by using the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Next you will need to activate nvm with the following command:
. ~/.nvm/nvm.sh
Using nvm, we install Node.js with the below command:
nvm install 16
In order to get the source code of the application from GitHub, you must install Git first. This can be done by using the command:
yum update -y
After installing Git, we clone the repository of our application. In our case, we use the following command:
git clone https://github.uconn.edu/mrd19007/CSESDP32
To install the dependencies, use the below command:
npm install
Once everything is set up and all dependencies are installed, you can run the application with the command:
node index.js
After running the application, you should now be able to connect to your application using the public IP of the machine. AWS will also auto generate a public DNS in which you can also access the application. The Public DNS will formatted similarly to the example below:
ec2-54-145-8-140.compute-1.amazonaws.com