AWS EC2 Deployment: Guide to Hosting a Node.js Application

AWS EC2 Deployment with Node.js application using Terraform:
In this blog, we’ll walk through the process of launching and deploying a Node.js and Express.js app on an AWS EC2 instance running Ubuntu. By the end of this guide, you’ll have a fully functional web server running your Node.js and Express.js app that’s accessible from any browser.
These are the following steps will guide you through the process of creating EC2 instance, Security group, Inbound traffic, Outbound Traffic, and ubuntu server.
Prerequisites:
Before starting the deployment process, make sure you have the following prerequisites:

Terraform is Installed on your local machine. You can download it from the official Terraform website. Download Terraform.
An account with the chosen cloud provider (AWS here).
Appropriate access and permissions to create the required resources.
Node.js application ready for deployment
I highly recommend you go through the attached docs as they can be really helpful.

Note: Terraform is declarative that is you describe the desired state of your infrastructure in a Terraform configuration file, specifying the resources, their properties, and the relationships between them. You define the desired end result rather than writing step-by-step instructions or imperative commands to achieve that result.
Note: Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript outside of a web browser. It is built on Google Chrome’s V8 JavaScript engine and is widely used for developing fast, scalable, and real-time applications.
Lets’ deep dive:

🪜Deployment Steps:
1. Set Up Terraform and Configure AWS Provider
Make sure to install Terraform on your device then install the Terraform extension from VS Code.

Create a folder and give a name of your choice.
Create main.tf file. Terraform file is created using
.tfextension.Configure AWS provider with Terraform.
To get your Access key and Secret key follow this article - AWS Account and Access Keys
// Configure the AWS Provider
provider "aws" {
region = "" # Add Region
access_key = "" # Add access_key
secret_key = "" # Add secret_key
}
Docs - AWS Provider
2.Launching an EC2 Instance
Open up the terminal in VS Code or you can also use cmd to make sure that you are in your correct directory.
Here are some basic CLI commands of Terraform: -
Docs - Terraform Basic CLI Commands
- Run
terraform init
You would see somethings similar to this -

Running the previous command terraform will automatically create the required files.
2. Now run terraform plan
It is used to preview the changes that terraform will make to your infrastructure before applying them - it’s dry run.

As you can see there are no running instances in AWS EC2 instances.

Let us now run
terraform apply -autu-approve


3.Connecting to the EC2 Instance
Once the Instance is running:
Open a terminal and run:
ssh -i instance.pem ubunutu@<IP_ADDRESS>Update Packages and Dependencies: Start by updating the package list to ensure everything is up-to-date:
sudo apt update
Install Git: Install Git on the EC2 instance
sudo apt install git
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Docs - Install Git
Install Node.js and npm
sudo apt updatesudo apt install nodejssudo apt install npmDocs - Node.js and npm
5.Deploying the Node.js Application
Now that the EC2 instance is configured, you can deploy your Node.js application.
Clone the Project on the EC2 Instance:
git clone https://github.com/amitkumar-Github8/Deploying-an-Web-Application-on-AWS-EC2.git cd Deploying-an-Web-Application-on-AWS-EC2
Set Up Environment Variables: Create the .env file again on the EC2 instance with the following content:
DOMAIN= "http://localhost:3000"
PORT=3000
STATIC_DIR="./public"
Dotenvs, or .env files, are files used to store “environmental variables”, which are variables needed to configure our program environment.
Install Dependencies and Start the Project: Run the following commands to install dependencies and start the project
npm install
npm start

Hurray!!!
You can also verify this by using Public IPv4 address, it must show a result similar to this.
🏁The End
Yippie!!! You’ve successfully deployed a Node.js application on an AWS EC2 instance. From setting up the EC2 instance to installing the necessary software and running your app, this guide has covered all the essential steps. You can now access your Node.js app through your EC2’s public IP.
Don’t forget to try it by yourself!
Git Repo -
Star and fork the repository.
clone the repository.
Read the steps from Instructions.md
https://github.com/amitkumar-Github8/Deploying-an-Web-Application-on-AWS-EC2.git