Episode 1: Deploy and Expose a node JS app to AWS
In this episode, we will learn how to deploy a Node.js app onto AWS EC2. This will help you understand the basics of deploying applications without using advanced concepts. For any project, there are key steps to follow:
Gather the application requirements:
Since this is a basic web app without a backend database, we only need access to the source code and the AWS console to create resources.
Create resources:
Sign up or log in to AWS and access the management console to create an AWS EC2 instance. It is also a best practice to create an IAM role that can be used to log in to the EC2 instance.
-
Log into the AWS console using that specific user instead of the root user.
Create an EC2 instance.
Select an OS image - Ubuntu.
Create a new key pair and download the
.pem
file.Connect to the EC2 instance using an SSH client from your terminal with the SSH key pair.
ssh -i "awsVM-keypair.pem" ubuntu@publicipaddress-here.compute-1.amazonaws.com
Test the code by deploying it locally:
- Let's get the source code onto the local machine for testing. Use
git clone
to download the code to your local machine if it is not available there.
git clone https://github.com/DevSecOps-man/Project_4_Deploy_nodeJs_app_on_AWS-EC2.git
- Use the node package manager to install the necessary dependencies.
$ npm install
If the dependencies are not updated in the Node.js files, they might show vulnerabilities.
Let's run
npm audit
to check for issues.
npm audit fix
This will fix some of the minor vulnerabilities.
npm run start
- Access the app on
localhost:3000
Deploy to AWS EC2
Since we already tested it locally, do the same thing on the EC2 instance: clone, deploy, and access.
Ensure that port 3000 is open to access the app after deploying.
Connect to EC2 using SSH - This depends on the local machine you are using.
You might need to generate an SSH key pair using the
ssh-keygen
command.
Check if Git is available on EC2
git --version
.If Git is not available, install it using
sudo apt install git
.Now, clone the repository.
git clone
https://github.com/DevSecOps-man/Project_4_Deploy_nodeJs_app_on_AWS-EC2.git
$ sudo apt install nodejs $ sudo apt install npm $ sudo npm start > simple-nodejs-app@1.0.0 start > node index.js node:internal/modules/cjs/loader:1137 throw err; ^ Error: Cannot find module 'express' Require stack: $ sudo npm install express --save $ sudo npm start > simple-nodejs-app@1.0.0 start > node index.js Listening at port 3000...
If we notice, there were no issues when tested locally, but the EC2 machine doesn't have the required dependencies or there are issues with the package manager instance. This is why the Docker concept has become popular. In the future, we will use Docker to package all requirements into containers and deploy that Docker container. Please add a security rule for the EC2 instance to ensure the port is exposed to the internet. Launch the app at ec2-public-IP:3000
Monitor the deployment
- Access the application in the browser at
ec2-public-ip:3000
.
In conclusion, we successfully deployed a simple Node.js app on an AWS EC2 instance. We covered the essential steps, from creating necessary AWS resources to testing and deploying the app. While this was a straightforward deployment, it's a foundational skill that will help us understand more advanced topics like Docker, Jenkins, and Kubernetes in future episodes. Stay tuned for more in-depth tutorials on automating CI/CD pipelines and containerizing applications.
Subscribe to my newsletter
Read articles from Phaneesh Yalavarthi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by