Pages

What I learned from deploying my Node application in AWS cloud

Well something needs to be done in the time of Corona (NCovid 19) virus. The schools are shutdown, and the work offices are shutting down as well. I has some free time and so I started getting restless as I always do.

Long story short, I has an idea for a simple application which I started building and then ended up creating the basic functionality over a 3 day weekend. The feature was pretty straight forward. It is a live story pointing application where a bunch of people would join and point stories based on their complexity. And I deployed it on an Aws application over my own sub-domain. Now other questions starts coming.

  1. How to make it more usable?
  2. How to add additional features?
  3. How to make is secure?
  4. What if the server restarts?
  5. How to make it scale if all the people in the whole world start using it?
  6. How much am I going to be billed?
  7. How to make it Https?

1. How to make it more usable?

Well there some work required to make an application work, but there's a lot and lot more work required to make an application more usable. I was able to develop a working application in a few hours(4-5 hrs). But to make it simple usable took the other two days (10-15 hrs). There's just a lot of scenarios that could occur in the real world. And I was able to think about a few of them. Rapid testing and fixing the bugs that could occur was the way to go. Then there was work to be done about the looks and feel of the application. I have never been a proper UI-designer so this is always a struggle for me. However, I was able to complete it using some of my previous designs. 

2. How to add additional features?

This was one of the important question in my mind. The first MVP product I have developed was using simple nodejs server and html and javascript. There's a lot of work that you need to do in the plain languages and if this project has to go anywhere, I need to use some kind of frameworks. There are frameworks for everything nowadays. Now I have to decide the backend framework, and then the css framework, and the frontend application framework etc. Then there is the effort required to port the application to the framework. Its a decent amount of work and there's a significant learning curve.
I ended up using express for the backend and bootstrap for css and React for the frontend .

3. How to make it secure?

Security was not a huge deal for this application. It does not have any sensitive information nor it deals with any kind of financial transaction. So it was not a big deal. But there is always a thinking about what if someone able to hack my system and application. What if someone is able to get into the ec2 instance, or shut down the application etc. Well, I did what I could to make it secure.

4. What if the server restarts?

Everybody should think about this point if they are deploying their application in the cloud. In the cloud, based on the resource contention, the server can restart any time. If that happens, you have to make sure, your application re-starts and is automatically available on the web. Previously I was using a linux tool called screen to run the application in a background shell. This would not have worked.
I found pm2 instead, which runs on background and can starts when the server restarts. I still need to figure out how to see the logs though.
# to keep the process running even after a server restart
npm i -g pm2
pm2 start --name "app-name" npm -- start
pm2 startup # to make sure pm2 starts when the server starts
pm2 save
pm2 ls
# to stop and delete a running instance
pm2 stop app-name
pm2 delete app-name

 5. How to make it scale if all the people in the world start using it?

This question runs on the back of my brain all the time. What would happen if a lot of people start using it. Right now I am running my node application on an ec2 micro instance with 1 GB or ram and 8 GB of memory. I choose Node because it has very less memory footprint and can serve huge number of requests rapidly. It's not good for long running tasks which this application is not. I do store some information in the server but clean it regularly and have gone about this strictly couple of iterations over my code. So I am pretty confident that my micro instance can handle significant amount of load. Also, I can start utilizing the 8 GB disk space that I have instead of relying on the memory which will make it slightly slow but I think overall it will help handle some more data. One thing I am not sure is how much of the parallel request (TCP connection) can the ec2 instance handle at a time. I believe that could be a hard limit and I have to go to other options of Auto scaling and so on. But let's see if I ever reach there.

6. How much am I going to be billed for this?

This is always one of a major concern. But I started feeling this way. If I am always worried about the bill, I will never do anything cool with my life. Beside, I cannot be billed like thousands of dollar for this application. I am still eligible for free tier and if the resources consumption starts increasing because of load, there will always come some way to pay for it. So I am calm about this. However I am ready to pay like a hundred dollars a month or so.

7. How to make it HTTPS?

Now a days, https is must for any web application. Even google will not rank you properly if you don't have https for your site. Because of this, I had to make my application use a certificate. There were other problems also while running the application on ec2. It was not allowing my application to use PORT 80 or 443 which are the default ports for http and https. So I had to use a Nginx server to route my http and https traffic to my local port on which the application was running on ec2.
# install nginx
sudo apt-get install nginx
# edit the config
sudo vi /etc/nginx/nginx.conf
# auto restart nginx on system restart
sudo systemctl enable nginx
-- to start nginx 
sudo systemctl start nginx.service
sudo systemctl stop nginx.service

## route traffic in port 80 to local 8080
sudo vim /etc/nginx/nginx.conf
server {
  listen 80;
  server_name myservername;
  if ($http_x_forwarded_proto = 'http'){
       return 301 https://$host$request_uri;
  }
  # you can add this in the sites-available/default file as well
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:3000;
  }
}
# restart again
sudo systemctl restart nginx.service

Now if the security group in your EC2 has just 80 port open, will still work.
Now researched and found that I could use aws certificate manager and configure certificate on Elastic load balancer and point the load balancer to this ec2 instance. Then I can point my domain to the elastic load balancer. Now my application is more safe, secure and load balanced as well.