lalalambda
Serverless functions powered by hapijs
Lead Maintainer - Devin Ivy
Installation
See further installation and setup instructions below for more information.
npm install @hapipal/lalalambda
Usage
See also the API Reference
Lalalambda is intended for use with hapi v19+, serverless v1 and v2, and nodejs v12+ (see v1 for lower support). Currently only deployments to AWS Lambda are supported, but we are open to expand support with your help!
Lalalambda offers three core features integrating hapi with the Serverless framework:
-
:mount_fuji: The ability to deploy an entire hapi server as a lambda function.
-
:sunrise_over_mountains: The ability to deploy individual hapi routes as lambda functions.
-
:sunrise: The ability to deploy arbitrary lambda functions triggered by cloud events, authored similarly to how you'd create a standard hapi route.
Let's take a quick look at a code example for each of these features.
Examples
Lambda-ify an entire server
server.js
'use strict';
const Hapi = require('@hapi/hapi');
const Lalalambda = require('@hapipal/lalalambda');
exports.deployment = async () => {
const server = Hapi.server();
await server.register({
plugin: Lalalambda,
options: {
lambdaify: true
}
});
server.route({
method: 'get',
path: '/hello/{name?}',
handler: ({ params }) => {
return {
hello: params.name || 'world'
};
}
});
return server;
};
Assuming you've already followed installation, now just deploy to get a URL to your hapi server deployed as a lambda function!
npx serverless deploy
Lambda-ify a single hapi route
server.js
'use strict';
const Hapi = require('@hapi/hapi');
const Lalalambda = require('@hapipal/lalalambda');
exports.deployment = async () => {
const server = Hapi.server();
await server.register(Lalalambda);
server.route({
method: 'get',
path: '/hello/{name?}',
options: {
id: 'hello',
plugins: {
lalalambda: true
},
handler: ({ params }) => {
return {
hello: params.name || 'world'
};
}
}
});
return server;
};
Assuming you've already followed installation, now just deploy to get a URL to your hapi route deployed as a lambda function!
npx serverless deploy
Create an arbitrary lambda function
Here we'll create a lambda that is scheduled to log the most recent earthquake on Earth each minute.
server.js
'use strict';
const Hapi = require('@hapi/hapi');
const Wreck = require('@hapi/wreck');
const Lalalambda = require('@hapipal/lalalambda');
exports.deployment = async () => {
const server = Hapi.server({
debug: {
log: ['earthquake']
}
});
await server.register(Lalalambda);
server.lambda({
id: 'earthquakes',
options: {
events: [{
schedule: 'rate(1 minute)'
}],
handler: async (event, context) => {
const { payload } = await Wreck.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');
const { features: [earthquake] } = JSON.parse(payload.toString());
if (earthquake) {
context.server.log('earthquake', earthquake.properties.title);
}
}
}
});
return server;
};
Assuming you've already followed installation, now just deploy to start logging earthquake data! You can then view these logs in realtime from your terminal.
npx serverless deploy
npx serverless logs --tail --function earthquakes
Installation and setup
Lalalambda is one package that doubles as 1. a hapi plugin and 2. a Serverless framework plugin. These two plugins work together to allow you to define lambda functions in hapi that can be packaged and deployed using the Serverless framework to AWS Lambda. A basic installation has just a few steps.
-
Install the lalalambda and serverless packages from npm.
npm install @hapipal/lalalambda
npm install --save-dev serverless
-
Setup a Serverless config in the root of your project including lalalambda as a plugin.
service: my-service
provider:
name: aws
runtime: nodejs12.x
plugins:
- '@hapipal/lalalambda'
There is also an optional configuration for declaring the path to the server file.
custom:
lalalambda:
serverPath: 'src/my-server.js'
-
Register lalalambda to your hapi server.
If you're using the pal boilerplate then simply add lalalambda to your manifest's plugins
section.
await server.register(require('@hapipal/lalalambda'));
-
Ensure server.js
or server/index.js
exports a function named deployment
that returns your configured hapi server. Note that the server may be initialized but should not be started.
If you're using the pal boilerplate then you can skip this step!
Below is a very simple example of boilerplate code to configure a hapi server, and is not necessarily "production-ready." For a more complete setup, consider using the pal boilerplate, or check-out its approach as seen here.
'use strict';
const Hapi = require('@hapi/hapi');
const Lalalambda = require('@hapipal/lalalambda');
const AppPlugin = require('./app');
exports.deployment = async ({ start } = {}) => {
const server = Hapi.server();
await server.register(Lalalambda);
await server.register(AppPlugin);
if (start) {
await server.start();
console.log(`Server started at ${server.info.uri}`);
}
return server;
};
if (!module.parent) {
exports.deployment({ start: true });
}
Now you have the full expressiveness of the Serverless and hapi frameworks at your fingertips!
The rest of your setup depends on however you want to further configure Serverless and hapi. We suggest checking-out the Serverless AWS Guide, continuing by taking a peek at the Credentials section so that the Serverless CLI will have access to deploy to your AWS account. Going forward you can invoke the Serverless CLI for deployment, etc.:
npx serverless --help
npx sls --help