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
Log into the AWS Management Console.
Navigate to the IAM service.
Create a new user with the necessary permissions to manage EC2 instances and security groups.
Download the user's access key ID and secret access key.
Step 2: Launch an EC2 Instance
Log into the AWS Management Console using your IAM user credentials.
Navigate to the EC2 service.
Launch a new instance, selecting a t2.micro instance type with an Ubuntu AMI.
Configure the security group to allow inbound traffic on port 3000 (or the port your application uses).
Create a key pair and download the private key.
Step 3: Connect to the EC2 Instance
- 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
- Create a .env file to store sensitive information like API keys , the setting up the app are listed here
Step 7: Configure Security Group
In the AWS Management Console, navigate to the EC2 service.
Select your instance's security group.
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
- 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