Day-12-devops

Deploying Your First Node.js Application on AWS: A Step-by-Step Guide

This blog post provides a comprehensive guide on deploying a Node.js application on AWS EC2, featuring a live demonstration by Kunal Verma. It covers the entire process from setting up the environment to exposing the application to the internet, making it accessible for users.

In this blog post, we will walk through the process of deploying a Node.js application on Amazon Web Services (AWS) Elastic Compute Cloud (EC2). This guide is based on a live demonstration by Kunal Verma, who shares his insights and experiences in deploying applications on AWS. Whether you are a beginner or looking to enhance your skills, this post will provide you with the necessary steps to get your application up and running.

Kunal Verma is a software engineering intern at Devtron and an ambassador for the Cube Simplify Community. He has been involved in the open-source ecosystem for over a year and is passionate about guiding others in their cloud-native journey. Kunal's enthusiasm for sharing knowledge makes him a valuable resource for anyone looking to learn about cloud technologies.

Prerequisites

Before we dive into the deployment process, ensure you have the following:

  • An AWS account (you can create one using your email address).

  • Basic knowledge of Git and Linux commands.

  • Familiarity with Node.js and npm (Node Package Manager).

Step 1: Cloning the Repository

The first step in deploying our application is to clone the GitHub repository that contains the Node.js application. Kunal has prepared a public repository for this session. To clone the repository, follow these steps:

  1. Open your terminal.

  2. Use the command git clone <repository-url> to clone the repository to your local machine.

This command creates a local copy of the repository, allowing you to work with the application files.

Step 2: Setting Up Environment Variables

Once you have cloned the repository, you need to set up environment variables for your application. This is done by creating a hidden file named .env in your project directory. The .env file is used to store sensitive information such as API keys and database credentials.

  1. Create the .env file using the command touch .env.

  2. Open the file in a text editor and add the necessary environment variables. Kunal emphasizes the importance of keeping sensitive information secure by using environment variables.

Step 3: Running the Application Locally

Before deploying the application to AWS, it is essential to test it locally:

  1. Install the required dependencies by running npm install in your terminal.

  2. Start the application using the command npm run start.

  3. Open your browser and navigate to localhost:3000 to verify that the application is running correctly.

Step 4: Creating an EC2 Instance on AWS

Now that we have confirmed that the application works locally, it's time to deploy it on AWS. Follow these steps to create an EC2 instance:

  1. Log in to your AWS account and navigate to the EC2 dashboard.

  2. Click on "Launch Instance" to create a new instance.

  3. Choose an Amazon Machine Image (AMI). For this demonstration, we will select the Ubuntu Server.

  4. Select an instance type (T2 micro is a good choice for beginners).

  5. Configure the instance settings, including creating a new key pair for SSH access.

  6. Launch the instance.

Step 5: Connecting to the EC2 Instance

Once the EC2 instance is running, you need to connect to it using SSH:

  1. Open your terminal and navigate to the directory where your key pair file (.pem) is located.

  2. Change the permissions of the key file using the command chmod 400 <your-key-file>.pem.

  3. Connect to the instance using the command ssh -i <your-key-file>.pem ubuntu@<public-ip-address>.

Step 6: Setting Up the Application on EC2

After successfully connecting to your EC2 instance, you need to set up the application:

  1. Update the package manager by running sudo apt update.

  2. Install Git and Node.js using the commands:

    • sudo apt install git

    • sudo apt install nodejs npm

  3. Clone your application repository again on the EC2 instance using git clone <repository-url>.

  4. Navigate to the application directory and set up the environment variables again in the .env file.

  5. Install the application dependencies with npm install and start the application using npm run start.

Step 7: Exposing the Application to the Internet

At this point, your application is running on the EC2 instance, but it is not yet accessible from the internet. To expose your application, you need to modify the security group settings:

  1. In the EC2 dashboard, select your instance and navigate to the "Security" tab.

  2. Click on "Security Groups" and then edit the inbound rules.

  3. Add a new rule to allow traffic on port 3000 from anywhere (0.0.0.0/0).

  4. Save the changes.

Now, your application should be accessible via the public IP address of your EC2 instance at port 3000.

Conclusion

Congratulations! You have successfully deployed your first Node.js application on AWS EC2. This process involves several steps, but with practice, it becomes easier. Kunal Verma's demonstration highlights the importance of understanding each step and utilizing resources like documentation and community support.

If you have any questions or need further assistance, feel free to reach out in the comments. Happy coding!