Deploying a Rails 5 App using Elastic Beanstalk and PostgreSQL

Update 24/11/17

I’ve recently published a post on deploying a Rails 5 API app with React on AWS Elastic Beanstalk, click here to read!

In the past I’ve done post involving AWS and about how to deploy your website using AWS and Jekyll. Seeing as they just had their annual Summit in London (which was great by the way), I figured I’d do a post on how to deploy a simple Rails app, that uses postgres, on Elastic Beanstalk.

What is Elastic Beanstalk?

EB or Elastic Beanstalk, allows users to quickly deploy and manage their applications without having to worry about configuring the infrastructure that runs those applications. It takes care of load balancing, health monitoring, capacity provision and scaling, which allows you to focus on your actual application a little more. At the moment, it supports applications developed in Java, PHP, .NET, Python, Node.js and Ruby. The great thing with EB is that it allows you to manage different environments under a single application. For example, you could test new features or plug-ins, live, on staging, without effecting your production site, which is pretty useful. If you’re used to deploying on Heroku, Elastic Beanstalk is kind of the same thing. Refer to the AWS free tier guide, if you’re unsure about potential charges.

Getting Started

It probably goes without saying, but make sure you have an AWS account. Next, assuming you have brew installed, make sure to update it and then download the CLI for EB.

To ensure it’s all correctly installed, just run the version command and you should get something resembling the above.

I’m going to assume you’ve got your rails app ready to go, in the sense that it works locally and you’re looking to just deploy it. With this in mind, in your app directory you’re going to want to initialise EB.

After running the above command, you’ll be presented with a set of questions regarding the app, the first of which about which zone you’d like to set up in.

As I’m in London, I’d obviously choose 15, but choose which ever zone is closest to where you are.

It will then ask you which application to use, you’d select a new application, I’ve got two options as I’ve deployed something already. Next you can change the name of your app, but it’ll choose the app’s root by default. The Beanstalk will even check what your app’s written in and make a presumption, but it’ll still confirm it with you first. Next you’re going to want to choose a ruby platform that matches your ruby version used for your app.

The last option allows you to set up SSH for your instances if you want to remotely connect and configure stuff, it’s not long to setup and will just generate a keypair for you to use, which is then automatically uploaded to your generated instance on the EB console. If you want to keep things simple for now, just hit no.

Setting up the Enviornment

Now if you log into your AWS console and head over to Elastic Beanstalk, you’ll see your created app just sitting there. It’s all ready to go, we just need to actually upload our code.

First thing to do is to create an environment.

Next EB will do some magic and create everything you need for your environment, this includes an S3 bucket to store environment data, a security group, a load balancer, an auto-scaling group and CloudWatch alarm notifications.

It doesn’t take too long but it gets everything nicely setup and ready to go. To check it’s all working, you can view the status, like below.

Last thing to do for the Rails App, is to generate a new SECRET_KEY_BASE environment variable to use, which is pretty simple to do.

Next, you’re going to want to update EB’s environment variables, it’s important to note that, similar to Heroku, you’re going to have to set all your environment variables again, as your now running on a totally different machine.

This will update the environment with the new environment variable. The process is the same for any other env vars you may have.

Setting up PostgreSQL with Rails on your Instance

So your app probably still won’t run at this point, as we haven’t actually setup a database for it. That’s alright, we’re about to do it now.

Head over to your app’s dashboard on the Elastic Beanstalk console and hit the “Configuration” Tab.

Then at the bottom of the page you’ll see the following.

Hit the “create a new RDS database” link and follow the instructions.

Choose a secure username and password, then hit apply. This will create the database for you.

We also need to tell our instance to use postgres, and in order to do this we use a yum package called postgresql93-devel.

You can create custom configurations for EB environments, all you need to do is create a .ebextensions folder in the root, and then name the file anything you want with the suffix .config. The file should also be formatted like a .yml file. In this case, your db.config file will look like this.

Now commit and push this update to your repo and then re-deploy the latest version of your app.

Once this is done, you can open up your web app with the following command.

Everything should now be setup and working!

If you have any questions, or need any help, please do leave a comment or shoot me a message and I’d be happy to help!

Thanks for reading, hit 💚 if you like what you read and be sure to follow to keep up to date with my future posts.




Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Datetime module in Python

Things you should consider before migrating to Azure Synapse Analytics

How to Create an Effective Project Schedule

some ways to handle errors in ruby

___How Forex Trendy Works The Forex Trendy incorporates many scientifically sophisticated…

Laravel 6.0 — Best New Features And Upgrades

Why QA Isn’t What You Think It Is

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Hamann

James Hamann

Software Developer

More from Medium

Creating Rails Api Using Existing MySql Database

Drop production database in Rails

How to Add JWT Auth API to Existing Rails Application

Server with wires

Dockerize a Ruby on Rails Rest API