Learn Clould Application Development and DevOps

The Blackninja Blog

Expand your Cloud Application Development and DevOps knowledge with detailed tutorials and case studies.

Ruby Installing and Configuring the Elastic Beanstalk CLI to Deploy a Ruby on Rails Application

This post is going to show you how to deploy a Ruby on Rails application to Elastic Beanstalk on Amazon Web Services using the Elastic Beanstalk Command Line Interface (CLI).

What is the Elastic Beanstalk CLI?

The Elastic Beanstalk CLI (EB CLI) is a high level interface provided by Amazon to work specifically with Elastic Beanstalk Applications and Environments. You may have already installed the AWS CLI, this one is different. You can see all the commands availble using the EB CLI using the EB CLI Command Reference provided by Amazon.

Assumptions

This guide assumes you are familiar with the macOS Terminal and have the mac package manager Homebrew installed. This guide also assumes you have an Elastic Beanstalk Application and Environment setup using Step 1: Configuring an Elastic Beanstalk Environment for a Ruby on Rails Application or on your own.

Installing the Elastic Beanstalk CLI

The EB CLI is installed using python’s pip package manager. This means you need to have python installed on your mac to use it.

Install python using Homebrew

The following command will install python 3 to your mac.

  1. Run the following command in your Terminal: brew install python

Install the Elastic Beanstalk CLI using pip

  1. Run the following command in your Terminal: pip3 install awsebcli --upgrade --user

Update your macOS PATH to use the Elastic Beanstalk CLI

Once you have finished installing awsebcli you will notice that if you type eb --version in the Terminal you will get command not found. This is because the awsebcli package was installed to a location that is not being covered by your PATH variable. We will fix that by adding the location to the PATH variable.

  1. Review the list of packages installed with pip3 by typing: pip3 list in your Terminal
  2. Notice that the package we just installed (awsebcli) is in the list
  3. Determine the location of the package by typing: pip3 show awsebcli in your Terminal
  4. Copy the location up to and including the version number only: ~/Library/Python/3.7 (We simplified the home directory path using ~ instead)
  5. Open your Terminal shell configuration file for editing using an editor of your choice:
    • Add the following line: export PATH=~/Library/Python/3.7/bin:$PATH (notice we added /bin to the path - where the eb executable lives)
    • Close and reopen your Terminal, or run the following to update the PATH variable: source ~/.bash_profile
  6. Test your EB CLI Installation by typing: eb --version (you should see the version number)

Congratulations! You installed the EB CLI!

Configuring the Elastic Beanstalk CLI inside your Rails Application Directory

Now we are going to configure your Rails Application directory to use a specific Elastic Beanstalk application and environment inside your AWS account. This step assumes you have NOT initialized your application directory with Elastic Beanstalk. Removing the .elasticbeanstalk folder from your application directory would return it to an uninitialized state if that’s what you would like to do.

Part of this configuration involves providing programmatic access to your AWS account using a key and secret. Creating a user with these credentials and ensuring they have the permissions to access Elastic Beanstalk is beyond the scope of this guide. However, you should consider using AWSElasticBeanstalkFullAccess for this user as it gives you everything you need for the EB CLI without creating an Admin user will full access to your AWS account.

  1. Using the Terminal, navigate to your Rails Application Directory
  2. Run the following command in your Terminal to initialize the EB CLI for this directory:
    • eb init
    • Choose the region where your Elastic Beanstalk application was created. (us-west-2 in our case)
    • Setup your credentials by copy and pasting your key and secret as prompted (These will be stored in a file located at ~/.aws/config)
    • Choose your application
    • No for CodeCommit (outside the scope of this guide)

Set a Production Secret for your Rails Application in Elastic Beanstalk

When you deploy your Rails application to Elastic Beanstalk it will run in production configuration by default. In production configuration, Rails looks for an environment variable named SECRET_KEY_BASE. If you don’t set that up in Elastic Beanstalk your Rails application with throw an exception.

  1. Generate a secret for your Rails application and copy it to the mac clipboard:
    • Run the following command in your Terminal:
    • rake secret | pbcopy
  2. Set an environment variable for your Elastic Beanstalk environment using the EB CLI:
    • Run the following command in your Terminal:
    • eb setenv SECRET_KEY_BASE=(paste your secret here)

Running the setenv command may take a moment to finish. You can check the status of your Elastic Beanstalk environment using the command: eb status. When your configuration update is complete, you can ensure the variable is set using the command: eb printenv.

Deploy your Rails Application using the Elastic Beanstalk CLI

One last caveat before you deploy… if your Rails application doesn’t have a default route, then you should add one now.

  1. Deploy your Rails application using the command: eb deploy (this will take some time)
  2. Open your application in a browser using the command: eb open

Congratulations! You just deployed your Rails application to Elastic Beanstalk!