class LambdaRubyBundler::Executor

Main entrypoint to the application, which packages the code from given directory into ZIP.

The packaging is done inside a container resembling Lambda environment, ensuring that the gems with C extensions will work there properly.

@example Default use case

# Given following directory structure:
# /tmp/my_serverless_app
# +-- Gemfile
# +-- Gemfile.lock
# +-- backend/
# |   +-- handler.rb
# +-- node_modules/...

executor = LambdaRubyBundler::Executor.new(
  '/tmp/my_serverless_app',
  'backend',
  true
)

result = executor.run
File.write('bundle.zip', result[:application_bundle].read)
File.write('dependencies.zip', result[:dependency_layer].read)

# Note that resulting structure of the ZIP will be flattened!
# + handler.rb
# + vendor/bundle/...

Attributes

app_path[R]
build_dependencies[R]
root_path[R]

Public Class Methods

new(root_path, app_path, build_dependencies) click to toggle source

Creates new instance of the Executor.

@param [String] root_path

Path to the root of the application (with Gemfile)

@param [String] app_path

Path to the Ruby application code, relative to root.

@param [Boolean] build_dependencies

Flag whether or not to bundle the dependencies.
Useful in cases when dependencies (Gemfile.lock) does not change
between builds.
# File lib/lambda_ruby_bundler/executor.rb, line 46
def initialize(root_path, app_path, build_dependencies)
  @root_path = root_path
  @app_path = app_path
  @build_dependencies = build_dependencies
end

Public Instance Methods

run() click to toggle source

Generates the ZIP contents.

@return [Hash{:application_bundle, :dependency_layer => StringIO}]

Hash with the zip IOs. If `build_dependencies` options was falsey,
There will be no :dependency_layer key.
# File lib/lambda_ruby_bundler/executor.rb, line 57
def run
  zipped_contents, = container.run.tap { container.destroy }
  contents = JSON.parse(zipped_contents.join)

  decode(contents)
end

Private Instance Methods

application_name() click to toggle source
# File lib/lambda_ruby_bundler/executor.rb, line 72
def application_name
  @application_name ||= File.basename(root_path)
end
container() click to toggle source
# File lib/lambda_ruby_bundler/executor.rb, line 76
def container
  @container ||= Container.new(
    root_path, app_path, volume, build_dependencies
  )
end
decode(contents) click to toggle source
# File lib/lambda_ruby_bundler/executor.rb, line 66
def decode(contents)
  contents.each_with_object({}) do |(key, data), result|
    result[key.to_sym] = StringIO.new(Base64.strict_decode64(data))
  end
end
volume() click to toggle source
# File lib/lambda_ruby_bundler/executor.rb, line 82
def volume
  @volume ||= Volume.new(application_name)
end