Class: LambdaRubyBundler::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_ruby_bundler/executor.rb

Overview

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.

Examples:

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/...

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, app_path, build_dependencies) ⇒ Executor

Creates new instance of the Executor.

Parameters:

  • root_path (String)

    Path to the root of the application (with Gemfile)

  • app_path (String)

    Path to the Ruby application code, relative to root.

  • build_dependencies (Boolean)

    Flag whether or not to bundle the dependencies. Useful in cases when dependencies (Gemfile.lock) does not change between builds.



46
47
48
49
50
# 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

Instance Attribute Details

#app_pathObject (readonly)

Returns the value of attribute app_path.



34
35
36
# File 'lib/lambda_ruby_bundler/executor.rb', line 34

def app_path
  @app_path
end

#build_dependenciesObject (readonly)

Returns the value of attribute build_dependencies.



34
35
36
# File 'lib/lambda_ruby_bundler/executor.rb', line 34

def build_dependencies
  @build_dependencies
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



34
35
36
# File 'lib/lambda_ruby_bundler/executor.rb', line 34

def root_path
  @root_path
end

Instance Method Details

#runHash{:application_bundle, :dependency_layer => StringIO}

Generates the ZIP contents.

Returns:

  • (Hash{:application_bundle, :dependency_layer => StringIO})

    Hash with the zip IOs. If ‘build_dependencies` options was falsey, There will be no :dependency_layer key.



57
58
59
60
61
62
# 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