Deploy Node-Express Server in AWS EC2 Instance
Last Updated :
18 Sep, 2023
AWS or Amazon Web Services is a leading cloud services provider which gives access to a number of on-demand computational services, databases, storage space, etc.EC2 or Elastic Compute Cloud is an on-demand computing service on the AWS cloud platform. Under computing, it includes all the services a computing device can offer to you along with the flexibility of a virtual environment.
Steps to Create a New AWS Instance For Deployment
To create a free AWS account refer to Amazon Web Services (AWS) – Free Tier Account Set up.
Step 1: Go to the AWS website and search for EC2.
Step 2: Click on Launch Instance to create a new instance. To know the instance types refer to Amazon EC2 – Instance Types
Step 3: Give a suitable name for your instance.
.jpg)
Step 4: Select UBUNTU as the Amazon Machine Image.

Step 5: In the Key-pair section, create a new key pair (if you don’t have one already). Give a suitable name for your key and save it on your local machine. This key would help to log in to our instance using an SSH client.

Step 6: In the Network Settings section, allow HTTPS and HTTP request traffic which are required when creating and using a web server.

Step 7: With all these settings, we are good to launch the instance. Click on the launch instance button on the right-hand bottom side.
Step 8: It will take a few seconds for the instance to start completely and go into a running state.

Connect to the Instance Using SSH Client
Step 1: Once the instance is in a running state, click on Instance ID. It will open up a page with all the details of a particular instance. Click on Connect button on the top right-hand side of the page.

Step 2: Open the SSH client on your system. (Terminal for Linux/MacOS, PuTTy for Windows).
Step 3: Go to the folder where you stored your private key file onto the terminal using the following command:
cd <path-to-your-folder>
Step 4: To give execute permission to the secret key file, execute the following command:
chmod 400 <privateKeyFile.pem>
Step 5: Connect to your instance using its public DNS. Copy the command given in the Example section and execute it on your terminal.
Step 6: After running this command, you will be asked “Are you sure you want to continue connecting (yes/no/[fingerprint])?”. Put “yes” and click Enter.

Step 7: You are now successfully connected to your instance on AWS from your local machine using an SSH client.
Download the Code For Node Express Server
Step 1: Go to your GitHub repository and copy the HTTP link to clone the repo.
Step 2: Run the following command on the terminal. It would work fine if the repo is public, for private repos you will need to insert your GitHub username and password.
git clone <http-link-for-github-repo>

Install Node To Run The Server
Step 1: Use the following command to install curl on the instance.
sudo apt-get install curl

Step 2: Use this curl command to install node.
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

Step 3: Run the below command to install nodeJs on our AWS instance.
sudo apt-get install nodejs

Step 4: Check if the node is properly installed on the instance.
node -v
Install All Dependencies Required to Run The Node Express Server Project
Step 1: Go to the project folder which we cloned from git.
cd path/to/folder
Step 2: Execute the following command to install all the dependencies to run the project.
npm install

Step 3: Use the following command to run the server. (This can be different based on the project settings. Check the scripts section in your package.json to get the start command). This will start the node server which will be listening for requests on some port number
npm start

Configure the Security Group For the Port Where Our API is Running
Step 1: Go to your AWS instance and select the Security tab.

Step 2: Click on the security group to which your instance is configured and it will open up the configuration settings.

Step 3: In the Inbound Rules section, go to Edit Inbound Rules and it will open up a dialog box. Go to Add Rule and add a new Custom TCP type with your Node server’s port number as Port Range and source as Anywhere-IPv4. Please refer to the screenshot to see how final settings on the dialog box should look like.

Access the Application
Step 1: Go to your instance and copy the public IPv4 address.

Step 2: Go to your preferred browser and hit the server endpoint using the following URL:
http://<publicIPv4Address>:<PortNumber>/<Endpoint>

Similar Reads
How To Deploy Flask APP On AWS EC2 Instance?
In this article, we will study how we can deploy our existing Flask application in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the flask application. For this article, you should know about setting up EC2 in AWS. So, let's begin with the deployment. Primary Termi
5 min read
Create Ubuntu Server on AWS EC2 Instance
In this article, we'll take you through the entire process of creating an Ubuntu server on an AWS EC2 instance from scratch. Whether you're new to Amazon Web Services (AWS) or just looking to set up a new server, this step-by-step tutorial will cover everything you need to know. We'll start right fr
6 min read
How To Create Redhat EC2 Instance in AWS
provisioning the Red Hat Enterprise Linux (RHEL) instances on Amazon Web Services (AWS) offers a powerful and versatile solution for hosting and running applications, overseeing the jobs, and utilizing the abilities of both platforms. Red Hat Enterprise Linux is a main Linux distribution eminent for
5 min read
How to Attach EBS Volume in EC2 Instance?
Pre-requisite: AWS, EC2, and EBS Elastic Block Store is a type of storage in AWS. It behaves like a virtual hard drive device in AWS and it is persistent storage. In EC2- The instance root volume will be destroyed when we terminate the EC2 instance, as well as data, will also be deleted. That's why
2 min read
How to Install MongoDB on AWS EC2 Instance?
AWS or Amazon web services is a cloud service platform that provides on-demand computational services, databases, storage space, and many more services. EC2 or Elastic Compute Cloud is a scalable computing service launched on the AWS cloud platform. In simpler words, EC2 is nothing but a virtual com
2 min read
How to Install SQL Server Express in Linux?
Microsoft SQL Server Express is a feature-limited edition of Microsoft's SQL Server relational database management which is free to download, distribute, and use. It is used for creating or developing small-scale applications. It has been published since the SQL Server 2005. Microsoft provides it fo
2 min read
Deploy Flask App on AWS EC2 Windows
Do you want to host a Windows-based Python Flask application on Amazon Web Services (AWS) Elastic Compute Cloud (EC2)? Flask is a frequently utilized framework for building web apps, APIs, and prototypes because of its ease of use and versatility. AWS EC2 provides virtual machines that are scalable,
5 min read
How to install Ansible in AWS EC2 Server ?
Ansible is an automation tool which helps in managing the configuration over multiple machines . On the other hand AWS EC2 is a web service which allows users to rent any required virtual server . Here in this guide, we will first discuss what is Ansible. Then we will discuss AWS EC2 Service. After
5 min read
Database Integration in Express.js
Express is a minimalistic framework that works along with Node.js and provides salient features to run a backend server and much more. As you know the database plays a vital role in a fully working application to persist data. In this article, we are going to discuss how we can integrate a database
8 min read
Why Express is used in Web Development?
Express JS is a small web application framework. It become very popular Node JS framework for building robust and scalable application. In this article we will dive into the topic of ExpressJ S. Table of Content What is Express JS?Why Express is used in web development?Steps to create an Express app
3 min read