Serverless backend

Installation (Gradle)

compile group: "org.http4k", name: "http4k-serverless-lambda", version: "3.21.1"


These modules provide integration with Serverless deployment environments, such as AWS Lambda.

AWS Lambda integration

Since http4k is server independent, it turns out to be fairly trivial to deploy full applications to AWS Lambda, and then call them by setting up the API Gateway to proxy requests to the function. Effectively, the combination of these two services become just another Server back-end supported by the library. This has the added bonus that you can test your applications in a local environment and then simply deploy them to AWS Lambda via S3 upload.

In order to achieve this, only a single interface AppLoader needs to be implemented.

This is far from a complete guide, but configuring AWS Lambda and the API Gateway involves several stages:

  1. Users, Roles and Policies for the API Gateway and Lambda.
  2. API Gateway to proxy all requests to your Lambda.
  3. Building your http4k application into a standard UberJar.
  4. Optionally using Proguard to minify the JAR.
  5. Package up the (minified) JAR into a standard Zip distribution.
  6. Create and configure the Lambda function, and at the same time:

    1. Upload the standard Zip file to S3.
    2. Set the function execution to call the main http4k entry point: org.http4k.serverless.lambda.LambdaFunction::handle
    3. Set an environment variable for the Lambda HTTP4K_BOOTSTRAP_CLASS to the class of your AppLoader class.

We hope to soon provide some tools to automate at least some of the above process, or at least document it somewhat. However, AWS is a complicated beast and many people have a preferred way to set it up: CloudFormation templates, Serverless framework, Terraform, etc. In the meantime, here is an example of how the AppLoader is created and a sneak peak at launching the app locally: