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

feat: lambda custom runtime #496

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p lambda-cloudformation-custom-resource/.js/target lambda-http4s/.jvm/target unidocs/target lambda-http4s/.js/target lambda/js/target scalafix/rules/target lambda/jvm/target sbt-lambda/target lambda-cloudformation-custom-resource/.jvm/target project/target
run: mkdir -p lambda-kernel/jvm/target lambda-kernel/js/target lambda-runtime-binding/jvm/target lambda-cloudformation-custom-resource/.js/target lambda-http4s/.jvm/target unidocs/target lambda-runtime/.js/target lambda-http4s/.js/target lambda-runtime/.jvm/target scalafix/rules/target lambda-runtime-binding/js/target sbt-lambda/target lambda-cloudformation-custom-resource/.jvm/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar lambda-cloudformation-custom-resource/.js/target lambda-http4s/.jvm/target unidocs/target lambda-http4s/.js/target lambda/js/target scalafix/rules/target lambda/jvm/target sbt-lambda/target lambda-cloudformation-custom-resource/.jvm/target project/target
run: tar cf targets.tar lambda-kernel/jvm/target lambda-kernel/js/target lambda-runtime-binding/jvm/target lambda-cloudformation-custom-resource/.js/target lambda-http4s/.jvm/target unidocs/target lambda-runtime/.js/target lambda-http4s/.js/target lambda-runtime/.jvm/target scalafix/rules/target lambda-runtime-binding/js/target sbt-lambda/target lambda-cloudformation-custom-resource/.jvm/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
98 changes: 86 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ lazy val commonSettings = Seq(
lazy val root =
tlCrossRootProject
.aggregate(
lambda,
`lambda-kernel`,
`lambda-runtime-binding`,
`lambda-runtime`,
lambdaHttp4s,
lambdaCloudFormationCustomResource,
examples,
Expand All @@ -82,10 +84,10 @@ lazy val rootSbtScalafix = project
.aggregate(scalafix.componentProjectReferences: _*)
.enablePlugins(NoPublishPlugin)

lazy val lambda = crossProject(JSPlatform, JVMPlatform)
.in(file("lambda"))
lazy val `lambda-kernel` = crossProject(JSPlatform, JVMPlatform)
.in(file("lambda-kernel"))
.settings(
name := "feral-lambda",
name := "feral-lambda-kernel",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"org.tpolecat" %%% "natchez-core" % natchezVersion,
Expand All @@ -94,18 +96,83 @@ lazy val lambda = crossProject(JSPlatform, JVMPlatform)
"com.comcast" %%% "ip4s-core" % "3.6.0",
"org.scodec" %%% "scodec-bits" % "1.2.0",
"org.scalameta" %%% "munit-scalacheck" % munitVersion % Test,
"io.circe" %%% "circe-literal" % circeVersion % Test
),
mimaPreviousArtifacts := Set(
"org.typelevel" %%% "feral-lambda" % "0.3.0"
),
mimaBinaryIssueFilters ++= Seq(
// These classes are moved to lambda-runtime-binding module
ProblemFilters.exclude[MissingTypesProblem]("feral.lambda.Context$"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.ContextCompanionPlatform"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.IOLambda"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.IOLambda$"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.IOLambda$Simple"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.IOLambdaPlatform"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("feral.lambda.Context.xRayTraceId"),
ProblemFilters.exclude[DirectMissingMethodProblem]("feral.lambda.Context#Impl.copy"),
ProblemFilters.exclude[DirectMissingMethodProblem]("feral.lambda.Context#Impl.apply"),
ProblemFilters.exclude[DirectMissingMethodProblem]("feral.lambda.Context#Impl.this")
)
)
.settings(commonSettings)
.jsSettings(
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % "2.5.0",
mimaBinaryIssueFilters ++= Seq(
// These classes are moved to lambda-runtime-binding module
ProblemFilters.exclude[DirectMissingMethodProblem]("feral.lambda.Context.fromJS"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.facade.ClientContext"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.facade.ClientContextClient"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.facade.ClientContextEnv"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.facade.CognitoIdentity"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.facade.Context")
)
)
.jvmSettings(
mimaBinaryIssueFilters ++= Seq(
// These classes are moved to lambda-runtime-binding module
ProblemFilters.exclude[DirectMissingMethodProblem]("feral.lambda.Context.fromJava")
)
)

lazy val `lambda-runtime` = crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("lambda-runtime"))
.settings(
name := "feral-lambda-runtime",
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-core" % http4sVersion,
"org.http4s" %%% "http4s-ember-client" % http4sVersion,
"org.http4s" %%% "http4s-circe" % http4sVersion,
"org.http4s" %%% "http4s-dsl" % http4sVersion % Test,
"org.typelevel" %%% "munit-cats-effect-3" % munitCEVersion % Test,
"io.circe" %%% "circe-literal" % circeVersion % Test
),
mimaPreviousArtifacts := Set.empty
)
.settings(commonSettings)
.dependsOn(`lambda-kernel`)

lazy val `lambda-runtime-binding` = crossProject(JSPlatform, JVMPlatform)
.in(file("lambda-runtime-binding"))
.settings(
name := "feral-lambda-runtime-binding",
libraryDependencies ++= Seq(
"org.typelevel" %%% "munit-cats-effect-3" % munitCEVersion % Test,
"io.circe" %%% "circe-literal" % circeVersion % Test
),
mimaPreviousArtifacts := Set(
"org.typelevel" %%% "feral-lambda" % "0.3.0"
),
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[IncompatibleResultTypeProblem]("feral.lambda.IOLambda.setupMemo")
ProblemFilters.exclude[IncompatibleResultTypeProblem]("feral.lambda.IOLambda.setupMemo"),
ProblemFilters.exclude[MissingClassProblem]("feral.lambda.*")
)
)
.settings(commonSettings)
.jsSettings(
libraryDependencies ++= Seq(
"io.circe" %%% "circe-scalajs" % circeVersion,
"io.github.cquiroz" %%% "scala-java-time" % "2.5.0"
"io.circe" %%% "circe-scalajs" % circeVersion
)
)
.jvmSettings(
Expand All @@ -115,6 +182,7 @@ lazy val lambda = crossProject(JSPlatform, JVMPlatform)
"co.fs2" %%% "fs2-io" % fs2Version
)
)
.dependsOn(`lambda-kernel`)

lazy val sbtLambda = project
.in(file("sbt-lambda"))
Expand All @@ -129,7 +197,9 @@ lazy val sbtLambda = project
scriptedLaunchOpts := {
scriptedLaunchOpts.value ++ Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
},
scripted := scripted.dependsOn(lambda.js / publishLocal).evaluated,
scripted := scripted
.dependsOn(`lambda-kernel`.js / publishLocal, `lambda-runtime-binding`.js / publishLocal)
.evaluated,
Test / test := scripted.toTask("").value
)

Expand All @@ -143,7 +213,9 @@ lazy val lambdaHttp4s = crossProject(JSPlatform, JVMPlatform)
)
)
.settings(commonSettings)
.dependsOn(lambda % "compile->compile;test->test")
.dependsOn(
`lambda-kernel` % "compile->compile;test->test",
`lambda-runtime-binding` % "compile->compile;test->test")

lazy val lambdaCloudFormationCustomResource = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
Expand All @@ -168,7 +240,7 @@ lazy val lambdaCloudFormationCustomResource = crossProject(JSPlatform, JVMPlatfo
)
)
.settings(commonSettings)
.dependsOn(lambda)
.dependsOn(`lambda-runtime-binding`)

lazy val examples = crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
Expand All @@ -183,7 +255,7 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform)
)
)
.settings(commonSettings)
.dependsOn(lambda, lambdaHttp4s)
.dependsOn(`lambda-runtime-binding`, lambdaHttp4s)
.enablePlugins(NoPublishPlugin)

lazy val unidocs = project
Expand All @@ -196,7 +268,9 @@ lazy val unidocs = project
inProjects(sbtLambda)
else
inProjects(
lambda.jvm,
`lambda-kernel`.jvm,
`lambda-runtime-binding`.jvm,
`lambda-runtime`.jvm,
lambdaHttp4s.jvm,
lambdaCloudFormationCustomResource.jvm
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ trait CloudFormationCustomResourceArbitraries {
invokedFunctionArn <- arbitrary[String]
memoryLimitInMB <- arbitrary[Int]
awsRequestId <- arbitrary[String]
xRayTraceId <- arbitrary[Option[String]]
logGroupName <- arbitrary[String]
logStreamName <- arbitrary[String]
identity <- arbitrary[Option[CognitoIdentity]]
Expand All @@ -99,7 +100,8 @@ trait CloudFormationCustomResourceArbitraries {
logStreamName,
identity,
clientContext,
remainingTime.pure[F]
remainingTime.pure[F],
xRayTraceId
)

implicit def arbContext[F[_]: Applicative]: Arbitrary[Context[F]] = Arbitrary(genContext[F])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sealed abstract class Context[F[_]] {
def identity: Option[CognitoIdentity]
def clientContext: Option[ClientContext]
def remainingTime: F[FiniteDuration]
def xRayTraceId: Option[String]

final def mapK[G[_]](f: F ~> G): Context[G] =
new Context.Impl(
Expand All @@ -45,12 +46,13 @@ sealed abstract class Context[F[_]] {
logStreamName,
identity,
clientContext,
f(remainingTime)
f(remainingTime),
xRayTraceId
)

}

object Context extends ContextCompanionPlatform {
object Context {
i10416 marked this conversation as resolved.
Show resolved Hide resolved
def apply[F[_]](
functionName: String,
functionVersion: String,
Expand All @@ -74,7 +76,37 @@ object Context extends ContextCompanionPlatform {
logStreamName,
identity,
clientContext,
remainingTime)
remainingTime,
None
)
}
def apply[F[_]](
functionName: String,
functionVersion: String,
invokedFunctionArn: String,
memoryLimitInMB: Int,
awsRequestId: String,
logGroupName: String,
logStreamName: String,
identity: Option[CognitoIdentity],
clientContext: Option[ClientContext],
remainingTime: F[FiniteDuration],
xRayTraceId: Option[String]
)(implicit F: Applicative[F]): Context[F] = {
val _ = F // might be useful for future compatibility
new Impl(
functionName,
functionVersion,
invokedFunctionArn,
memoryLimitInMB,
awsRequestId,
logGroupName,
logStreamName,
identity,
clientContext,
remainingTime,
xRayTraceId
)
}

private final case class Impl[F[_]](
Expand All @@ -87,7 +119,8 @@ object Context extends ContextCompanionPlatform {
logStreamName: String,
identity: Option[CognitoIdentity],
clientContext: Option[ClientContext],
remainingTime: F[FiniteDuration]
remainingTime: F[FiniteDuration],
xRayTraceId: Option[String]
Copy link
Author

Choose a reason for hiding this comment

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

propagate X-Ray-Trace-Id to handlers via Context to workaround the limitation that environment variables couldn't be set from JVM runtime.

) extends Context[F] {
override def productPrefix = "Context"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package feral.lambda
import cats.effect.Sync
import cats.effect.std.Env
import cats.syntax.functor._
import io.circe.JsonObject
import io.circe.scalajs._

import scala.concurrent.duration._

private[lambda] object ContextPlatform {

private[lambda] def fromJS[F[_]: Sync: Env](context: facade.Context): F[Context[F]] = Env[F]
.get("_X_AMZN_TRACE_ID")
.map(traceId =>
Context(
context.functionName,
context.functionVersion,
context.invokedFunctionArn,
context.memoryLimitInMB.toInt,
context.awsRequestId,
context.logGroupName,
context.logStreamName,
context.identity.toOption.map { identity =>
CognitoIdentity(identity.cognitoIdentityId, identity.cognitoIdentityPoolId)
},
context
.clientContext
.toOption
.map { clientContext =>
ClientContext(
ClientContextClient(
clientContext.client.installationId,
clientContext.client.appTitle,
clientContext.client.appVersionName,
clientContext.client.appVersionCode,
clientContext.client.appPackageName
),
ClientContextEnv(
clientContext.env.platformVersion,
clientContext.env.platform,
clientContext.env.make,
clientContext.env.model,
clientContext.env.locale
),
clientContext
.custom
.toOption
.flatMap(decodeJs[JsonObject](_).toOption)
.getOrElse(JsonObject.empty)
)
},
Sync[F].delay(context.getRemainingTimeInMillis().millis),
traceId
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private[lambda] abstract class IOLambdaPlatform[Event, Result] {
val io =
for {
event <- IO.fromEither(decodeJs[Event](event))
result <- handle(Invocation.pure(event, Context.fromJS(context)))
ctx <- ContextPlatform.fromJS[IO](context)
result <- handle(Invocation.pure(event, ctx))
} yield result.map(_.asJsAny).orUndefined

dispatcher.unsafeToPromise(io)
Expand Down
Loading