How to Deploy a Ruby on Rails Application to Elastic Beanstalk

Who This Tutorial Is For

This tutorial is for developers who are comfortable with Ruby on Rails but have no particular expertise in system administration.

If you’ve never used AWS before and you don’t even know what Elastic Beanstalk is, that’s okay.

What We’re Going To Do

We’re going to get a Ruby on Rails application running on a staging environment on AWS Elastic Beanstalk. The Rails app will be connected to a PostgreSQL database instance.

Here are the steps we’re going to carry out:

  1. Create a Rails App to Deploy
  2. Create an Application on Elastic Beanstalk
  3. Install AWS CLI and EB CLI
  4. Create an Environment on Elastic Beanstalk
  5. Set Up an RDS Database
  6. Observe Our Working App


All you need in order to be able to follow this tutorial is to have an AWS account. We’re not going to perform any operations locally so it shouldn’t matter if your computer is Mac, Windows or Linux.

Create a Rails App to Deploy

We’re going to create a very simple Rails app to deploy. If this is your first time deploying to Elastic Beanstalk, I wouldn’t recommend trying to deploy an existing app. The reason is that if you try to deploy an existing app and something goes wrong, there’s so much stuff you’d have to dig through to try to find the problem. If you deploy a freshly-created app and it doesn’t work, there are a much smaller number of things that could be the culprit.

Let’s create the Rails app now.

I personally always use PostgreSQL so that’s why we have the -d postgresql flag. I also always use RSpec instead of Test::Unit so I always initialize my Rails apps with -T to exclude Test::Unit. This tutorial would probably work just fine for you without those flags, though.

Now that we’ve created our Rails app, let’s set it aside for a moment and go get Elastic Beanstalk ready.

Create an Application on Elastic Beanstalk

I assume you’re familiar with the concepts of a development, staging and production environment. Let me explain how those things relate to Elastic Beanstalk.

In Elastic Beanstalk, you can have any number of applications and each one of those applications can contain any number of environments.

So if you want to deploy a particular Rails application, it would make sense to create a single Elastic Beanstalk application which contains within it a production environment and a staging environment.

In our case, we might have an Elastic Beanstalk application called hello-world and inside it a hello-world-production-env environment and a hello-world-staging-env environment. (AWS seems to like environment names to be suffixed with -env and it seems to like kebab case for names of most stuff.

In this tutorial, we’ll only create the hello-world-staging-env environment. The steps for creating a production environment would be the exact same.

First, go to the Services menu and click Elastic Beanstalk. You’ll then want to click the Create New Application link in the upper right-hand corner.

When prompted for an application name, choose hello-world. (This is arbitrary and could be anything you want.)

You should see a screen that looks like this. You could create an environment through the GUI right now if you wanted to, but in my experience that path leads to problems. Instead we’ll create our environment using a certain command-line tool.

Install AWS CLI and EB CLI

Now we’re going to install the AWS CLI (Amazon Web Services Command-Line Interface) and the EB CLI (Elastic Beanstalk Command-Line Interface).

These CLI tools will allow us to do the same stuff the AWS console lets us do (by “AWS console”, I mean the AWS website we’ve been using so far) except more quickly and efficiently.

We’re actually going to take another step before we install AWS CLI and EB CLI. When you set up these tools, you’re going to have to hook them up to your particular AWS account. Part of this setup process involves providing something called your AWS Access Key ID and something called your AWS Secret Access Key.

If those terms sound intimidating, don’t worry. For now you can think of those things as kind of a username and password that let you connect to a particular AWS account.

You might wonder where you find your AWS access keys. That’s what we’ll walk through in the next steps.

First, click on your name in the upper right-hand corner and click My Security Credentials.

You’ll get a message about AWS wanting you to get set up with IAM. This is probably a good idea for a real production account, but discussing this part is probably outside the scope of this tutorial, so for now just say Continue to Security Credentials.

Next you’ll want to click on the thing that says Access keys (access key ID and secret access key).

Then click Create New Access Key.

Lastly, click Show Access Key and store the values somewhere where you’ll be able to get at them in a minute when we need to use them.

Now we’ll install the AWS CLI (CLI = command line interface) using Homebrew.

Next we have to configure AWS by running aws configure. This is where you’ll be prompted for the access keys I showed you how to get earlier. Important: make sure you’re inside you’re Rails project directory.

Now we’ll install the Elastic Beanstalk-specific CLI, again using Homebrew.

The first step with the Elastic Beanstalk CLI is the init step. Let’s run eb init.

Create an Environment on Elastic Beanstalk

You’ll be asked about a region. I’m picking us-east-2.

For the application, select the one you just created. Select Ruby as the platform.

Select Ruby 2.3 (Puma) as the platform version. Why Puma? I just picked Puma since according to its documentation, it’s the default server for Rails.

This will create a file called .elasticbeanstalk/config.yml which will look something like the following:

Now we’ll create the Elastic Beanstalk environment.

If we visit the URL for our new Elastic Beanstalk environment, we’ll see that it doesn’t work.

If you want to, you can check the logs by going to Logs, then clicking Request Logs, then clicking Last 100 Lines. You’ll probably see something like this:

2017/09/25 11:45:57 [crit] 3023#0: *64427 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client:, server: _, request: "HEAD /phpMyAdmin/ HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/phpMyAdmin/", host: ""

You might notice a clue there. See the phpMyAdmin stuff? It’s trying to do MySQL stuff but we wanted to use PostgreSQL. We need to set up and configure a PostgreSQL database.

Set Up an RDS Database

Go to the Configuration area and scroll to the bottom. You’ll want to click create a new RDS database. (RDS stands for Relational Database Service.)

For DB engine, choose postgres. I assigned mine a Master username of hwstaging. (AWS seems to be weird about certain names in certain ways. For some reason it seems to want your database name to be really short. I don’t really like that but whatever.) Put whatever you want for the password.

The RDS database will probably take forever to spin up. If you’re looking for ways to kill time while you’re waiting, I might suggest preparing a full Thanksgiving dinner from scratch or watching every episode of Roseanne.

In addition to creating the RDS database instance, there’s another housekeeping item we have to carry out that we might as well get out of the way now. We have to set up the Rails secret keys.

Go to the Configuration area again and click the gear icon next to Software Configuration.

There’s a value you’ll need to grab and paste somewhere. Run the following command:

This will output a token. Copy that token to the clipboard. Under Environment Properties, create a SECRET_KEY_BASE property and paste in the token for the value. Click Apply.

I’ve found that at this point our application probably still won’t work. For some reason, a redeployment is necessary.

If you thought that after all this work things were finally going to work now, you’re in for a disappointment. If we pull up our environment’s URL, we’ll probably see this screen:

We haven’t created any scaffolds yet so there’s nothing to actually be seen. Let’s create a scaffold so our app has something to show us. We’ll create a Person scaffold.

Let’s make people#index the root route.

Let’s commit what we did and redeploy.

Yay! We should finally be able to see a working app.

Observe Our Working App

Just to be sure, let’s use the Person form to add a person.

Now we see our person show up on the list.

Note: It’s unlikely that I wrote this whole post without making a mistake or accidentally leaving something out. If my instructions don’t work for you, please do leave me a comment and let me know what kind of problem you’re having. I want to make sure this tutorial really works.

6 thoughts on “How to Deploy a Ruby on Rails Application to Elastic Beanstalk

  1. Tony

    Great tutorial,
    Thanks for putting this together.
    The docs on AWS suck!
    Way too many choices to pick from.

  2. Tony

    I was able to get the tutorial working.
    One not, make sure you are in the rails app directory
    when running the aws and eb commands.

    Excellent introductory tutorial to get a Rails app up on AWS.

    Thanks, Jason.

Comments are closed.