Practical guide: Deploy and scale node server — Auto Scaling Group, Code Deploy

Ravali Peddi
4 min readOct 30, 2022

--

The following guide will help you to:
1. Deploy Node.js server on EC2 instance on AWS cloud
2. Setup auto-scaling group and application load balancer to allow the application to scale
3. Automate deployment of code changes to all the servers using CodeDeploy service

Step 1: Setup EC2 instance

Steps to launch an EC2 instance:

  • Choose Ubuntu 18.04 LTS (recommended)
Choose Ubuntu 18.04 LTS AMI
  • Create new security group that allows traffic on — SSH port 22, Node server port, let’s say 8080 for example
Create new Security Group and setup inbound rules
  • Create and attach an IAM role which allows access to specific S3 buckets
Create IAM role and attach the policy
  • Add the following script under user data field. The script installs Code Deploy agent on all the EC2 instances since user-data is run at launch
Bash script to use for user-data

After all the required steps above are done, launch the EC2 instance. Once the EC2 instance is running, you should be able to SSH into it from your host machine using the key-pair

$ ssh -i <path-to-key.pem> ubuntu@<public ip address of EC2>
  • Setup github access on EC2

On the EC2 instance generate new SSH key. Copy and paste the new SSH key to your git account(settings) in order to be able to use git on the new EC2 instance

Github settings page to add new SSH key
  • Clone the node.js project repository in the EC2 instance
  • Install Node and NVM
  • Copy .env file, if it exists in your project and run the server
$ nano .env
$ yarn run start
  • Check if sever is accessible on the public-ip of the EC2 instance on port 8080

If you have gotten this far, Congratulations 🎉 you now have a nodejs application accessible through the internet. That’s definitely something to celebrate. But you know we are not stopping here. Let’s get going ⏭

  • Let’s install PM2 . Our process manager for making the nodejs server production ready 💪
$ npm install -g pm2

Now let’s make sure our application can run through pm2. In order to do that pm2 service should be made a systemdprocess so that it can automatically start-up if out instances are re-booted/restarted for any reason

$ cd node-server
$ pm2 start npm -- start
$ pm2 startup
$ sudo env PATH=$PATH:/home/ubuntu/.nvm/versions/node/v18.8.0/bin /home/ubuntu/.nvm/versions/node/v18.8.0/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu
  • After we make sure that the nodejs server is running, it’s time to create an image off of this instance under(Actions -> Images & templates -> create image)and name it something like node-server-staging . If it’s successfully created, you should see the image under AMIs in EC2 dashboard

Step 2: Create Auto-Scaling Group

  • Create a launch configuration with the AMI that we just created above
  • Assign S3 read access IAM role so that the newly spinned up EC2 instances by the ASG can be setup with Code Deploy agent
  • Create Security Group with inbound rules on port 8080 and port 80
  • Create new application load balancer and attach SG with inbound traffic allowed on port 80
  • Create new Target Group with routing config that has access to HTTP traffic on port 8080
  • Setup health checks on /status

Step 3: Setup Code Deploy

  • Setup Code Deploy application with Code Deploy policy IAM role and integrate it with ASG
  • Create a Deployment Group that contains both AutoScalingGroup and EC2 staging instance node-server-staging
  • Give Code Deploy access to your git repository
  • Add appspec.yml to the root of the project repository and specify mandatory fields , source , destination

Example appspec.yml file: https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-example.html

  • And finally, you can create a new deployment by specifying <git-user>/<repo-name> and commit SHA
Code Deploy: Deployment lifecycle events

It might take several minutes for the deployment to finish and you can check the status of all the events for the deployment. Once it’s finished successfully, you should see the new changes deployed to all the targets in your Auto-Scaling Group.
You can choose to automate deployment by configuring the code deploy to trigger a deployment for example when something is merged into main / stable branches. For this project, I still want it to be manual where I go to Code Deploy dashboard and trigger new deployment by copying the commit SHA from git.

Hope this guide is helpful for your projects 🚀

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ravali Peddi
Ravali Peddi

No responses yet

Write a response