lambda-layer-cake

A set of Rake tasks to bundle Rails apps for use on AWS Lambda


License
MIT
Install
gem install lambda-layer-cake -v 0.2.1

Documentation

LambdaLayerCake

LambdaLayerCake provides a set of rake tasks to produce two zip files for deploying Rails projects to AWS Lambda. One zip file (layer.zip) is of gems and any necessary system-level shared libraries, the second (app.zip) is of the Rails code itself. By splitting the project into two pieces, the inline lambda code editor can be used to edit most Rails projects.

I wrote this code to bridge the best aspects of Ruby on Jets and Lamby. Jets is great, but I didn't want to migrate off Rails onto a framework where the APIs potentially different (or are unimplemented). Lamby is also solid, but I wanted to package my dependencies indepentently and I didn't want to use AWS SAM to manage deployment of my app. I also wanted a clear, automated mechanism to include extra system packages in the Lambda environment.

This gem does just the packaging of the app and leaves deployment as an exercise for the user.

Usage

  1. Include Lamby and this gem in your Rails app's Gemfile.

gem 'lambda-layer-cake' gem 'lamby'

  1. Create an app.rb in the root of your project that contains (stock Lamby app.rb with an extra line for Bundler config):
# This line tells Bundler where the Layer Cake gems are located
ENV['BUNDLE_PATH'] = 'vendor/bundle/ruby/2.5.0'

require_relative 'config/boot'
require 'lamby'
require_relative 'config/application'
require_relative 'config/environment'

$app = Rack::Builder.new { run Rails.application }.to_app

def handler(event:, context:)
  Lamby.handler $app, event, context, rack: :api
end
  1. If you need extra system packages (such as postgresql-libs for the pg gem), create a file called system-packages.txt in the root of your Rails project and include package names, one per line.
  2. When you are ready to deploy your app, use rake .layer_cake/layer.zip and rake .layer_cake/app.zip to build files suitable for deployment to Lambda.
  3. Use the the demployment tools of your choice to define a Lambda function. The layer.zip

How It Works

LambdaLayerCake uses three input files to build layer.zip: Gemfile, Gemfile.lock, and system-packages.txt. The layer is built using the lambci/lambda:build-ruby2.5 Docker image and any packages you specify in system-packages.txt are installed prior to building and installing gems. The packages themselves are not copied into the Lambda layer. LambdaLayerCake, however, walks all shared libraries built during gem installing, uses ldd to find dependent libraries that are not in the standard Lambda image, and copies them into the layer.

LambdaLayerCake simply bundles the Rails directory into app.zip, excluding files that begin with . and several directories that are not needed outside of development.

Contributing

This is my first attempt at publishing a complete gem and Rails plugin. I took it as an opportunity to learn and write idiomatic Rakefiles, automate usage of Docker, etc. Suggestions on how to improve the structure of any of the moving pieces is most welcome, pull requests are even better. Please open an issue.

TODO

Contributions on the following items is particularly appreciated:

  • Modifying the Docker build environment to probe the host system's cache of gemfiles to more gracefully handle private and unreleased gems
  • Give proper names to the rake tasks
  • A better clean Rake task
  • Under some circumstances, app.zip does rebuild due to not detecting dependencies

License

The gem is available as open source under the terms of the MIT License.