How to deploy a simple Node Express API to an AWS EC2 AMAZON Linux 2 instance

JonathanSanchez.Dev
3 min readSep 4, 2024

--

WWYL (what will you learn):
1. Host NODE API on AWS EC2 instance
2. Access NODE API from the web

There are many ways to deploy a Node Express API to AWS, but this is one of the most straight forward ways to set up.

Overview of steps:
1. Create EC2 instance in AWS
2. Connect and remote into EC2 instance via web
3. Once connected Install Node, NVM, GIT on EC2 instance
4. Allow access to your EC2 instance via the port where your API is running

Pre-requirements:
1. Node Express API in a GitHub repository
3. AWS account (if you don’t have an AWS account you could open one here https://aws.amazon.com/free/)

Steps in detail

1. Create EC2 instance in AWS

2. Connect and remote into EC2 instance

Select the EC2 instance and click Connect
Click on the Connect button to remote into the EC2 instance

3. Once connected to the EC2 instance — Install Node with NVM + Git on the EC2 instance


# run system update
$ sudo yum update

# Download and install nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

# Activate nvm on machine
$ . ~/.nvm/nvm.sh

# install Node version
$ nvm install 18

# check if it was installed with
$ node -v

# we should get something like
Creating default alias: default -> 18 (-> v18.14.2)

# Install Git
$ sudo yum install git

# check if it was installed properly
$ git version

Ref: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html

4. Clone your NODE API from the GitHub Repository to your new EC2 instance

a. While connected to Clone your API code from your GitHub onto your Ubuntu EC2 instance using the HTTPS link, not the SSH

# run this while you are connected to your EC2 instance
$ git clone 'your repo link'

b. Install all the dependencies, by going inside the cloned folder and usually by npm install

6. Manually run the NODE API

# run this while you are connected to your EC2 instance
ubuntu@ip-172-31-90-127 $ node 'your cloned folder'/index.js// make sure it runs on the server, just as you run it locally on your machine

7. Allow access to your EC2 instance from anywhere via the port where your API is running

Choose the EC2 instance and Select Security Groups review the Inbound Rules and the PORT 4000 for example

Find the public IP ADDRESS, by selecting the instance

8. Finally use the public IP address with the port

Your API should be running using the EC2 public IP address and the Port where your API is listening to like

http://54.89.183.158:4000/

--

--

JonathanSanchez.Dev
JonathanSanchez.Dev

Written by JonathanSanchez.Dev

Developer by day, rockin' drummer by night. Loves dogs🐶 and collaborating with fellow engineers. Passionate, creative, and always up for a coding adventure! 🚀