Deploying a Basic Express API on Amazon EC2 | by Akshay Kumar | May, 2022

A step-by-step information

Picture from Unsplash

A number of articles I’ve deliberate sooner or later require an specific API working over an EC2 occasion, so I need to present a one-stop answer my readers can check with that may assist them deploy a completely practical specific API on an AWS EC2 occasion.

You need to have an lively AWS account.

Observe: Initially, AWS offered you with credit to check out its providers, however providers like EC2, route53, and many others., price now. Please be aware of that earlier than utilizing any of them and continually maintain observe of your prices by way of AWS’s cost explorer.

Step one is to initialize an EC2 occasion on AWS. For that, head over to https://aws.amazon.com/ec2/.

Click on on “Get began with Amazon EC2.” In case you have an AWS account, log in, and also you’ll be redirected to the next web page:

Click on on the “Launch Occasion” button as proven beneath:

On the subsequent web page, select the working system, “Ubuntu,” and supply a reputation to your server.

Then choose an occasion sort. I’ve chosen t2 micro, which offers us with 1 GB of reminiscence and 1 CPU core.

Subsequent, you’ll want to offer an SSH key pair in order that the occasion can authenticate your sshrequest. Right here’s a picture:

In case you have already got an sshkey pair instantiated, you should use that, or click on on “Create a brand new key pair.”

Present a reputation for the important thing, and click on create. A zipped file will probably be downloaded in your system containing the important thing.

Ensure you extract and save the file regionally; it will likely be used to connect with our distant occasion at a later stage.

Then within the community settings, permit site visitors by way of SSH (used to get distant entry to your server), HTTP, and HTTPS (used to entry our API).

Depart the remainder of the settings because the default settings, and click on the “Launch Occasion” button.

After launch, you’ll see the next display screen:

Right here, click on on “view all cases.” From right here, you’ll be redirected to a display screen the place you’ll be able to see all cases.

On this display screen, copy the general public ipv4 handle of the occasion. We’ll want this handle together with the saved .pem file within the subsequent part.

To arrange our occasion, we’ll want to connect with it first. For this, we’ll use this ssh protocol.

For that, open the terminal of our working system, transfer to the listing which incorporates our saved .pem file (which we generated within the earlier part), and run the next command:

ssh -i aws-test-key.pem ubuntu@54.234.113.208

Observe that you simply’ll have to switch the above command as per your context.

ssh -i <PEM_FILE_NAME> OPERATING_SYSTEM@SERVER_IPV4_ADDRESS

You’ll see an analogous terminal after logging in.

Now, we’ll have to put in NodeJS and some different dependencies to run our specific server on the occasion.

To put in NodeJS, you should use any of the curl instructions (relying on which model you need to set up) on the official GitHub page.

For our case, I’ll use the next:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get set up -y nodejs

We’ll see the next output post-install.

You may confirm whether or not Node was put in efficiently by working this command:

node --version

Subsequent, we’ll set up a package deal supervisor often known as pm2 to handle our NodeJs processes in order that they carry on working even after we shut the present terminal session.

To put in pm2, run the next command:

sudo npm set up -g pm2

As soon as once more, to confirm set up run this:

pm2 --version

You may initialize your personal specific server within the occasion, or you’ll be able to clone the next repository utilizing this command:

git clone https://github.com/AkshayCHD/node-basic.git

Primarily this undertaking incorporates a index.js file initializing the next specific server:

After that, transfer into the listing and run the next command:

cd node-basic && npm set up

Now, to spawn a pm2 course of, run this command:

pm2 begin index.js

After working this, you’ll be able to confirm if the method has began accurately by working a brief command:

pm2 logs

You need to see the next output.

Now, if we need to expose our API to the surface world, we’ll have to begin our course of on port 80, which is the default HTTP port, or we’ll should map our course of (working on port 5000) to port 80 someway.

The proper approach to obtain that is by means of a reverse proxy like Nginx, which we’ll be utilizing within the subsequent article. For now, we’ll be utilizing a not so perfect approach of mapping port 80 to 5000 in our system’s IPtable utilizing the next command:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 5000

Now, once you sort the IP of your occasion within the browser, you’ll see the next message:

message: server up and running

Which means our server is working nice.

By following the above steps, you’ll be able to instantiate a naked metallic Ec2 occasion, which can run a easy specific API uncovered to the surface world.

Within the subsequent article on this collection, we’ll be studying the way to arrange a reverse proxy for our API in order to make it extra environment friendly, safe, and extensible.

More Posts