Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Lambda Runtime #276

Open
wants to merge 59 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
7ea939b
Bump base version
armanbilge Sep 25, 2022
26b1622
Shuffle build for custom runtime
armanbilge Sep 25, 2022
95efe18
http4s and cloudformation only depend on kernel
armanbilge Sep 25, 2022
be96317
WIP: added runtime skeleton, needs error handling
scott-thomson239 Sep 26, 2022
ebf5e97
fixed context creation from environment variables
scott-thomson239 Sep 27, 2022
6f990ad
added comments to unimplemented parts
scott-thomson239 Sep 28, 2022
f6a904b
used new cats effect Env instead of custom env var solution
scott-thomson239 Sep 29, 2022
defb679
added function to obtain LambdaRequest from response and added error …
scott-thomson239 Oct 2, 2022
c95cecb
renamed module and fixed handler error handling
scott-thomson239 Oct 3, 2022
2de1748
changed hander fiber to not raise error after cancellation
scott-thomson239 Oct 4, 2022
b7e6054
created custom invocation response header models
scott-thomson239 Oct 6, 2022
b07406c
tests work in progress
scott-thomson239 Oct 12, 2022
0eb162e
fixed all but one test
scott-thomson239 Oct 13, 2022
24c4261
added test to verify runtime can handle invocation errors correctly
scott-thomson239 Oct 15, 2022
602460b
fixed failing tests, although tests need refactored
scott-thomson239 Oct 16, 2022
25403d0
refactored tests
scott-thomson239 Oct 17, 2022
a65d045
added init error handling and tests
scott-thomson239 Oct 19, 2022
206ec6a
fixed failing test
scott-thomson239 Oct 20, 2022
aa06238
general clean up
scott-thomson239 Oct 20, 2022
39562d6
fix missing import
scott-thomson239 Oct 20, 2022
108c2cb
ran scalafmt
scott-thomson239 Oct 20, 2022
016df57
ran sbt githubWorkflowGenerate
scott-thomson239 Oct 20, 2022
7ce4f85
added file headers
scott-thomson239 Oct 20, 2022
fe73a65
added .settings(commonSettings) to lambdaRuntime and lambdaKernel
scott-thomson239 Oct 22, 2022
ed8cb7d
Merge branch 'main' into feature/custom-lambda-runtime
scott-thomson239 Oct 22, 2022
f2b7151
ran sbt githubWorkflowGenerate
scott-thomson239 Oct 22, 2022
0cf6033
fix type erasure warning
scott-thomson239 Oct 22, 2022
e3f8553
Merge branch 'main' into feature/custom-lambda-runtime
armanbilge Oct 22, 2022
7c69d9e
fix for potential type inference problem in expect method and correct…
scott-thomson239 Oct 23, 2022
cfd8f0a
modified sbtLambda in build.sbt to include lambdaKernel.js in scripte…
scott-thomson239 Oct 23, 2022
accb4b6
forgot to format buld.sbt oops
scott-thomson239 Oct 23, 2022
6acf432
updated natchez-core in lambdaKernel and updated scalajs plugin to 1.…
scott-thomson239 Oct 23, 2022
2449d3e
fix for 'withFilter is not a member of IO' error
scott-thomson239 Oct 23, 2022
9d79651
added snapshot resolver to sbt-lambda build.sbt
scott-thomson239 Oct 23, 2022
3c4974c
added lambdaKernel and lambdaRuntime to build.sbt unidocs
scott-thomson239 Oct 23, 2022
2548296
test to see if removing rethrow from IOSetup solves compiler error
scott-thomson239 Oct 23, 2022
35d797f
added MatrixExclude for 2.12/native
scott-thomson239 Oct 23, 2022
93d55aa
changed cats-effect-kernel to cats-effect in lambdaKernel and removed…
scott-thomson239 Oct 23, 2022
af3b527
Make lambda headers package-private
armanbilge Nov 30, 2022
f3022e4
Parse/decode in single op
armanbilge Nov 30, 2022
d630597
More package private models
armanbilge Nov 30, 2022
366248a
Build error body from `Throwable`
armanbilge Nov 30, 2022
ab6fa05
Define `EntityEncoder` for `LmabdaErrorBody`
armanbilge Nov 30, 2022
54c3032
Reduce noise
armanbilge Dec 4, 2022
b5e3019
Simplifications
armanbilge Dec 4, 2022
d6de562
Use `Uri` for `lambdaRuntimeApi`
armanbilge Dec 4, 2022
13aa8e9
Running lambda returns `Nothing`
armanbilge Dec 4, 2022
3517cb7
Refactor `processEvents`
armanbilge Dec 4, 2022
ffa7bb3
`deadlineTimeInMs` -> `deadlineTime`
armanbilge Dec 4, 2022
f1f7a2c
Header names are ci strings
armanbilge Dec 4, 2022
f83a9de
Do not leak input in sanitized error msg
armanbilge Dec 4, 2022
c700129
Fix compile
armanbilge Dec 4, 2022
9a49c7d
Use `embedError`
armanbilge Dec 6, 2022
d53f8be
refactored to have environment variables read in the initialization p…
scott-thomson239 Dec 6, 2022
f284899
readded Nothing instead of Unit in LambdaRuntime
scott-thomson239 Dec 6, 2022
3da24a9
fixed failing tests from refactoring initialization phase
scott-thomson239 Dec 6, 2022
8cdfc9b
remove accidental encoder import
scott-thomson239 Dec 6, 2022
541704e
reverted to using F[Nothing] as output type
scott-thomson239 Dec 7, 2022
89d1c69
changed init error test env to better reflect real behaviour
scott-thomson239 Dec 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ val Scala213 = "2.13.9"
val Scala3 = "3.1.3"
ThisBuild / crossScalaVersions := Seq(Scala212, Scala3, Scala213)

val catsEffectVersion = "3.3.14"
val catsEffectVersion = "3.4.0-RC1"
val circeVersion = "0.14.3"
val fs2Version = "3.3.0"
val http4sVersion = "0.23.16"
Expand Down Expand Up @@ -121,8 +121,9 @@ lazy val lambdaRuntime = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("feral-lambda-runtime"))
.settings(
name := "feral-lambda-kernel",
name := "feral-lambda-runtime",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops :) thanks! can also rename the file to just file("lambda-runtime")

libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"io.circe" %%% "circe-jawn" % circeVersion,
"org.http4s" %%% "http4s-client" % http4sVersion,
"org.http4s" %%% "http4s-circe" % http4sVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ case class LambdaRequest(
)

object LambdaRequest {
// Still need to decide how to handle failed request or invalid header values
def fromResponse[F[_]](response: Response[F])(implicit F: ApplicativeError[F, Throwable]): F[LambdaRequest] = {
F.pure(LambdaRequest(???))
//Unsure on best way to unpack the response headers into LambdaRequest case class
???
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,54 @@
* limitations under the License.
*/

// TODO implement a runtime here
// it should retrieve incoming events and handle them with the handler
// it will run on a background fiber, whose lifecycle is controlled by the resource

package feral.lambda
package runtime

import cats.Applicative
import cats.syntax.all._
import cats.effect.kernel.{Resource, Sync}
import cats.effect.kernel.Sync
import io.circe.Json
import org.http4s.Method.POST
import org.http4s.client.Client
import org.http4s.circe.jsonEncoderWithPrinter

import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.TimeUnit
import org.http4s.{EntityEncoder, Uri}
import org.http4s.client.dsl.Http4sClientDsl
import io.circe._

import java.time.Instant //safe to use in native?
import cats.effect.kernel.Async
import cats.effect.std.Env
import org.http4s.implicits.http4sLiteralsSyntax

// TODO apply function error handling
// How to run global or static code from handler?
// TODO Custom AWS header models
// TODO CognitoIdentity/ClientContext JSON encoding

object FeralLambdaRuntime {

val LAMBDA_VERSION_DATE = "2018-06-01"
final val ApiVersion = "2018-06-01"

def apply[F[_]](client: Client[F])(handler: (Json, Context[F]) => F[Json])(implicit F: Async[F]): Resource[F, Unit] = {
F.background {
val runtimeUrl = getRuntimeUrl(LambdaReservedEnvVars.AWS_LAMBDA_RUNTIME_API)
implicit val jsonEncoder: EntityEncoder[F, Json] = jsonEncoderWithPrinter[F](Printer.noSpaces.copy(dropNullValues = true))
val http4sClientDsl = new Http4sClientDsl[F] {}
import http4sClientDsl._
(for {
request <- client.get(runtimeUrl)(LambdaRequest.fromResponse) // unsure how to deal with bad response
context <- createContext(request)
result <- handler(request.body, context)
invocationUrl = getInvocationUrl(LambdaReservedEnvVars.AWS_LAMBDA_RUNTIME_API, request.id)
_ <- client.successful(POST(result, invocationUrl))
} yield ()).foreverM
}
}.as(()) // how to handle Outcome error and cancellation?
def apply[F[_]](client: Client[F])(handler: (Json, Context[F]) => F[Json])(implicit F: Async[F]): F[Unit] = {
implicit val lambdaEnv: LambdaRuntimeEnv[F] = LambdaRuntimeEnv(Env.make) // maybe better way
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just directly request an implicit env: LambdaRuntimeEnv[F]

implicit val jsonEncoder: EntityEncoder[F, Json] = jsonEncoderWithPrinter[F](Printer.noSpaces.copy(dropNullValues = true))
val http4sClientDsl = new Http4sClientDsl[F] {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit superstitious, but would you mind if we avoid using the DSL here and just use the Request(...) constructor? Since our use is so small and straightforward, and DSLs always risk to be more unstable than the core APIs (indeed, there was some discussion of moving the routing DSL to a separate repository).

import http4sClientDsl._
(for {
runtimeApi <- lambdaEnv.lambdaRuntimeApi
request <- client.get(getRuntimeUrl(runtimeApi))(LambdaRequest.fromResponse)
context <- createContext(request)
result <- handler(request.body, context)
invocationUrl = getInvocationUrl(LambdaRuntimeEnv.AWS_LAMBDA_RUNTIME_API, request.id)
_ <- client.successful(POST(result, invocationUrl))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

successful returns a Boolean, which is currently being ignored. I wonder if expect[Unit] will work, so it will raise an exception on failure.

} yield ()).foreverM
}.void

private def createContext[F[_]](request: LambdaRequest)(implicit F: Sync[F]): F[Context[F]] = for {
functionName <- envVar(LambdaReservedEnvVars.AWS_LAMBDA_FUNCTION_NAME)
functionVersion <- envVar(LambdaReservedEnvVars.AWS_LAMBDA_FUNCTION_VERSION)
functionMemorySize <- envVar(LambdaReservedEnvVars.AWS_LAMBDA_FUNCTION_MEMORY_SIZE).map(_.toInt)
logGroupName <- envVar(LambdaReservedEnvVars.AWS_LAMBDA_LOG_GROUP_NAME)
logStreamName <- envVar(LambdaReservedEnvVars.AWS_LAMBDA_LOG_STREAM_NAME)
private def createContext[F[_]](request: LambdaRequest)(implicit F: Sync[F], lambdaEnv: LambdaRuntimeEnv[F]): F[Context[F]] = for {
functionName <- lambdaEnv.lambdaFunctionName
functionVersion <- lambdaEnv.lambdaFunctionVersion
functionMemorySize <- lambdaEnv.lambdaFunctionMemorySize
logGroupName <- lambdaEnv.lambdaLogGroupName
logStreamName <- lambdaEnv.lambdaLogStreamName
} yield {
new Context[F](
functionName,
Expand All @@ -73,23 +71,18 @@ object FeralLambdaRuntime {
request.id,
logGroupName,
logStreamName,
None, //need
None, //need
F.delay(FiniteDuration(request.deadlineTimeInMs.toEpochMilli - Instant.now.toEpochMilli, TimeUnit.MILLISECONDS))
None,
None,
F.realTimeInstant.map(curTime => FiniteDuration(request.deadlineTimeInMs.toEpochMilli - curTime.toEpochMilli, TimeUnit.MILLISECONDS))/// how to provide test version?, maybe separate Clock parameter?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of converting to millis and back to duration, you can use F.realTime and do that math directly with FiniteDuration.

)
}

private def getRuntimeUrl(api: String) = Uri.unsafeFromString(s"http://$api/$LAMBDA_VERSION_DATE/runtime/invocation/next") //need to be unsafe?
private def getRuntimeUrl(api: String) = uri"http://$api/$ApiVersion/runtime/invocation/next"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, does using uri"..." here actually work? That seems like a bug 😅 since it has no way of proving at compile-time that api is valid.


private def getInvocationUrl(api: String, id: String) = Uri.unsafeFromString(s"http://$api/$LAMBDA_VERSION_DATE/runtime/invocation/$id/response")
private def getInvocationUrl(api: String, id: String) = uri"http://$api/$ApiVersion/runtime/invocation/$id/response"

// Called if initialization of handler function fails, seems impossible here since handler function is provided as constructor?
private def getInitErrorUrl(api: String) = Uri.unsafeFromString(s"http://$api/$LAMBDA_VERSION_DATE/runtime/init/error")
private def getInitErrorUrl(api: String) = uri"http://$api/$ApiVersion/runtime/init/error"

// from docs, called "if the function returns an error or the runtime encounters an error", will be used after error handling implemented
private def getInvocationErrorUrl(api: String, errorType: String) = Uri.unsafeFromString(s"http://$api/$LAMBDA_VERSION_DATE/runtime/invocation/$errorType/error")
private def getInvocationErrorUrl(api: String, errorType: String) = uri"http://$api/$ApiVersion/runtime/invocation/$errorType/error"

private def envVar[F[_]](envVar: String)(implicit F: Sync[F]): F[String] = {
F.delay(sys.env(envVar))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package feral.lambda.runtime

import cats.{ApplicativeError, Functor}
import cats.effect.kernel.Sync
import cats.effect.std.Env
import cats.syntax.all._

trait LambdaRuntimeEnv[F[_]] {
def handler: F[String]
def region: F[String]
def executionEnv: F[String]
def lambdaFunctionName: F[String]
def lambdaFunctionMemorySize: F[Int]
def lambdaFunctionVersion: F[String]
def lambdaLogGroupName: F[String]
def lambdaLogStreamName: F[String]
def accessKeyId: F[String]
def secretAccessKey: F[String]
def lambdaRuntimeApi: F[String]
def lambdaTaskRoot: F[String]
def lambdaRuntimeDir: F[String]
def timezone: F[String]
}

object LambdaRuntimeEnv {
final val HANDLER = "_HANDLER"
final val AWS_REGION = "AWS_REGION"
final val AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV"
final val AWS_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME"
final val AWS_LAMBDA_FUNCTION_MEMORY_SIZE = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE"
final val AWS_LAMBDA_FUNCTION_VERSION = "AWS_LAMBDA_FUNCTION_VERSION"
final val AWS_LAMBDA_LOG_GROUP_NAME = "AWS_LAMBDA_LOG_GROUP_NAME"
final val AWS_LAMBDA_LOG_STREAM_NAME = "AWS_LAMBDA_LOG_STREAM_NAME"
final val AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"
final val AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"
final val AWS_LAMBDA_RUNTIME_API = "AWS_LAMBDA_RUNTIME_API"
final val LAMBDA_TASK_ROOT = "LAMBDA_TASK_ROOT"
final val LAMBDA_RUNTIME_DIR = "LAMBDA_RUNTIME_DIR"
final val TZ = "TZ"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can all be private[this]


def apply[F[_]: Functor](env: Env[F]): LambdaRuntimeEnv[F] = new LambdaRuntimeEnv[F] { // should be error effect?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, on second thought, I think we want:

Suggested change
def apply[F[_]: Functor](env: Env[F]): LambdaRuntimeEnv[F] = new LambdaRuntimeEnv[F] { // should be error effect?
def apply[F[_]](implicit lre: LambdaRuntimeEnv[F]): LambdaRuntimeEnv[F] = lre
implicit def forEnv[F[_]: MonadThrow](implicit env: Env[F]): LambdaRuntimeEnv[F] = new LambdaRuntimeEnv[F] {

override def handler: F[String] = env.get(HANDLER).map(_.get)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid .get, instead something like this with MonadThrow[F].

Suggested change
override def handler: F[String] = env.get(HANDLER).map(_.get)
def handler: F[String] = env.get(HANDLER).flatMap(_.liftTo(new NoSuchElementException(HANDLER))


override def region: F[String] = env.get(AWS_REGION).map(_.get)

override def executionEnv: F[String] = env.get(AWS_EXECUTION_ENV).map(_.get)

override def lambdaFunctionName: F[String] = env.get(AWS_LAMBDA_FUNCTION_NAME).map(_.get)

override def lambdaFunctionMemorySize: F[Int] = env.get(AWS_LAMBDA_FUNCTION_MEMORY_SIZE).map(_.get)

override def lambdaFunctionVersion: F[String] = env.get(AWS_LAMBDA_FUNCTION_VERSION).map(_.get)

override def lambdaLogGroupName: F[String] = env.get(AWS_LAMBDA_LOG_GROUP_NAME).map(_.get)

override def lambdaLogStreamName: F[String] = env.get(AWS_LAMBDA_LOG_STREAM_NAME).map(_.get)

override def accessKeyId: F[String] = env.get(AWS_ACCESS_KEY_ID).map(_.get)

override def secretAccessKey: F[String] = env.get(AWS_SECRET_ACCESS_KEY).map(_.get)

override def lambdaRuntimeApi: F[String] = env.get(AWS_LAMBDA_RUNTIME_API).map(_.get)

override def lambdaTaskRoot: F[String] = env.get(LAMBDA_TASK_ROOT).map(_.get)

override def lambdaRuntimeDir: F[String] = env.get(LAMBDA_RUNTIME_DIR).map(_.get)

override def timezone: F[String] = env.get(TZ).map(_.get)
}
}