Deploying a Node.js App to AWS EC2

Deploying a Node.js App to AWS EC2

ยท

2 min read

In this blog post, we'll walk through the process of deploying a Node.js application to an AWS EC2 instance. We'll cover everything from setting up an IAM user to configuring security groups to ensure your application is accessible.

Prerequisites:

  • An AWS account

  • Basic understanding of the command line

  • Node.js and npm installed on your local machine

  • A Node.js application ready for deployment

Step 1: Create an IAM User

  1. Log into the AWS Management Console.

  2. Navigate to the IAM service.

  3. Create a new user with the necessary permissions to manage EC2 instances and security groups.

  4. Download the user's access key ID and secret access key.

Step 2: Launch an EC2 Instance

  1. Log into the AWS Management Console using your IAM user credentials.

  2. Navigate to the EC2 service.

  3. Launch a new instance, selecting a t2.micro instance type with an Ubuntu AMI.

  4. Configure the security group to allow inbound traffic on port 3000 (or the port your application uses).

  5. Create a key pair and download the private key.

Step 3: Connect to the EC2 Instance

  1. Use an SSH client to connect to your EC2 instance using the public IP address and private key.

Step 4: Install Node.js and npm

sudo apt update

sudo apt isntall nodejs

sudo apt install npm

#check if git is installed

git --version

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

npm install

Step 6: Set Up Environment Variables

  1. Create a .env file to store sensitive information like API keys , the setting up the app are listed here

Step 7: Configure Security Group

  1. In the AWS Management Console, navigate to the EC2 service.

  2. Select your instance's security group.

  3. Add an inbound rule to allow traffic on port 3000 from anywhere: Type: HTTP Protocol: TCP Port Range: 3000 Source: 0.0.0.0/0

Step 8: Access Your Application

  1. Use the public IP address of your EC2 instance to access your application in a web browser
http://your_ec2_public_ip:3000

I successfully deployed my Node.js app to AWS EC2! ๐ŸŽ‰

I learned how to set up an IAM user ๐Ÿ‘ค to securely access my instance, launched a t2.micro instance ๐Ÿ–ฅ๏ธ, and SSH'd into it ๐Ÿ”’. I installed Node.js and npm ๐Ÿ› ๏ธ, configured environment variables ๐Ÿ”, and opened up the security group firewall firewall to allow incoming traffic.

Finally, I started my app with npm run start ๐Ÿš€ and accessed it through the public IP address ๐ŸŒ. This project boosted my skills in AWS, Linux, Node.js, and networking ๐Ÿง !

~Thank you

Happy learning

ย