Deploying a Node.js Application to AWS EC2

beginner
14 min

Deploying a Node.js Application to AWS EC2

Welcome to our comprehensive guide on deploying a Node.js application to AWS EC2! By the end of this tutorial, you'll have a solid understanding of how to deploy your Node.js projects on Amazon's Elastic Compute Cloud (EC2) platform. Let's get started!

What is AWS EC2?

Amazon Web Services (AWS) EC2 is a popular service that allows you to rent virtual servers (instances) to run your applications. It's a scalable, reliable, and cost-effective way to host your web applications.

šŸ’” Pro Tip: EC2 instances can be thought of as a virtual computer within the AWS cloud that you can customize and manage according to your application's needs.

Prerequisites

Before diving into the deployment process, ensure you have the following prerequisites in place:

  1. An AWS account: Sign up for a free AWS account if you don't already have one - AWS Sign Up.
  2. Node.js installed on your local machine: Download and install Node.js from the official website.
  3. A simple Node.js project: Create a basic Node.js project to deploy, such as a simple Express server or a server handling an API.

Setting up your AWS EC2 instance

Now that you have your project ready and the prerequisites in place, let's create and configure an EC2 instance to deploy our Node.js application.

Step 1: Launch an EC2 instance

  1. Navigate to the EC2 dashboard in your AWS Management Console.
  2. Click "Launch Instance" to start the process of creating a new instance.
  3. Select an Amazon Machine Image (AMI) that supports Node.js, such as the "Amazon Linux 2" AMI (HVM), SSD Volume Type - ami-0a9830334252e269b).
  4. Choose your instance type based on the size and requirements of your application.
  5. Configure the instance details, add a key pair (for secure access), and review the summary before launching the instance.

Step 2: Connect to your EC2 instance

Once your instance is running, you'll need to connect to it using SSH (Secure Shell).

  1. Download the key pair you created earlier (.pem file).
  2. Open the Terminal (or Command Prompt on Windows) and use the following command to connect to your instance:
bash
ssh -i path/to/your/key.pem ec2-user@your-instance-public-ip-address

šŸ“ Note: Replace "path/to/your/key.pem" with the actual path to your downloaded key file and "your-instance-public-ip-address" with the public IP address of your EC2 instance.

Step 3: Install Node.js and copy your project

After connecting to your EC2 instance, you can install Node.js and copy your project files.

  1. Update the package manager (yum) and install Node.js and npm (Node Package Manager) using the following commands:
bash
sudo yum update -y curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash . ~/.nvm/nvm.sh nvm install node
  1. Create a directory for your project and copy the project files to the instance:
bash
mkdir my-node-app cd my-node-app scp -i path/to/your/key.pem -R ../path/to/your/project/directory ec2-user@your-instance-public-ip-address:my-node-app

šŸ’” Pro Tip: Replace "path/to/your/key.pem" and "path/to/your/project/directory" with the actual paths to your key file and project directory.

Step 4: Install project dependencies and start the server

Now that your project files are on the EC2 instance, you can install the project dependencies and start the server.

  1. Change to your project directory:
bash
cd my-node-app
  1. Install the project dependencies:
bash
npm install
  1. Start the server:
bash
npm start

Your Node.js application should now be running on the EC2 instance. You can access it by navigating to the instance's public IP address in your web browser.

Quick Quiz
Question 1 of 1

Which command is used to start the Node.js server once the project files are on the EC2 instance?

Configuring Port Forwarding (Optional)

If you'd like to access your application using a specific domain instead of the public IP address, you can configure port forwarding.

  1. Navigate to the EC2 instance in your AWS Management Console.
  2. Click on "Security" in the navigation pane, and then click on "Security groups" under Network & Security.
  3. Select the security group associated with your instance, and click "Edit Inbound Rules".
  4. Add a new rule with the following settings:
    • Type: Custom TCP Rule
    • Protocol: TCP
    • Port Range: 80 (for HTTP) or 443 (for HTTPS)
    • Source: My IP

šŸ“ Note: Replace "My IP" with your own IP address.

After saving the rule, your EC2 instance will be accessible using the domain associated with your security group (e.g., ec2-123-456-789.us-west-2.compute.amazonaws.com).

Conclusion

Congratulations! You've successfully deployed a Node.js application to AWS EC2! Now you can build, test, and deploy your Node.js projects on a scalable and reliable platform. Keep exploring AWS services to take your applications to the next level.

Stay tuned for more tutorials on CodeYourCraft!

šŸŽÆ Key Takeaways:

  • Understand the basics of AWS EC2 and how it can host Node.js applications.
  • Launch an EC2 instance, connect to it, and install Node.js and project dependencies.
  • Copy your project files to the EC2 instance and start the server.
  • Configure port forwarding to access your application using a specific domain (optional).