Deploy a NodeJS app on AWS EC2
Tags: devops, aws, node, ec2
Published: 2020-09-07 12:51:13

Cloud computing is reshaping the way we build, maintain, collaborate, and deploy modern software programs. Cloud providers are regularly launching new services to cater to the needs of businesses and individuals who operate and provide internet-based solutions.

In this article, we will deploy a NodeJS app on AWS Elastic Compute Cloud (Amazon EC2) which can be reached on a public IP address. NodeJS is a javascript runtime built on Chrome’s V8 JavaScript engine. In this article, you would have:
* Launched an EC2 instance on AWS free tier
* SSH into the instance securely
* Installed Nodejs on the instance
* Deployed your app on the instance
* And used PM2 to keep your Node app running after the terminal has been closed.

### Amazon Elastic Compute Cloud (EC2)

Amazon launched Amazon Web Services(AWS) in 2006 with EC2 as the first public cloud service offering server instances on-demand by leveraging the power of virtualisation. Amazon logically divided the servers in their own data centre and added a software layer over it to create smaller logical servers which they called instances. An EC2 instance on AWS is now offered to the public as virtual machines on the web with configurable size, memory and networking which is available on-demand and billed per seconds. Erstwhile, any business or individual who needs a server has to purchase a physical server and maintain it on-premise, even if they only needed it for a few days in a month or even a year. Applications could also be deployed without having to host hardware or worry about scaling up or down as the need require. In this article, you’ll launch your own Amazon EC2 instance for free.
Traditional architecture has one hardware, one OS, and one application while virtual architecture has one hardware, many OS.

### Launch an EC2 instance

AWS offers a free tier for some services including t2.micro and t3.micro (dependent on region) machine types with Linux or Windows. Create an account on AWS you can run this instance free for 750 hours every month for a year.

After you have created an account navigate to the EC2 instances page and click the Launch Instances button. Follow these steps to configure your instance:
1. Select an Amazon machine image(AMI). An AMI is a template that contains the software configuration (operating system, application server, and applications) required to launch your instance. Scroll down and select **Ubuntu Server 18.04 LTS**
2. Choose an instance type. Select **T2 Micro**
3. Click on **Next: Configure Instance Details**, accept the defaults
4. Click on **Next: Add Storage**, accept the defaults too
5. Click on **Next: Add Tags**, add any tags that will help you identify your instance in key/value pairs.
6. Click on **Next: Configure Security Groups**. There is a default rule there that allows you to SSH into the instance on port 22. Add another rule that allows all IP addresses to access the instance over the internet on port 80. This rule will be a Custom TCP rule, TCP protocol on port 80, **source** should be set to Anywhere or 0.0.0.0/0
7. You will get a pop-up warning, ignore it and click **Review and Launch**
8. The review screen will show all your configurations, check again and click **Launch**
9. You will be prompted to create a key-pair. A key pair consists of a public key that AWS stores, and a private key that you store. Together, they allow you to connect to your instance securely. For our instance here, we will need the private key file to SSH into our instance securely. In the drop-down menu select **Create new key pair**. Give the private key any name, I will name mine KP_node1.
10. Click on **Download Key Pair**, note where the file is downloaded and do not delete it. Without this file, you cannot access your instance.
11. Click on **Launch** and your instance will launch after some minutes
12. Click on **View instances** to see your instance in the EC2 instance page.

You now have a running EC2 instance congratulations!

### Install NodeJS and Deploy your Application

The next line of action is to SSH into your instance so you can deploy your application. In the AWS console EC2 instance dashboard, click on the blue checkbox before your instance and then click on the **Actions** drop-down menu in the top right area of the page. Select on **Connect**, this will show you instructions on how to SSH into your instance in the **SSH Client** pane. Mac and Linux users can just run the command:

`ssh -i KP_node1.pem ubuntu@55.180.16.47`

This command must be run from the terminal directory where the private key is located. If you used a different name for your private key, you must use the file name you downloaded. The IP address is the public IPv4 address of your instance which you can copy from the instance dashboard.

Windows users can use [PuTTY](www.putty.org) to convert the private key file from `pem` extension to `ppk` then connect to the instance. I prefer to use [Git Bash](https://www.atlassian.com/git/tutorials/git-bash) instead. Git bash gives you a bash terminal in windows, this will allow you SSH into your instance using the command above.

Once you are in your instance, update the packages by running the command:

`sudo apt-get update`

Then install the latest version of NodeJS by running the following commands:

`curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs`

Confirm NodeJS is install by checking the version of NodeJs installed with this command:

`node -v`

I installed v14.9.0.
Next install git with the following command:

`sudo apt-get install git`

[Git](http://git-scm.com/) is a free and open-source distributed version control system. We will clone a git repository containing a simple node app from Github, modify it and deploy. Git might already be installed on the instance.
Confirm git has been installed with this command:

`git --help`

Clone the Node app files by running:

`git clone https://github.com/duoarc/node-app.git`

Navigate to the directory with:
`cd node-app`

Then install Node dependencies which will create the node-modules folder by running:

`npm install`

Your application can now be deployed live when you run:

`node index.js`

If you visit your instance public IPv4 address (example: 55.180.16.47) or public IPv4 DNS (example: ec2–52–86–220–88.compute-1.amazonaws.com) you will get the message
Welcome to Abdulrahman’s first Node app! Keep things Jiggy ;-)
You can edit the index.js file to modify the message and replace Abdulrahman with your name.

### Keep App running using PM2

The app is running as soon as you open the terminal and it will terminate as you will close the terminal. We will install PM2 (Production manager 2) to keep live our app after closing our terminal or disconnect from the remote server. Run the following command:

`sudo npm install pm2 -g`

PM2 will be installed on the server globally. Run your app using PM2:

`sudo pm2 start index.js`

### Conclusion
Congratulations, you have now successfully deployed a NodeJS application on Amazon EC2 and kept it running after you close the terminal.