Railway: Deploy Rails to AWS using Ansible

At the start of the year I decided it was time to move my company's app from Heroku to AWS. Main reason was that our clients were on South America, where Heroku does not have a datacenter. At the time I did not find a guide on how to do this, so I had to spend an entire week (\~40 hours), learning about AWS, Ansible, and hitting my head against a wall. Fast forward a few months, and we have an entire repository of playbooks that can, with a single command, do most of the things that Heroku did for us.

So I decided it might be worth to open source the entire thing, so that if anyone else is trying to do the same thing. I named the project "[Railway](https://github.com/FestaLab/railway)"and here's what you get:

1. Create a custom AMI with everything a Rails app need;
2. Create a development/staging environment with webservers, workers, cache and job redis instances, postgres database and support for up to 9 branches of your app at the same time;
3. Create a production environment with load balanced webservers, workers, cache and job redis instances, postgres database with a read replica;
4. Deploy to development/stating environment;
5. Deploy to production using rolling restarts;
6. Update production servers to get the latest security updates;
7. Upgrade production servers to a new AMI with zero downtime;
8. Compile ImageMagick and Libvips from source in order to get versions more recent, faster, and with more supported formats than the ones available in official repos;

As you can imagine this is not an easy project to use. That’s why I included:

1. An [installation guide](https://github.com/FestaLab/railway/blob/main/docs/INSTALL.MD) to help you get your machine and github/aws accounts ready;
2. A [first run guide](https://github.com/FestaLab/railway/blob/main/docs/FIRST_RUN.MD), to guide you through the process of using the playbooks to bring up the entire infrastructure for the first time;
3. A [customization guide](https://github.com/FestaLab/railway/blob/main/docs/CUSTOMIZATION.MD), to help you make your own app work with this project
4. A [day to day workflow guide](https://github.com/FestaLab/railway/blob/main/docs/DAY_TO_DAY_WORKFLOW.MD), to teach you how to perform common tasks, like deploying and rolling back code, scaling up and down, fixing problems, upgrading servers, backing and restoring databases, etc.

There’s also a [companion app](https://github.com/FestaLab/railway-app) that you can use to deploy and test without having to make changes to your own app. If you also don’t want to install a bunch of stuff in your computer, it’s perfectly fine to install Ansible in a Vagrant box with ubuntu/focal64. That’s what I did when preparing the guides.

I hope this is useful for people who have considered moving from a PaaS to a IaaS before, but didn’t know where to start.

**If you have any ideas on how to improve this project, they would be very welcome.**

[https://github.com/FestaLab/railway](https://github.com/FestaLab/railway)

4 thoughts on “Railway: Deploy Rails to AWS using Ansible”

  1. I really like these initiatives. I was last week fighting to install everything in a standard VPS host using Ubuntu, Puma, Nginx and Certbot for SSL. Considering sharing something similar, but don’t know how to do the Ansible part.

    Also a big lesson was to change to a domain provider with a low TTL (Time-to-Live). The one I had took almost 48h to apply changes, and this is something you don’t want in a production environment (In case you need to change to a bigger server/service in a different host)

    PD: I would like to hear the experiences of people about Ansible vs Chef. I will go into one sooner or later.

    Reply
  2. I’ve always used my own custom playbooks to provision my server and deploy using Capistrano.
    I’ll give this a go as you have a loooooooy more than my basic scripts!
    Good job and thanks for sharing

    Reply

Leave a Comment