This Is How I Deploy My First NestJS Application into AWS EC2

Image for post
Image for post

Since the past few months, I’ve been into deploying small things up into the cloud. I want to level up my game and gain some experience besides from developing websites.

I was able to deploy some of my personal sites with Google Firebase, Serverless apps using Zeit Now and a little from AWS Lambda. It was actually fun to see your apps get into the cloud!

I am not that really good at it, I just follow some materials on the internet but if you want some help and also a newbie into this. Hit me up and let’s chat. I would love to! 👋 ❤️

Enough with the background and let’s deploy our first NestJS app into AWS EC2. Let’s head on to our journey!

What is AWS EC2?

AWS EC2 is a service provided by Amazon to spin up applications using their virtual machines that you can connect in to.

If you are a developer who wants to manage machine configurations, securities or networks, you might like to play with this service.

Let’s Get Our Hands Dirty

To get started, you’ll need to login to your AWS Management Console.

When you are in the AWS Management Console, you will see the list of services available.

Select EC2 under Compute services.

Image for post
Image for post

After clicking the service, you will be redirected to the EC2 Dashboard page, and you’ll see a button with a label of “Launch Instance”. That button will be the one responsible creating our instance, our virtual machine.

Image for post
Image for post

Then the next page, we will be now selecting our virtual machines. I am actually not an expert on this but what I did was decide with my intuition 😅

Since I have a fair knowledge on the command lines of a Linux machine and I feel that this virtual machine is correct for my needs, I did select this item.

Image for post
Image for post

Since our goal is to deploy a NestJS application, I think this beast is already capable on deploying it based from the description and for my ultimate super free tier account.

Moving on to the next steps, I really don’t know what are those and I just click the next button and stop on step 6 which is “Configure Security Group”.

Image for post
Image for post

On this screen, I did include the application’s name (my-nest-application) to the Security group name and Description.

The first row on the table is of type SSH, I did select on the Source is anywhere.

I know that choosing anywhere should not be allowed because it means you can do SSH and have access to your virtual machine anywhere and even on an unsecured network. Beware of the consequences.

Moving to the second row is the a custom TCP with a port of 3000 because our NestJS application uses port 3000. I selected anywhere as my Source again because I want my application to be accessible anywhere. This is intentional to me unlike the SSH source.

We are now closer to the finish line! Let’s start launching this virtual machine!

Here is our final configuration:

Image for post
Image for post

After clicking the Launch button, we will be prompted by an alert to select or create a new key pair that we can use when accessing our virtual machine via SSH.

Image for post
Image for post

Enter any Key pair name of your choice and click the Download Key Pair. It will download a pem file and you need to save it securely! Away from the thieves!

Even if we select the anywhere from the SSH source awhile ago, we can’t still access it without this key when we do SSH.

Let’s launch the instance! 🚀

If we go back to our AWS Management Console, then click on Instances on the left panel. We can now see our instance is now running. Yahoo!

Image for post
Image for post

Now that our instance is up and running. Let’s have a short break from AWS Management Console and setup how we can access it via SSH. Are you ready? Let’s go!

Open up your terminal and check if you have a SSH installed by typing `ssh`. If that is not yet installed for you, install it first.

MacOS users already have pre-installed SSH client. Not sure about other operating systems.

I told you we will just have a short break from AWS Management Console but the time is over! We need to get back and grab something really important for us to connect to our instance.

On the AWS Management Console, copy the Public DNS as this will be our way to connect to our instance.

Image for post
Image for post

Then back to our terminal, we will do this to connect:

Image for post
Image for post
ssh -v -i /Users/jsantos/Downloads/my-nest-application.pem ec2-user@ec2–18–188–20–76.us-east-2.compute.amazonaws.com
  • -v is a flag to show logs when running the command (not required)
  • -i is a flag to specify where the pem key is located (required)

The SSH URL would be something like this:

ec2-user@[Public DNS]
  • ec2-user@ is the username for some AWS Linux machines (Amazon Linux 2 or the Amazon Linux AMI). We are using Amazon Linux AMI.

Once we execute the command, we will receive a Permission 0644 error saying:

Permissions 0644 for '/Users/jsantos/Downloads/my-nest-application.pem' are too open.It is required that your private key files are NOT accessible by others.This private key will be ignored.Load key "/Users/jsantos/Downloads/my-nest-application.pem": bad permissionsdebug1: No more authentication methods to try.ec2-user@ec2-18-188-20-76.us-east-2.compute.amazonaws.com: Permission denied (publickey).

After minutes of looking for the answer, it says from this StackOverflow link that we can easily fix it by doing a chmod command to change permissions. To do that, we need to type the following command:

chmod 400 /Users/jsantos/Downloads/my-nest-application.pem

This will set the permission to be readable to the current logged in user.

After changing the permission, we can now re-execute the previous command to the terminal.

Image for post
Image for post

We can now see that we are connected! Awesome 🎉

Reference: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Now that we are connected and since this does not come with a Node.js. We will need to install it.

We can install Node.js by using a Node Version Manager (NVM) by typing the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

After the installation is done, let’s activate NVM by typing the following command:

. ~/.nvm/nvm.sh

The dot (.) at the beginning is required as it is a shortcut to register something to the terminal. It is a shorthand to source ~/.nvm/nvm.sh as well.

After registering, we can now install Node with any different version of our choice. You can see more here. For now, we will install the latest version available by typing the following the command:

nvm install node

Verify that Node.js is installed by typing the following the command:

node -v 

You should see something like this:

Image for post
Image for post

Now that we have setup our Node.js. Our instance doesn’t contain a Git yet. We can install it quickly by typing following the commands:

# Perform a quick update on your instance:sudo yum update -y# Install git in your EC2 instancesudo yum install git -y

Reference: https://cloudaffaire.com/how-to-install-git-in-aws-ec2-instance/

We can now start cloning our repository.

The repository that we will use is the Typescript Starter provided by the NestJS team. Let’s start cloning the repository by typing the following the command:

git clone https://github.com/nestjs/typescript-starter

After cloning the repository, let’s go inside the folder and install the dependencies by typing the following the command:

cd typescript-starter && npm install

Once the installation of dependencies is done, let’s build our application by typing the following command:

npm run build

After building it, let’s run it on production mode!

npm run start:prod
Image for post
Image for post

Now that our application is finally running, we can now test it on the browser and see if we can access it!

But what is the URL?

We need to get back to our AWS Management Console then go to the Instances section then click our instance on the table. By doing that, we will see the instance details, our virtual machine details and we need to copy the IPv4 Public IP.

Image for post
Image for post

So for this instance, it can be access using the URL

http://18.188.20.76:3000

After entering that on the browser, we should be able to see the most glorious message we needed.

Image for post
Image for post

Congratulations! We have now deployed our NestJS application into the AWS EC2 🎉

Written by

Improvise, Adapt, Overcome

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store