Create EC2 Instance and Deploy Nodejs Application


AWS: 

Amazon Web Services (AWS) is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments on a metered pay-as-you-go basis. In aggregate, these cloud computing web services provide a set of primitive abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet. AWS’s version of virtual computers emulates most of the attributes of a real computer, including hardware central processing units (CPUs) and graphics processing units (GPUs) for processing; local/RAM memory; hard-disk/SSD storage; a choice of operating systems; networking; and pre-loaded application software such as web servers, databases and customer relationship management (CRM). 

What is Amazon EC2?

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic. 

Create an AWS EC2 Ubuntu Instance:  

image-1 Create EC2 Instance and Deploy Nodejs Application
image-17 Create EC2 Instance and Deploy Nodejs Application

To create a new EC2 instance. 
Click > EC2 dashboard. Here, If no instances are running, Click > instances and select Launch Instances

[ Step 1: Choose an Amazon Machine Image (Ami) ] 
Search for Ubuntu in the search bar 

Click > Ubuntu Server 18.04 LTS (HVM), SSD Volume Type, which is the latest Ami from Ubuntu  

Select and use > 64-bit version. 

image-16 Create EC2 Instance and Deploy Nodejs Application

[ Step 2: Choose an Instance Type ] 
We’ll create a medium instance  

Use a t2.medium instance. Click> Next: Configure Instance Details 

image-19 Create EC2 Instance and Deploy Nodejs Application

[ Step 3: Configure Instance Details ] 

image-2 Create EC2 Instance and Deploy Nodejs Application

The number of instances is 1, now using the default VPC and the default Subnet.   

Click > Add Storage 

[ Step 4: Add storage ] 

In this step, you’ll see that the instance has been automatically provisioned with a general-purpose SSD root volume of 8GB. (The maximum volume size we can give to a General-Purpose volume is 16GB) 

AWS provides 3 types of EBS volumes- Magnetic, general-purpose SSD, and Provisioned IOPS. 

image-3 Create EC2 Instance and Deploy Nodejs Application

Click > Add Tags 

[ Step 5: Add Tags ] 

Name your instance. Adding the final domain is good practice. 

Click > Configure Security Group 

[ Step 6: Configure Security Group ] 

This is an added firewall mechanism provided by AWS. 

Usually, you have one security group. You can create one as per the requirement of the server. 

You can define open ports and IPs 

image-15 Create EC2 Instance and Deploy Nodejs Application

Click > Review and Launch 

[ Step 7: Review and Launch ]  

In this step, we can review our choices and parameters. 

Click > Launch 

After clicking on Launch, you will be asked to create a key pair to log in to your instance. 

The select key if you already have one or create a new one. 

Click > Launch Instances 

image-4 Create EC2 Instance and Deploy Nodejs Application

Our EC2 instance is created successfully, now SSH to Ubuntu EC2 instance and deploys NodeJS application. 
 

Open terminal 

ssh -i “path to Accesskey” ubuntu@”e2 ip” 

image-13 Create EC2 Instance and Deploy Nodejs Application

For the first time, click yes for confirmation 

[ Next Step: ] 
Installing Nginx 

sudo apt install nginx 

image-5 Create EC2 Instance and Deploy Nodejs Application

You can check status using  

sudo systemctl status nginx 

image-21 Create EC2 Instance and Deploy Nodejs Application

Hit the same ip in the browser to check 

image-18 Create EC2 Instance and Deploy Nodejs Application

[ Next Step: ] 
Installing Nodejs 

Now we will install nodejs into the server 

sudo apt install nodejs 

image-6 Create EC2 Instance and Deploy Nodejs Application

Installing Npm 

sudo apt install npm 

image-11 Create EC2 Instance and Deploy Nodejs Application

[ Next Step: ] 
Installing MongoDB 

sudo apt install –y MongoDB  

image-10 Create EC2 Instance and Deploy Nodejs Application
image-20 Create EC2 Instance and Deploy Nodejs Application

Type mongo to check MongoDB is installed successfully 
 

[ Next Step: ] 
Check-in All source code into the server 

After we are done with all prerequisites. 
Now we’ll clone code from the git repo 
Copy HTTPS link of the repository from git/AWS code commit and clone your code 
git clone “HTTPS link here.”   

image-8 Create EC2 Instance and Deploy Nodejs Application

Enter username and password  

[ Next Step: ] 
Installing dependencies 

Go into the code directory and install dependencies 

npm install 

[ Next Step: ] 

Installing PM2 

npm install pm2 -g  

image-7 Create EC2 Instance and Deploy Nodejs Application

Start PM2 

pm2 start server.js 

image-9 Create EC2 Instance and Deploy Nodejs Application

Now we need to use Nginx to serve our application on another port 

sudo nano /etc/nginx/sites-available/default 

image-11 Create EC2 Instance and Deploy Nodejs Application

In location  
Comment try_files  by adding # in front of that line 
Add proxy_pass  http://localhost: port no here”; to next line 

sudo nginx –t to check nginx configuration 

image-12 Create EC2 Instance and Deploy Nodejs Application

Test success will be displayed for a successful configuration. 

Now restart the server once using  

sudo systemctl restart nginx 

image-14 Create EC2 Instance and Deploy Nodejs Application

Every request to root will be redirected now 

That’s it, your server is ready to serve requests for you! 🙂 


For any questions and inquiries related to web development or if you’re looking to hire Node.js developers, we invite you to visit us at Thinkitive.

Abhay Ahire

A seasoned Full Stack Developer specializing in Spring Boot and Angular. With a wealth of experience and a passion for creating high-quality web applications, Expertise shines through in insightful blog posts. Stay up to date with the latest trends and best practices in Full Stack Development by following blog. Gain valuable insights and practical tips to enhance your skills and excel in your own development projects.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button