Module: ElasticGraph::LambdaSupport::LambdaFunction

Defined in:
lib/elastic_graph/lambda_support/lambda_function.rb

Overview

Mixin that can be used to define a lambda function, with common cross-cutting concerns handled automatically for you:

- The amount of time it takes to boot the lambda is logged.
- An error handling hook is provided which applies both to boot-time logic and request handling.

It is designed to be prepending onto a class, like so:

class DoSomething

prepend LambdaFunction

def initialize
  require 'my_application'
  @application = MyApplication.new(ENV[...])
end

def handle_request(event:, context:)
  @application.handle_request(event: event, context: context)
end

end

Using ‘prepend` is necessary so that it can wrap `initialize` and `handle_request` with error handling. It is recommended that `require`s be put in `initialize` instead of at the top of the lambda function file so that the error handler can handle any errors that happen while loading dependencies.

‘handle_exceptions` can also be overridden in order to provide error handling.

Instance Method Summary collapse

Instance Method Details

#handle_request(event:, context:) ⇒ Object



48
49
50
# File 'lib/elastic_graph/lambda_support/lambda_function.rb', line 48

def handle_request(event:, context:)
  handle_exceptions { super }
end

#initialize(output: $stdout, monotonic_clock: Support::MonotonicClock.new) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/elastic_graph/lambda_support/lambda_function.rb', line 40

def initialize(output: $stdout, monotonic_clock: Support::MonotonicClock.new)
  handle_exceptions do
    log_duration(output, monotonic_clock, "Booting the lambda function") do
      super()
    end
  end
end