Deploy NodeJS App to AWS EC2!

Deploy NodeJS App to AWS EC2.

  • Create EC2 Instance
    1. find EC2 find  EC2
    2. Filter “Free Tier Only”, Select “Ubuntu” select EC2 type
    3. Select “t2.micro (free tier)” Filter Free Tier Only
    4. launch instance launch EC2 instance
  • Generate a Key file and download it
    1. generate and download the key Generate PEM key
  • SSH into the EC2 instance with your key file
    1. open terminal macos x terminal cd into PEM key directory
    2. cd into the directory that has the key you downloaded. for example, if you save the key in Desktop:
      cd ~/Desktop
      
    3. Find instructions on how to connect to EC2 via SSH. In terminal, type the command that change the key access mod, and SSH into EC2 find connection info for EC2 SSH into EC2
  • Clone down the app into EC2
    1. git clone down the repo that has your app. For example, I have an app:
      sudo git clone https://github.com/7seven7lst/demo-ec2-nodejs.git
      
    2. alternatively, you can also scp a directory into the ec2 instance (if you are using ubuntu instance):
      scp -i <your_PEM_key> -r <your_project_folder>/* ubuntu@e<ec2_public_dns>:/home/ubuntu
      
  • Install relavent packages needed to run app.
    1. Note, if you are not using Ubuntu, but some other linux server, you may have “yum” as package manager, instead of “apt-get”. In this case, type “sudo yum update”, etc….

    2. sudo apt-get update
      
    3. sudo apt-get upgrade
      
    4. sudo apt-get install nodejs-legacy
      
    5. sudo apt-get install npm
      
  • Deploy App
    1. cd into the app directory
      cd demo-ec2-nodejs # this is the name of my app
      
    2. npm install
      
    3. node index.js
      
  • Expose the port
    1. find security group option find security group option
    2. choose the security group (should have something “wizard”) select security group
    3. Expose app port to outside find inbound config expose app port
  • Navigate to the url
    1. copy the url copy url
    2. In brower addres bar, type <url>:3001 and see the result
  • Navigating to observe dashboard chart and coudwatch
    1. dashboard ec2 dashboard
    2. cloudwatch monitoring cloudwatch monitoring
  • Don’t forget to shutdown the EC2 instance
    1. Stop the instance shutdown EC2
    2. Terminate the instance
Written on September 8, 2018