Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (46 sloc) 1.99 KB
package guide.modules.serverless
import org.http4k.client.ApacheClient
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.server.SunHttp
import org.http4k.server.asServer
import org.http4k.serverless.AppLoader
import org.http4k.serverless.BootstrapAppLoader
import org.http4k.serverless.lambda.ApiGatewayProxyRequest
import org.http4k.serverless.lambda.LambdaFunction
// This AppLoader is responsible for building our HttpHandler which is supplied to AWS
// It is the only actual piece of code that needs to be written.
object TweetEchoLambda : AppLoader {
override fun invoke(env: Map<String, String>): HttpHandler = {
fun main() {
// Launching your Lambda Function locally - by simply providing the operating ENVIRONMENT map as would
// be configured on AWS.
fun runLambdaLocally() {
val app: HttpHandler = TweetEchoLambda(mapOf())
val localLambda = app.asServer(SunHttp(8000)).start()
println(ApacheClient()(Request(Method.GET, "http://localhost:8000/").body("hello hello hello, i suppose this isn't 140 characters anymore..")))
// the following code is purely here for demonstration purposes, to explain exactly what is happening at AWS.
fun runLambdaAsAwsWould() {
val lambda = LambdaFunction(mapOf(BootstrapAppLoader.HTTP4K_BOOTSTRAP_CLASS to
val response = lambda.handle(ApiGatewayProxyRequest().apply {
path = "/"
body = "hello hello hello, i suppose this isn't 140 characters anymore.."
httpMethod = "GET"
headers = mapOf()
queryStringParameters = mapOf()