Making your node.js app live using AWS

In this post, I will show how to make a node.js app live using AWS. For this, I will first show how to create a simple hello world app using node.js.
Lets get started:

  1. Create your project folder ( mkdir sampleNodeApp )
  2. Move to that folder ( cd sampleNodeApp )
  3. Create a file server.js
  4. Create another subFolder inside your project folder public
  5. Move to public ( cd public )
  6. Create a file index.html
  7. Move to your main directory ( cd .. )
  8. Update server.js to have the following content
var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/public/index.html');
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

     9. Update index.html inside public folder to have following content
         <html>
           <head>
       <title>
         This is a sample app.
        </title>
            </head>
            <body>
         Welcome Everybody!
            </body>
          </html>

     10. Move to the main folder and do sudo npm install express 
     11. After express is installed do node server.js

      After the last command you will see a message on your terminal:
        Example app listening on port 3000!
     Now go to your localhost:3000 and you will see index.html will load successfully with message
     Welcome Everybody!
   
Create a github/bitbuck repository and push your files in that repository as this will be very useful when we will be uploading our app on aws ec2 instance.
Here is the source code of above node.js app you can clone/download it.

Now we need to create a AWS account for this follow the following steps:
  1. Go to https://aws.amazon.com/ and click on Create AWS account button




2. Enter your email and select I am a new user 
  3. Fill in the details and click create account 

4. Fill next details and click create account and continue

  5. Enter you card details and proceed next. (They will never charge without your consent)
  6. Follow the steps for identity verification
  7. Select Basic plan and continue

  8. After your registration is successful click on log in to console button


  9. Enter your email you used to register and select I am a returning user and then enter your password that you have created in step 3.
  10. In the search box on console screen main page search EC2


  11. Click on EC2 - virtual servers in cloud option form the drop down
  12. On the EC2 dashboard click on key pairs 


  13. Click on import key

  14. Give a name of your key and upload your public key

  15. After importing your key pair now click on EC2 Dashboard from the left nav bar
  16. Click on running instances

  17. Click on Launch Instance 

  18. Select ubuntu server 16.04

  19. Select the default t2.micro and click Review and Launch

  20. Click on edit security groups under security groups

  21. Click on Add Rule and select Custom TCP rule from the type drop-down,  select protocol TCP, edit port range to have 3000 (this should be the port you are exposing your app) and select  anywhere  in source drop-down.

  22. Click on Review and Launch and then press Launch

23. Select the key-pair that you have created in step 12 and click Launch Instance

 24. EC2 instance should be launched now navigate to your EC2 dashboard and click Running Instances (this may take couple of minutes for the machine to be running)

25. You can see your machine running now copy its IPv4 Public Ip 

  26. Open your terminal and run ssh ubuntu@[IPv4 public address] for example ssh ubuntu@34.213.123.88

  27. Type yes and you will be inside your EC2 machine

  28. As we will be running our node app in this machine so lets install node first by sudo apt install  nodejs-legacy

  29. Now clone your nodejs web app from the github/bitbucket and move to that folder
  30. Inside that folder do node server.js this should start the app

  31. Go open your web browser and go to [your IPv4 public address]:3000 for example 34.213.123.88:3000



So simply by following these steps, you can easily make your web app live on AWS. If you have any queries just type it in the comment box.






Comments

  1. Nice one ! instead of manual creation of EC2 instance, you can use terraform to create infrastructure.

    ReplyDelete
    Replies
    1. Yes Suman we can do this but I wanted to keep this post simple and for beginners so I choose to launch instances manually. But thanks a lot for the suggestion I might be writing about this in my future posts.

      Delete

Post a Comment

Popular posts from this blog

All about NIT Agartala

How to integrate a payment gateway into your WordPress website for donations without using woocommerce

Implementing a Self Organizing List in Go