-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleLambda2.scala
58 lines (41 loc) · 1.66 KB
/
ExampleLambda2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.encalmo.lambda.example
import org.encalmo.lambda.LambdaContext
import org.encalmo.lambda.LambdaEnvironment
import org.encalmo.lambda.LambdaRuntime
import org.encalmo.aws.AwsClient
import org.encalmo.utils.JsonUtils.*
import upickle.default.*
import scala.annotation.static
import org.encalmo.aws.LambdaSecrets
import scala.language.experimental.namedTuples
object ExampleLambda2 {
@static def main(args: Array[String]): Unit = new ExampleLambda2().run()
case class Config(greeting: String) derives ReadWriter
case class Response(message: String) derives ReadWriter
}
class ExampleLambda2(maybeAwsClient: Option[AwsClient]) extends LambdaRuntime {
// required for java runtime handler example
final def this() = this(None)
import ExampleLambda2.*
type ApplicationContext = (config: Config, awsClient: AwsClient)
override def initialize(using environment: LambdaEnvironment): ApplicationContext = {
val awsClient = maybeAwsClient
.getOrElse(AwsClient.initializeWithProperties(environment.maybeGetProperty))
val secrets = LambdaSecrets.retrieveSecrets(environment.maybeGetProperty)
val greeting = secrets
.get("SECRET_LAMBDA_GREETING")
.getOrElse("Hello <input>!")
environment.info(
s"Initializing ${environment.getFunctionName()} with a greeting $greeting"
)
val config = Config(greeting)
(config, awsClient)
}
override inline def handleRequest(
input: String
)(using lambdaConfig: LambdaContext, context: ApplicationContext): String = {
val greeting = context.config.greeting.replace("<input>", input)
println(s"Sending greeting: $greeting")
Response(greeting).writeAsString
}
}