Native runtime

Even though JVM is supported by AWS Lambda, we have small wrapper that takes care of runtime integration to provide own custom runtime that will work in provided.al2 environment. One reason for it is that we want to use native images for running JVM lambdas (for performance reasons) and second is that built-in runtimes have very limited default serialization capabilities.

In order to run a Lambda handler we need to create a runtime wrapper that takes care about all the API communication by specifying ObjectMapper instance responsible for serialization. Then you can execute your “real” Lambda handler in that wrapper - currently only stream handlers are supported. Signature of your handler needs to be (can be also a method reference as long as the contract is the same):

fun handleRequest(inputStream: InputStream, outputStream: OutputStream, context: Context)

Example on how one to execute the runner (you can investigate signature of NativeLambdaApi class, but most of the arguments are there for testing purposes):

import pl.wrzasq.commons.aws.runtine.NativeLambdaApi

fun main() {
    val handler = YourLogic()
    val objectMapper = ObjectMapper()
    val api = NativeLambdaApi(objectMapper)
    api.run(handler::handleRequest)
}

Note: NativeLambdaApi also handles X-Ray trace ID and exposes it to AWS SDK via system property.

Off-loading bootstrapping to AWS

You can also utilize pl.wrzasq.commons.aws.runtime.Runner as a main class of your deployable and define handler in AWS Lambda to point to resources factory, that needs to implement pl.wrzasq.commons.aws.runtime.config.ResourcesFactory interface:

class LambdaResourcesFactory: ResourcesFactory {
    private val objectMapper: ObjectMapper by lazy {
        ObjectMapperFactory.createObjectMapper()
            .disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
    }

    private val customerDao = DynamoDbCustomerDao()

    override val lambdaApi = NativeLambdaApi(objectMapper)

    override val lambdaCallback = Handler(objectMapper, customerDao)::handle
}

Then you need to specify just a class name your.package.LambdaResourcesFactory as Lambda handler.