Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

In this article, we are going to discuss how to create an Amazon Linux AMI EC2 instance and then how to deploy the node js application on Linux EC2 instance easily and also install the node modules directly on the EC2 instance.
 
Pre-requisites
  • AWS Account root/IAM user
  • NodeJS application to be hosted

What is Amazon EC2 Instance

 
EC2 stands for Elastic compute cloud (Amazon EC2) and is one of the longest-running services on AWS. EC2 is just a virtual machine provisioned with a certain amount of CPU cores, memory, storage space, and networking. With this, anyone can install any type of operating system (OS) on it whether it's Linux or Windows.

The OS that is installed on EC2 instance is also called an Amazon Machine Image or AMI. So in this article, we are going to create an Amazon Linux AMI to deploy our Nodejs Application.

Steps to create EC2 Instance
  • Choose AMI
  • Choose Instance Type
  • Configure Instance
  • Add Storage
  • Add Tags
  • Configure Security Group
  • Review 
So let's create EC2 instance and first login to the AWS management console. Here I am going to login from my IAM user account, you can create a new AWS account or login from the root account as per your convenience. You simply go to https://aws.amazon.com/console/ and navigate to sign in to the console. A page opens where you need to enter your credentials.

Step 1
 
login to AWS account

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 2
 
After signing in you will see the below screen and search for EC2 in services

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 3
 
Here you see an EC2 dashboard and click on Launch Instance 

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 4
 
Here a wizard opens to create an EC2 Instance wherein the first step is you need to select the AMI and here we are selecting the first one, Amazon Linux 2 AMI, which is also eligible for the free tier.

Click here for more About free tier

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 5
 
Choose instance type, here you need to choose the install family which is varying combinations of CPU memory, storage and networking capacity which gives you the flexibility to choose the mixed resources for your application. Here I am going with a free tier family that is t2 micro which consists of 1 CPU, 1 GB of RAM, and then clicking on next.
 
Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 6
 
Choose Instance Details where I should put everything by default, but if you want to change you can change the VPC, subnet, and other options

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 7
 
Next is add storage which I'm not going to add and continue with the default one which is 8 GB for Linux OS and 30 GB for Windows machines.

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 8
 
Next step is to add tags. Tags consist of case sensitive key-value pair, you can define tags as many as you can here we add a tag with key=name and value=Nodejs

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
  
Step 9
 
The next step is to configure security groups where we define a set of firewall rules that control the traffic for your instance. Also you can add rules to allow specific traffic to reach your instance and here I'm adding one more rule other than default one to allow the access to port 3000.

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 10
 
Now next you need to review your instance launch details. Also you can go back to edit changes to any of the sections and  if all looks good you can click on Launch to assign a key pair to your instance remember. This key pair is not recoverable so you must keep this key pair in a safe place.

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Download this key pair and launch instances.

Step 11
 
After you can check your instance, make your instance run and stop and check the instance state with other details. Here you need to check if Public IP is not assigned then you need to create an Elastic IP for this instance and after that, you need to assign the instance to the created Elastic IP so that we can connect this instance with the SSH client.
 
Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Now Our EC2 Instance is running and needs to connect the EC2 instance via SSH.

Step 12
 
Now select your instance and click on connect under actions. The below screen is opened and needs to move to tab SSH Client 

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 13
 
Now copy the Example command and open the command prompt and move to the path where your Key-Pair file is  (Keypair file extension is .pem). Now run the command. This will connect to the EC2 instance.

-- ssh -i <pem-file> ec2-user@<ec2-ip>

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 14
 
Now we have access to the instance, we need to update any packages that are out of date. Run the below command and press Y if it prompts for installation.

-- sudo yum update

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Step 15
 
Next step is to install node on our instance so that we can run the demo application. We need to update the repositories in the YUM package manager in the EC2 instance prompt. Enter the below command.

-- curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
 
Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 16
 
Now install nodejs to the EC2 instance with the following command.

-- sudeo yum install -y nodejs

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 17
 
Once installation is complete you should have Node installed on EC2 instance and you can check by running the commands to check the node and npm versions.

-- node -v
-- npm -v

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application
 
Now transfer the Demo application code to EC2 instance.

Step 18
 
Now exit from the instance by running the command EXIT this will make you log out from the current SSH client and this should return you to the system prompt. Before we copy our demo code to the EC2 instance let's make sure to delete the local node module folder. This includes all node dependencies. To remove local node module folder run below command

-- rmdir /s node_modules

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 19
 
Once you have deleted that folder enter the command to copy the demo code to the EC2 instance.

-- scp -r -i <pem-file> <local-code> ec2-user @<ec2-ip> :/home/ec2-user/demoApp
-- scp -r -i ~/Downloads/NodeJs.pem ./Downloads/demoapp ec2-user@13.127.169.44:/home/ec2-user/demoapp

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 20
 
Once it's complete log to the instance again with ssh command and run the command to check the directory where we copy the demoapp code.

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 21
 
Next step is to install node dependencies into the folder so run the command:

-- npm install

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 22
 
When it finishes we can start our application 

-- npm start

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

Step 23
 
Now the output says the app is running at 10.x.x.x IP but this is the internal IP, open up a browser window, and enter elastic IP with ':' and then the port 3000 hits go and you should see the demoapp is running on the Amazon EC2 instance. 

-- <ec2-elastic-ip>:3000

Create Amazon Linux AMI EC2 Instance And Deploy NodeJS Application

That's it. You see how easy it is to deploy and host nodejs application on EC2 instance. Also we loaded all the dependencies on the instance itself and I would love to answer your queries in the comment section. I hope you like this article.

Stay safe and learn more!