Skip to content

Instantly share code, notes, and snippets.

@Savinda96
Last active August 12, 2021 03:10
Show Gist options
  • Save Savinda96/3bd0907cd548584bed910d5197fa9cdd to your computer and use it in GitHub Desktop.
Save Savinda96/3bd0907cd548584bed910d5197fa9cdd to your computer and use it in GitHub Desktop.
Setting up simple node application in EC2

Deploy NodeJS Application into EC2 with GitHub actions

This is an experience shared on getting a simple node application deployed into EC2 with github actions.

Create a simple node application

  1. Go the project directory and type the following commands mkdir node-hello && cd node-hello
  2. Type npm init in your terminal to initiate a node project (Make sure you have node installed if not follow the guide here)
  3. Create the index.js file (touch index.js)
  4. Copy the following code for a simple node application
const express = require('express');
const app = express();
const port = 3001;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log('Example app listening at http://localhost:${port}');
});
  1. Run npm install
  2. Run the app (node index), connect to localhost:3001 and you will see "Hello World!"
  3. Push your application into git

Set up an EC2 instance (Console mode)

  1. Visit AWS management console

  2. Under services go to EC2

  3. Go to Launch Instace

  4. Choose an Amazon Machine Image. Make sure to select an image that is compatible with Github self hosted runners.

  5. Choose instace type : Go for t2.micro which is available in free tier

  6. Keep configure instance as default. Go to next.

  7. Add storage keep as default. Go to next.

  8. Add tag keep it as default

  9. Under configure security groups add the following rules

    • HTTP : Source - Anywhere
    • HTTPS : Source - Anywhere
    • SSH : Source - Anywhere
    • Custom TCP rule : Port - Port where your node application runs : Source - Anywhere

Custom TCP is used to access your node application from browser

  1. Create a key pair and save it locally
  2. Launch the instace

Setting up Node application in EC2 instace

  1. SSH into your EC2 instance using downloaded key
  2. Install node into instance. Follow the guide here
  3. Install git into instance sudo apt-get install git
  4. Install daemon process manager as PM2 (npm install pm2 -g) to run the node application forever. Read more
  5. Clone your repo and move into directory
  6. Install required node modules (npm install)
  7. Run the application using PM2 sudo pm2 start app.js
  8. Exit the SSH, get the Public IPv4 DNS add the port you are running node application on to end in our case it is 3001. Access the address through browser. Note that since we haven't SSL encryption we have to http to access the URL. Eg : http://<Public IPv4 DNS>:3001. Another alternative is to use reverse proxy. Read more

Setting up self hosted runner

  1. Comprehensive guide on how to install self runner into EC2 instace is available here
  2. Once you install if command ./run.sh doesn't initiate your self hosted runner run sh run.sh. This will however run git runner in foreground which will cause the runner to go off line once you close the terminal. There for run with nohup sh my_script.sh &. Type jobs to see active jobs running on background

Creating a workflow

  1. Create a folder called .github/workflow inside your local node application directory
  2. Create workflow file with workflow folder (touch main.yml)
  3. Example workflow which uses git ui or user triggered event to trigger workflow and is given bellow.
# This is a basic workflow to help you get started with Actions

name: Test Deployment

# Controls when the workflow will run
on:
  repository_dispatch:
    types: [start_deploy]

  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  node-deploy:
    # The type of runner that the job will run on
    runs-on: self-hosted

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - run: cd /home/ubuntu/hello-world-node/ && git pull origin && npm install && pm2 restart index.js
  1. Make sure to use indentations correctly or it will fail
  2. Read about actions from here. If you want to learn more regarding the syntex read here
  3. Push the changes into remote repo
  4. Once you visit actions you can select your workflow under workflows and run workflow

Notes

Once we have done the configurations we can remove the public SSH port since the deployment will be handled by github runner itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment