-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
7ea939b
26b1622
95efe18
be96317
ebf5e97
6f990ad
f6a904b
defb679
c95cecb
2de1748
b7e6054
b07406c
0eb162e
24c4261
602460b
25403d0
a65d045
206ec6a
aa06238
39562d6
108c2cb
016df57
7ce4f85
fe73a65
ed8cb7d
f2b7151
0cf6033
e3f8553
7c69d9e
cfd8f0a
accb4b6
6acf432
2449d3e
9d79651
3c4974c
2548296
35d797f
93d55aa
af3b527
f3022e4
d630597
366248a
ab6fa05
54c3032
b5e3019
d6de562
13aa8e9
3517cb7
ffa7bb3
f1f7a2c
f83a9de
c700129
9a49c7d
d53f8be
f284899
3da24a9
8cdfc9b
541704e
89d1c69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just directly request an |
||
implicit val jsonEncoder: EntityEncoder[F, Json] = jsonEncoderWithPrinter[F](Printer.noSpaces.copy(dropNullValues = true)) | ||
val http4sClientDsl = new Http4sClientDsl[F] {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} 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, | ||
|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of converting to millis and back to duration, you can use |
||
) | ||
} | ||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh, does using |
||
|
||
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" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can all be |
||||||||||
|
||||||||||
def apply[F[_]: Functor](env: Env[F]): LambdaRuntimeEnv[F] = new LambdaRuntimeEnv[F] { // should be error effect? | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, on second thought, I think we want:
Suggested change
|
||||||||||
override def handler: F[String] = env.get(HANDLER).map(_.get) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid
Suggested change
|
||||||||||
|
||||||||||
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) | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
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")