From GitHub to AWS: Deploy Your First Node.js App on EC2

From GitHub to AWS: Deploy Your First Node.js App on EC2

Deploying a web application on AWS EC2 might sound overwhelming, but it’s a skill every developer should have in their toolkit. In this blog, I’ll show you how I successfully deployed a Node.js application on EC2 using AWS’s free-tier services and a GitHub project. With this step-by-step guide, even beginners can follow along and achieve their first deployment.


Step 1: Set Up Your AWS Account and IAM User

Why IAM Matters

IAM (Identity and Access Management) allows you to securely manage access to AWS resources. For this guide, we’ll create a user with administrator access for simplicity, but it’s recommended to follow the principle of least privilege in production setups.

Steps:

  1. Create an AWS Account: If you don’t already have one, sign up at aws.amazon.com.

  2. Navigate to the IAM Console: After logging in, go to the IAM console.

  3. Add a New User:

    • Click on Users in the left sidebar and select Add user.

    • Assign the user administrator access (for this tutorial only).

  4. Save Access Keys:

    • AWS will provide you with a password and access key. Save these securely as they’ll be needed later.

Note: For production environments, use specific permissions instead of full administrator access.


Step 2: Launch an EC2 Instance

Why EC2?

AWS Elastic Compute Cloud (EC2) provides resizable compute capacity, making it ideal for hosting web applications.

Steps:

  1. Navigate to the EC2 Console:

    • From the AWS Management Console, go to EC2.
  2. Launch an Instance:

    • Click Launch Instance and choose the Ubuntu Amazon Machine Image (AMI).
  3. Select an Instance Type:

    • Choose t2.micro, which is free-tier eligible and sufficient for small applications.
  4. Create a Key Pair:

    • Download the .pem file and store it securely. This file is essential for accessing your EC2 instance via SSH.
  5. Configure and Launch:

    • Use the default configurations or modify them as needed. Finally, click Launch Instance.

Pro Tip: Choose a region close to your target audience for reduced latency.


Step 3: Access Your EC2 Instance

Steps:

  1. Set Permissions for the .pem File: Run the following command to ensure your key file has the correct permissions:

     chmod 400 your-key.pem
    
  2. SSH Into the Instance: Use the public IP of your EC2 instance to SSH in:

     ssh -i your-key.pem ubuntu@<ec2-public-ip>
    

Troubleshooting Tip: If you encounter SSH errors, ensure your security group allows inbound traffic on port 22.


Step 4: Clone the GitHub Repository

Why Git?

Git allows you to download and manage code repositories. For this tutorial, we’ll use the AWS-Session project as an example.

Steps:

  1. Install Git:

     sudo apt-get update
     sudo apt-get install git
    
  2. Clone the Repository:

     git clone https://github.com/verma-kunal/AWS-Session.git
     cd AWS-Session
    

    The repository contains a Node.js application that we’ll deploy.


Step 5: Install Dependencies

Steps:

  1. Install Node.js and npm: If not already installed, run:

     sudo apt-get install nodejs
     sudo apt-get install npm
    

    Verify the installation:

     node -v
     npm -v
    
  2. Install Project Dependencies: Navigate to the project folder and run:

     npm install
    

Step 6: Run the Application

Steps:

  1. Start the Application: Run the following command:

     npm run start
    

    Ensure the correct entry point (e.g., server.js) is specified in your package.json file.

  2. Access the Application: Open your browser and go to the public IP of your EC2 instance, followed by the port number (e.g., http://<ec2-public-ip>:3000).

Security Note: Update your security group to allow inbound traffic on the application’s port (e.g., port 3000).


Special Thanks

A big thank you to Kunal Verma for the AWS-Session repository. The clear instructions and well-structured project made this deployment seamless for beginners.


Conclusion

Deploying a Node.js application on AWS EC2 is a fantastic way to dive into cloud computing. By following this guide, you’ve gained hands-on experience with setting up an EC2 instance, configuring it, and running a web application.

Feel free to share your deployment experiences or ask questions in the comments. Happy coding! 🚀


Additional Resources