Skip to main content

Command Palette

Search for a command to run...

Backend Deployment: A Guide to Using AWS EC2 for Node.js Projects

Published
4 min read
Backend Deployment: A Guide to Using AWS EC2 for Node.js Projects
G

Full-stack developer with expertise in both front-end and back-end development. Proven ability to design, develop, and deploy robust web applications from conception to completion.

Deploying the backend is a crucial part of an application to expose the APIs and Sockets onto the internet.

There are many platforms available for deploying applications, such as AWS, Azure, GCP, and Vultr. Each platform comes with its own set of advantages, disadvantages, and learning curve. For this blog post, I'll focus on AWS.

In particular, I'll be demonstrating how to deploy a Node.js project using AWS EC2, which is a type of Virtual Machine (VM) provided by AWS. The project is hosted on GitHub, and if you’d like to follow along with the deployment process, please proceed with the steps below. Otherwise, feel free to explore the blog post for general insights on backend deployment.

💡
Each step includes a screenshot, which may make the blog appear longer and more image-heavy.

Deployment Process

The deployment process is divided into 2 parts

  1. Server setup

  2. Code Deployment

Server Setup

  1. Login to your AWS account.

  2. Search for EC2 in the search box and select EC2 (Elastic Cloud Compute).

  3. Click on “Launch Instance”.

  4. Choose a name for your instance. It can be anything, but make sure it's something you can easily identify if you have multiple instances.

  5. Select “Ubuntu” as the operating system.

  6. Choose the instance size based on your requirements. For this demo, I will use the 't2.micro' instance.

  7. Click on “Create new key pair”.

  8. Create a key pair with a name of your choice. Clicking 'Create Key Pair' will generate a key value pair and download it to your device. Ensure you store the private key securely, as it will be required to access the instance you're about to create.

  9. Allow SHH, HTTPS, and HTTP traffic, from anywhere(0.0.0.0/0). Clicking on Launch instance will create an Instance. Keep the IP address of the server handy.

  10. With these steps, we've successfully created the server. Before deploying, we'll need to configure additional settings to access our API after deployment.

  11. If you can't find the highlighted launch wizard, simply click on the instance you just created and then select 'Launch Wizard'.

  12. To allow traffic to access our application on port 3000 from any location, modify the inbound rules by adding the highlighted rule and then click 'Add Rule'.

    Why port 3000?
    In this example, the application is configured to listen for incoming requests on port 3000, which is commonly used for web applications, particularly in development environments.

We've successfully completed the server setup! Now, let’s dive into deploying the code.

Code Deployment

  1. Open your terminal and navigate to the directory where your <file name>.pem file (key) is stored. Then, use the following commands to SSH into the server we created.

     chmod 700 <file name>.pem # change the permissions
     ssh -i <file name>.pem ubuntu@<ip of the server> # replace with your server IP
    

    The SSH session might look something like this, though it varies depending on your OS.

  2. The application we are using as an example is written in Node.js. To run this application, Node.js must be installed on the server. Use the following commands to install Node.js.

     curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash #install nvm
     source ~/.bashrc #reread bashrc
     nvm install --lts #install lts version of node js
     node --version #check node version
    
  3. Clone the example application repository. To run the application, install the necessary packages and start it using the following commands.

     git clone https://github.com/giripatel/deployment-backend.git # clone repo
     cd deployment-backend
     npm install # install packages
     node index.js
    
  4. Use the server’s IP address and Port 3000, as the URL in your browser’s search bar or in Postman to access the API.

What more can you do?

  • Add an SSL certificate to secure the site and remove the "Not secure" warning.

  • Configure DNS for the server's IP address to use a custom domain.

  • Set up a reverse proxy to eliminate the port number from the URL.