Skip to main content
Version: 2.x

ZLambdaRunner

ZLambdaRunner is the entry point of the zio-lambda module. It turns a function from an event and a Context into a program that serves AWS Lambda invocations. Both of its methods return a RIO[R, Unit], so you return them from the run method of a ZIOAppDefault.

object ZLambdaRunner {
def serve[R, IN: JsonDecoder, OUT: JsonEncoder, ERR <: Throwable](
appFunction: (IN, Context) => ZIO[R, ERR, OUT]
): RIO[R, Unit]

def serveRaw[R](
rawFunction: (String, Context) => ZIO[R, Throwable, String]
): RIO[R, Unit]
}

The Invocation Loopโ€‹

Both methods run the same loop. For each invocation, the runner asks the Lambda Runtime API for the next event, builds a Context, calls your function and reports the outcome to Lambda. The loop never ends on its own.

  • If your effect succeeds, the runner sends the result to Lambda as the invocation response.
  • If your effect fails, or the event cannot be decoded, the runner sends an invocation error to Lambda and then waits for the next invocation.

serveโ€‹

serve decodes the event JSON into IN with its JsonDecoder, calls your function, and encodes the OUT value with its JsonEncoder. Both codecs are implicit parameters, so they must be in scope, usually in the companion objects of the types.

The error type ERR must be a subtype of Throwable. The environment R is provided by your application, for example with .provide(...) on the effect your function returns.

If decoding fails, the runner fails the invocation with a Throwable whose message starts with Error decoding json. Json= followed by the received payload and the zio-json error.

serveRawโ€‹

serveRaw passes the event to your function as the raw JSON String and sends back the String you return, with no codecs. Its error type is Throwable, and the string you return is sent to Lambda as is, so it must already be valid JSON if the caller expects JSON.

ZLambda (Deprecated)โ€‹

ZLambda[E, A] is an abstract class that predates ZLambdaRunner. It is deprecated since 1.0.3. It extends ZIOAppDefault, requires a JsonDecoder[E] and a JsonEncoder[A], and asks you to implement def apply(event: E, context: Context): Task[A]. To migrate, replace the class with an object that extends ZIOAppDefault and pass the same function to ZLambdaRunner.serve, as shown in Getting Started.

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–