Node is a fast performing non-blocking I/O JavaScript run-time platform which is mostly used for web servers, API and microservices because of its small footprint, and interpreted nature. Node allows developers to port and share the front-end code to the server easily.

You can compare Apache httpd and PHP stack or Tomcat and Java to Node when doing this simple Hello World deployment.

In this tutorial, we will have Hello World HTTP web server written in Node working and accessible to the entire world via public AWS DNS. Most importantly, we will be automating the creation of the application environment and the start of the application itself. This will allow you to NOT HAVE TO SSH into the instance. You can even disable the SSH access!

Here are the steps:

Create an EC2 Instance

Test website

Test app restart

We begin with the launch of an Amazon Web Services Elastic Compute Cloud instance using AWS web console.

1. Create an EC2 Instance

Log in to the web console and navigate to the EC2 dashboard. Select “Launch instance” to start the wizard. On the first screen of the instance wizard, find in Quick Start an Amazon Linux. We recommend using “Amazon Linux 64-bit, HVM, SSD, EBS” because it’s eligible for free tier on t2.micro.

On the next screen Choose an instance type, select “t2.micro” (Free tier eligible).

Next, open Advanced settings as shown on the screen capture below

Paste the following code for bash command into User Data. For bonus, change the Hello World to Hello {YOUR_NAME}. You’ll need to create your own gist or write the source code in the User Data or load from somewhere else. (The second line is a better log because there’s a delay in web console and cloud-init-output has too much. Below is the description of what the script does.) Becareful when copying from a PDF - there might be unnecessary line breaks.

Add any one of the restarting commands (crontab or any other) code to your User Data script.

The script above does many things. Here they are:

Install nvm (Node version manager)

Configure nvm to run with the “nvm” command

Install Node version 6.7

Create a source code file by using echo command

Install pm2 (process manager)

Launch pm2 with source code and an optimal number of processes (vertical scaling to maximize CPU usage on this instance) - crontab or any other

Configure pm2 to start Node servers on the instance startup (for stop->start or for reboots)

Additionally, developers can also use nvm to install Node (and add the paths to the user profile for Amazon Linux - ec2-user; for Ubuntu, it’s ubuntu). This is advantageous because you can run multiple versions of Node at the same time.

Leave screens (wizard steps) 3 and 4 with the default settings. Add tag named role with value “aws-course” on screen 5. Configure security group to have these ports open:

HTTP 80

HTTPS 443

TCP/IP 3000

SSH 22

2. Test website

Copy the public URL for the newly created EC2 instance from the AWS EC2 dashboard (click on Instances and the on the instance ID). Paste the instance public URL (Public DNS) in the browser and add :3000 to navigate to port 3000. You’ll see the Hello World.

Alternatively, execute curl from your developer machine:

curl PUBLIC_URL:3000

3. Test app restart

Now you need to test if the Node servers will be run on the startup coming from a stop or reboot. Go to web console and stop the instance.

Wait for a moment and see that Hello World is inaccessible from the public URL plus port number.

Start the same instance again and copy the new public URL. Add port 3000 and navigate to the address. Make sure you can see Hello World again.

Go to the web service and verify that you still see the message. That’s because pm2 restarted the app. If you run ps again, then you’ll see a different process ID.

You enabled restart not only on the instance reboot but also on the process failure which could happen due to memory leaks, bugs or attacks.

Wrap up

Node is great for REST APIs and microservices. With AWS EC2 and its amazingly easy to get started web console, it’s straightforward to launch a virtual machine in the cloud and have automation scripts to run a Node server non-stop.

0 comments

ABOUT

This is the official blog of Node University: The Ultimate Resource on Node.js. You can find here the top resources on Node.js, JavaScript, cloud, React and software engineering. For free video previews, view courses at https://node.university/courses.