Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including hapi-lambda with all npm packages installed. Try it out:

require("hapi/package.json"); // hapi is a peer dependency. var hapiLambda = require("hapi-lambda")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

hapi-lambda v1.0.0

Support Hapi 17+ applications on Amazon Lambda


npm version

This module will allow you to host your Hapijs (17+) application on Amazon Lambda with node 8.10+. If you are using API Gateway, you should set the Gateway to "proxy" mode.

CAUTION: There are significant breaking changes between this version and the pre-1.0 versions of this module.


// api.js

const Hapi = require('@hapi/hapi');

module.exports = {
  init: async () => {
    const server = new Hapi.server({
      port: process.env.port || 3000,
      routes: { cors: true }

    const plugins = []; // your plugins here
    await server.register(plugins);

    // return the server for Lambda support
    return server;

Your index.js file that you expose to Lambda should look like the following:

// index.js

const api = require('./api');
const { transformRequest, transformResponse } = require('hapi-lambda');

// cache the server for better peformance
let server;

exports.handler = async event => {
  if (!server) {
    server = await api.init();

  const request = transformRequest(event);

  // handle cors here if needed
  request.headers['Access-Control-Allow-Origin'] = '*';
  request.headers['Access-Control-Allow-Credentials'] = true;

  const response = await server.inject(request);

  return transformResponse(response);


Deployment is a much larger topic and not covered by this module, however I highly recommend deploying your Lambda application with Serverless

Usage with Serverless

Here is an example serverless configuration.

service: hapi-lambda-demo
  name: aws
  runtime: nodejs8.10

stage: dev
region: us-east-1

    handler: index.handler
      - http:
          path: "{proxy+}"
          method: any
          cors: true

  - serverless-offline


A working repository and example is provided at

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free