AWS DevOps Blog

Locally Packaging Gem Dependencies for Ruby Applications in Elastic Beanstalk

Today’s guest post is by Charlie Crawford, a developer on the Elastic Beanstalk team.

The Puma Ruby 2 container has a built-in feature to detect locally installed gems. This feature is easy to use, ensures that your production environment is using the same gems as your development environment, and helps your application deploy faster.

Although this feature works for any Ruby application containing a Gemfile, this brief tutorial will cover the concept of a Rails application.

Here is an example of a fairly typical Rails Gemfile:

gem 'rails', '4.1.0'

gem 'sqlite3'

gem 'sass-rails', '~> 4.0.3'

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.0.0'

gem 'jquery-rails'

gem 'turbolinks'

gem 'jbuilder', '~> 2.0'

gem 'sdoc', '~> 0.4.0',          group: :doc

gem 'spring',        group: :development

From the root directory of your application bundle, run this command:

$> bundle package

You will now see a new directory called vendor/cache in your root directory.

Use the following command to see the contents of the directory. You should see a list like the following.

$> ls vendor/cache

actionmailer-4.1.0.gem   arel-   execjs-2.0.2.gem        mail-2.5.4.gem         rack-test-0.6.2.gem  sass-rails-4.0.3.gem       thor-0.19.1.gem        uglifier-2.5.0.gem

actionpack-4.1.0.gem     builder-3.2.2.gem               hike-1.2.3.gem          mime-types-1.25.1.gem  rails-4.1.0.gem      sdoc-0.4.0.gem             thread_safe-0.3.3.gem

actionview-4.1.0.gem     coffee-rails-4.0.1.gem          i18n-0.6.9.gem          minitest-5.3.3.gem     railties-4.1.0.gem   spring-1.1.2.gem           tilt-1.4.1.gem

activemodel-4.1.0.gem    coffee-script-2.2.0.gem         jbuilder-2.0.6.gem      multi_json-1.9.3.gem   rake-10.3.1.gem      sprockets-2.11.0.gem       treetop-1.4.15.gem

activerecord-4.1.0.gem   coffee-script-source-1.7.0.gem  jquery-rails-3.1.0.gem  polyglot-0.3.4.gem     rdoc-4.1.1.gem       sprockets-rails-2.1.3.gem  turbolinks-2.2.2.gem

activesupport-4.1.0.gem  erubis-2.7.0.gem                json-1.8.1.gem          rack-1.5.2.gem         sass-3.2.19.gem      sqlite3-1.3.9.gem          tzinfo-1.1.0.gem

Now when you deploy your application with the Puma Ruby 2 container, your environment will automatically use these locally saved gems and will not attempt to download them.

If you want to deploy your app using the online web console, your next step is to create a .zip of everything in the root directory of your app. This includes the newly created vendor/cache directory.

You can then deploy this .zip folder using the AWS Elastic Beanstalk console: Elastic Beanstalk Console

You can deploy your cached gems using the eb command-line tool:

$> git add vendor/cache && git commit -m 'Add cached gmes' && git aws.push

For additional help on deploying your finished application, go here: Deploying AWS Elastic Beanstalk Applications in Ruby Using Eb and Git