How to deploy SailsJs app via Amazon’s OpsWorks

Week ago CTO of company where I am working at the moment, asked me do I know what is OpsWorks and what it does. I was like, nope, never heard of that, sounds like something that has to do with DevOps and work, in some sense šŸ™‚ We started chatting and talking about it. Hours later I did checked it out briefly and it seemed to be just what i was looking for. Easy atomic deployment with full power of load-balancing and creation of EC2 instances. Perfect!! As many of us who worked for at least 2 different companies, small or big, doesn’t matter, know that deployment is ONE thing that varies greatly from company to company.

I think there is more deploying tools for any kind of code than frameworks itself, which also means that people have different mindset for deploying. Some like Capistrano, some like Mina others like other deployment tools that are written in a language they know and/or they are comfortable with. These deployment tools wary greatly in performance and in language they are written in.

As we are talking about deployment, my personal opinion is that deployment process should be as simple as possible, somewhat centralised and well defined. Most of us are using version control software like Git or Subversion/Mercurial.Ā So question is why use other software that will do it’s magic with your code when you have it already online and accessible? As this is not a post about Deployment Management, i will stop here and get to the point.

Let’s start!!

First of all there will be minor tweaks to SailsJs app before we can deploy it properly.

Step 1, preparing SailsJs app:

  • Rename your app.js to server.js
  • open your package.json file and change all occurrences of app.js to server.js, that will not mess up sails lift and everything should work as it was before
  • openĀ  config/env/production.js and enable port: 80,
  • save

Step 2, preparing OpsWorks environment:

  • Check out Getting started With OpsWorks, that should cover quite a lot of what you should do. One note on repositories, use PRIVATE key, not public, it will not work. I have mine hosted on bitbucket as private repo, so i created deployment key-pair just for opsworks deployment. If you are stuck somewhere in the middle of the process, or it doesn’t work, please let me know via comments and i will try to answer or evenĀ writeĀ more detailed post about it.
  • Make sure to set Env variable NODE_ENV = production for the env settings; that tells sails to run production.js config

When you see that your deployment is done, go to instances and select one that is tied to your app, there should be public IP address, click on it and enjoy your deployed sails app.

I’ve created github repo of clean SailsJs app SailsJs deployable template, feel free to use it and/or modify it.

Useful links

OpsWorks doc on nodejs apps
SailsJs