Writing a handler for AWS lambda in Scala can be as easy as...
import io.circe.generic.auto._
import io.github.mkotsur.aws.handler.Lambda._
import io.github.mkotsur.aws.handler.Lambda
import com.amazonaws.services.lambda.runtime.Context
case class Ping(inputMsg: String)
case class Pong(outputMsg: String)
class PingPongHandler extends Lambda[Ping, Pong] {
override def handle(ping: Ping, context: Context) = Right(Pong(ping.inputMsg.reverse))
}
The input JSON will be automatically de-serialized into Ping
, and the output into Pong
. The handle()
method is supposed to return Either[Throwable, Pong]
: Right
if the input was handled correctly, and Left
otherwise.
This handler can be used in AWS Lambda as: io.github.mkotsur.example::handle
.
Features:
More docs are coming soon... Feel free to look at src/test/scala
if you want to use it right now.
import io.circe.generic.auto._
import io.github.mkotsur.aws.handler.Lambda._
import io.github.mkotsur.aws.handler.Lambda
import com.amazonaws.services.lambda.runtime.Context
import scala.concurrent.Future
case class Ping(inputMsg: String)
class PingFuturePongHandler extends Lambda[Ping, Future[Int]] {
override def handle(ping: Ping, context: Context) =
Right(Future.successful(ping.inputMsg.length))
}
This lambda will accept an empty string, or string with null
as an input.
import io.circe.generic.auto._
import io.github.mkotsur.aws.handler.Lambda._
import io.github.mkotsur.aws.handler.Lambda
import com.amazonaws.services.lambda.runtime.Context
class NothingToNothingHandler extends Lambda[None.type, None.type] {
override def handle(_: None.type , context: Context) = {
println("Only sinde effects")
Right(None)
}
}
Scala versions supported: 2.11.x, 2.12.x, 2.13.x.
libraryDependencies += "io.github.mkotsur" %% "aws-lambda-scala" % {latest-version}
Short answer: they complement each other. Long answer: read this blog post.