AWS.Lambda.Runtime.Handler behaviour (AWS Lambda Runtime v0.1.0)

View Source

Behaviour implemented by your function, plus resolution of the _HANDLER environment variable that Lambda sets from the function's Handler setting.

Accepted handler strings

MyLambda.Handler                  # calls MyLambda.Handler.handle_event/2
MyLambda.Handler.handle_event     # explicit function
Elixir.MyLambda.Handler.process   # the Elixir. prefix is optional

The rule is simple: if the last dot-separated segment starts with a lowercase letter or underscore it is taken as the function name, otherwise the whole string is the module and handle_event/2 is assumed.

A compile-time override can be set with

config :aws_lambda_runtime, :handler, {MyLambda.Handler, :handle_event}

which takes precedence over _HANDLER and is handy for local testing.

Return values

  • {:ok, payload}payload is JSON encoded and returned to the caller
  • {:error, reason} — reported to Lambda as a function error
  • anything else — encoded and returned as the payload

Raising, exiting or throwing is caught by the runtime and reported as a function error without taking down the VM, so the sandbox stays warm.

Summary

Functions

Parses a handler spec (a module, a {module, function} pair, or a handler string as described in the moduledoc) into a {module, function} pair.

Resolves the configured handler into a {module, function} pair, raising if it cannot be loaded. Called once during initialisation so that a bad handler surfaces as an init error rather than failing every invocation.

Callbacks

handler(event, context)

@callback handler(event :: term(), context :: term()) ::
  {:ok, term()} | {:error, term()} | term()

Functions

parse!(module)

Parses a handler spec (a module, a {module, function} pair, or a handler string as described in the moduledoc) into a {module, function} pair.

resolve!()

Resolves the configured handler into a {module, function} pair, raising if it cannot be loaded. Called once during initialisation so that a bad handler surfaces as an init error rather than failing every invocation.