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

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.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Each step includes a screenshot, which may make the blog appear longer and more image-heavy.</div>
</div>

## 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).
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586267461/3096ee25-9bf3-4723-9468-97555e0816bf.png align="center")
    
3. Click on “Launch Instance”.
    
4. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586447090/6a54641d-ebf3-468f-bcd6-0136952832e6.png align="right")
    
    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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586604546/60892bc8-9be6-4334-a3bf-dac52e22c4c5.png align="center")
    
    Select “Ubuntu” as the operating system.
    
6. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586658638/5fff4329-8ccb-4c2b-a9af-bf00c98e79c5.png align="center")
    
    Choose the instance size based on your requirements. For this demo, I will use the 't2.micro' instance.
    
7. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586684420/b16b6bf3-ee22-4b2c-8f38-5b253639a6c3.png align="center")
    
    Click on “Create new key pair”.
    
8. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586713026/66f4112a-233c-4d8f-abde-caad1ced19cd.png align="center")
    
    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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726587150847/790a46a4-38f6-42a8-a322-6109e02dfa8d.png align="center")
    
    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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586892232/d53c3204-94b6-465f-91c7-1a8c31d450be.png align="center")
    
    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'.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726586938581/ba4bb2ec-0029-4f37-9a03-450f05ae6bbd.png align="center")
    
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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726587012980/58ef9d91-f83f-4d0f-aaa0-0b0926a5d5c7.png align="center")
    

> 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.
    
    ```bash
    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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726596519203/e4798749-383f-4e19-9ddb-360bdd051cf9.png align="center")
    
    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.
    
    ```bash
    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.
    
    ```bash
    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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726598002203/390e6b6f-4f10-4f2e-a5c9-28d2456a479f.png align="center")
    

![](https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExNzFtbWFsOTB5ZjB2d2JiOHN6Z3dscTh1NWdneHR2eGF0OTBpdW1kMCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/slOhiKAVFgwr6/giphy.gif align="center")

**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.
